有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使JPanel在单击Jbutton时消失

我有两个JPanel的问题。两者都由一个绿色矩形表示,一个比另一个大。我一直在尝试从一个面板切换到另一个面板,这意味着,当你点击按钮时,一个会替换框架中的另一个。但是,我无法找到第二个出现的解决方案。我得到的最多的就是第一次躲藏。我应该如何聚焦它?我没有主意了

public class GrowAndShrinkSquareGUI {
    JFrame frame;
    SquareDrawPanel greenPanel;

    public class SquareDrawPanel extends JPanel {
        int locationX;
        int locationY;
        int width;
        int height;

        SquareDrawPanel(int x, int y, int w, int h) {
            locationX = x;
            locationY = y;
            width = w;
            height = h;
        }

        public void paintComponent(Graphics g) {
            g.setColor(Color.green);
            g.fillRect(locationX, locationY, width, height);
        }
    }

    public class growAndShrinkListener implements ActionListener {
        JButton button;
        public growAndShrinkListener() {
            JButton button = new JButton("Click me to grow the Square");
            frame.add(button, BorderLayout.NORTH);
            button.addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.print("clicked");        
            greenPanel.setVisible(false);
        }
    }

    public static void main(String[] args) {
        GrowAndShrinkSquareGUI test = new GrowAndShrinkSquareGUI();
        test.go();
    }

    public void go() {
        frame = new JFrame();
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        drawPanel(greenPanel);

        growAndShrinkListener button = new growAndShrinkListener();
        //addButton(CreateJButton());
    }

    private JPanel createRectPanel(int x, int y) {
        greenPanel = new SquareDrawPanel(x, y, 100, 100);
        return greenPanel;
    }

    private void drawPanel(JPanel panel) {
        panel = createRectPanel(setLocationX(), setLocationY());
        frame.add(panel, BorderLayout.CENTER); // DoesNot run properly
    }

    private int setLocationX() {
        int centeredX = frame.getWidth() / 2 - 50;
        return centeredX;
    }

    private int setLocationY() {
        int centeredY = frame.getHeight() / 2 - 75;
        return centeredY;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    对于这个特定的任务,您应该使用^{},它使您能够在“卡片”之间快速切换。卡片本质上是与一个名字关联的JComponent

    public class GrowAndShrinkSquareGUI {
        CardLayout layout;
        JPanel cardPanel; // Holds the cards
    
        public class SquareDrawPanel extends JPanel {
            int locationX;
            int locationY;
            int width;
            int height;
    
            SquareDrawPanel(int x, int y, int w, int h) {
                locationX = x;
                locationY = y;
                width = w;
                height = h;
            }
    
            public void paintComponent(Graphics g) {
                g.setColor(Color.green);
                g.fillRect(locationX, locationY, width, height);
            }
        }
    
        public static void main(String[] args) {
            GrowAndShrinkSquareGUI test = new GrowAndShrinkSquareGUI();
            test.go();
        }
    
        public void go() {
            JFrame frame = new JFrame();
            frame.setSize(500, 500);
            frame.setVisible(true);
            frame.setLayout(new BorderLayout());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // Set up CardLayout
            layout = new CardLayout();
    
            // Set up the first panel
            JPanel firstPanel = new JPanel(new BorderLayout());
            JButton switchButton = new JButton("Switch to green panel!");
            switchButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    layout.show(cardPanel, "green");
                }
            }
            firstPanel.add(switchButton, BorderLayout.NORTH);
            layout.add(firstPanel, "first");
    
            // Set up the second panel
            JPanel secondPanel = new SquareDrawPanel(5, 5, 50, 50);
            layout.add(secondPanel, "green");
    
            // Set up the main panel
            cardPanel = new JPanel(layout);
            frame.add(cardPanel);
        }
    
        private int setLocationX() {
            int centeredX = frame.getWidth() / 2 - 50;
            return centeredX;
        }
    
        private int setLocationY() {
            int centeredY = frame.getHeight() / 2 - 75;
            return centeredY;
        }
    }