简体   繁体   English

单击JButton后,JPanel不会出现

[英]JPanel does not appear after click JButton

My JPanel doesn't show up when my JButton is clicked. 单击我的JButton时,我的JPanel不显示。 It does shows up when I add the JPanel in my go() method. 当我在go()方法中添加JPanel时,它的确显示了。 However, once I tried to execute it by click the JButton, it doesn't work. 但是,一旦我尝试通过单击JButton来执行它,它就无法工作。 The program does goes into the loop of the actionPeformed() method of the listener though. 程序确实进入了侦听器的actionPeformed()方法的循环。

public class MyShape 
{
    JFrame frame;
    JPanel panel;
    JButton drawButton;

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

    public void go()
    {
        drawButton = new JButton("Draw Shape!");
        drawButton.addActionListener(new DrawListener());

        frame = new JFrame();
        frame.add(drawButton, BorderLayout.NORTH);
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }    

    private class DrawListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            if(empty)
            {
                System.out.print("IN");
                panel = new DrawPanel();
                frame.add(panel, BorderLayout.CENTER);
            }
        }
    }     

    private class DrawPanel extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            super.paintComponents(g);

            int randNo = (int)(Math.random() * 3);

            int width = (int)(Math.random() * getWidth());
            int height = (int)(Math.random() * getHeight());
            int xpos = getWidth()/2-width/2;
            int ypos = getHeight()/2-height/2;

            int v1 = (int)(Math.random() * 256);
            int v2 = (int)(Math.random() * 256);
            int v3 = (int)(Math.random() * 256);

            g.setColor(new Color(v1, v2, v3));

            if(randNo == 0)
            {   
                g.fillOval(xpos, ypos, width, height);
            }
            else if(randNo == 1)
            {
                g.fillRect(xpos, ypos, width, height);
            }
            else
            {
                int startAngle = (int)(Math.random() * 360);
                int arcAngle = (int)(Math.random() * 360);
                g.fillArc(xpos, ypos, width, height, startAngle, arcAngle);
            }
        }
    }    
}

How do I get the JPanel to show up once the button is clicked? 单击按钮后如何显示JPanel?

You have to call parentComponent.revalidate() every time you do one or more parentComponent.add(childComponent) (or change its children in other ways, such as reorder or remove them). 每次执行一个或多个parentComponent.add(childComponent) (或以其他方式更改其子级,例如重新排序或删除它们)时,都必须调用parentComponent.revalidate() )。

In your case, your code should be 就您而言,您的代码应为

private class DrawListener implements ActionListener {
    public void actionPerformed(ActionEvent event)
    {
        if(empty)
        {
            System.out.print("IN");
            panel = new DrawPanel();
            frame.add(panel, BorderLayout.CENTER);
            frame.revalidate(); // <---------- important
        }
    }
}

Few changes, check this 少量更改,请检查此

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

public void go() {
    drawButton = new JButton("Draw Shape!");
    drawButton.addActionListener(new DrawListener());

    frame = new JFrame();
    frame.getContentPane().add(drawButton, BorderLayout.NORTH);
    panel = new DrawPanel();
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    frame.setSize(500, 500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private class DrawListener implements ActionListener {

    public void actionPerformed(ActionEvent event) {

            frame.getContentPane().remove(1);
            panel = new DrawPanel();
            frame.getContentPane().add(panel, BorderLayout.CENTER);
            frame.repaint();
            frame.validate();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM