Java我无法插入组件

时间:2017-11-30 14:48:54

标签: java swing

添加组件时的原因

config.add(my_cirle);

未添加

intellij用紫色告诉我 enter image description here      public static void main(String [] args){

    JFrame app = new JFrame("Draw Draw");
    JButton cirle = new JButton("Circle");
     final JPanel config = new JPanel();

    cirle.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int cirle = Integer.parseInt(JOptionPane.showInputDialog("Insert circumference"));
            Cirle my_cirle = new Cirle(cirle);
            config.add(my_cirle);  //this Problem
        }
    });

    config.add(cirle);
    app.add(config);
    app.setVisible(true);
    app.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    app.setSize(300,300);
}

这在我的班级圆圈中起作用:

    public class Cirle extends JComponent{
    private int width,height;
    private Ellipse2D miniCircle;
    public Cirle(int radius){
        miniCircle = new Ellipse2D.Double(0,0,2*radius,2*radius);
    }
    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;

        g2.draw(miniCircle);
    }

}

这项工作:

    public static void main(String[] args){

    JFrame app = new JFrame("Draw Draw");

    int cirle = Integer.parseInt(JOptionPane.showInputDialog("Insert circumference"));
    Cirle my_cirle = new Cirle(cirle);

    app.add(my_cirle);
    app.setVisible(true);
    app.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    app.setSize(300,300);
}

enter image description here

0 个答案:

没有答案