单击JButton后,JPanel不会出现

时间:2013-11-05 15:14:59

标签: java swing jpanel awt jbutton

单击我的JButton时,我的JPanel不显示。当我在go()方法中添加JPanel时,它会显示出来。但是,一旦我尝试通过单击JButton执行它,它就不起作用。该程序确实进入了侦听器的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);
            }
        }
    }    
}

单击按钮后,如何让JPanel显示?

2 个答案:

答案 0 :(得分:1)

每次执行一项或多项parentComponent.revalidate()(或以其他方式更改其子项,例如重新排序或删除它们)时,您必须致电parentComponent.add(childComponent)

在您的情况下,您的代码应该是

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
        }
    }
}

答案 1 :(得分:1)

几乎没有变化,请检查

 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();

    }
}