Java 使用按钮将对象添加到JFrame

Java 使用按钮将对象添加到JFrame,java,swing,Java,Swing,我创建了四个类-三个用于形状,一个用于显示所有内容。每次单击按钮btn时,我想向frame2添加一个形状。我已经找了一段时间了,但我想不出来 这是我的密码: import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; public class Window extends JFrame implements ActionListener{ public JFram

我创建了四个类-三个用于形状,一个用于显示所有内容。每次单击按钮
btn
时,我想向
frame2
添加一个形状。我已经找了一段时间了,但我想不出来

这是我的密码:

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

public class Window extends JFrame implements ActionListener{

 public JFrame frame = new JFrame("Prototype");
 public JFrame frame2 = new JFrame("Container");
 JLabel l1 = new JLabel("  " + "2.Choose a color: ");    
 JLabel l2 = new JLabel("Type x coordinates: ");
 JLabel l3 = new JLabel("Type y coordinates: ");
 JLabel l4 = new JLabel("Type width: ");
 JLabel l5 = new JLabel("Type height: ");
 public JTextField t1 = new JTextField(10);
 public JTextField t2 = new JTextField(10);
 public JTextField t3 = new JTextField(10);
 public JTextField t4 = new JTextField(10);
 private JButton btn = new JButton("Make It");
 private JButton btn1 = new JButton("Show ArrayList");
 JMenuBar mb = new JMenuBar();
 JMenu mn = new JMenu("1.Choose a shape: ");
 JRadioButtonMenuItem fr = new JRadioButtonMenuItem("Filled Rectangle", new ImageIcon("src/webicon.jpg"),true);
 JRadioButtonMenuItem uo = new JRadioButtonMenuItem("Unfilled Oval", new ImageIcon("src/webcon2.png"));
 JRadioButtonMenuItem fo = new JRadioButtonMenuItem("Filled Oval", new ImageIcon("src/webcon3.png"));
 JColorChooser newColor;
 Color color;
 ArrayList<String> shapes = new ArrayList<String>();

    public Window () {
         ButtonGroup bg = new ButtonGroup();
         bg.add(uo);     mb.add(mn);     mn.add(fo);
         bg.add(fo);     mn.add(uo);     mn.add(fr);
         bg.add(fr);    

                 JPanel p = new JPanel();
                    p.setLayout(new BorderLayout(2,0));
                    p.add(l1, BorderLayout.NORTH);
                    newColor = new JColorChooser();
                    p.add(newColor, BorderLayout.CENTER);

                 JPanel p1 = new JPanel();
                    p1.setLayout(new GridLayout(4,2));
                    p1.add(l2); p1.add(t1);  
                    p1.add(l3); p1.add(t2);
                    p1.add(l4); p1.add(t3);
                    p1.add(l5); p1.add(t4);

                 JPanel p2 = new JPanel();
                    p2.setLayout(new GridLayout(1,2));
                    p2.add(btn);
                    btn.addActionListener(this);
                    p2.add(btn1);
                    btn1.addActionListener(this);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setJMenuBar(mb);
                frame.getContentPane().add(p, BorderLayout.NORTH);
                frame.getContentPane().add(p1, BorderLayout.CENTER);
                frame.getContentPane().add(p2, BorderLayout.SOUTH);
                frame.setLocation(500,300);
                frame.setVisible(true);
                frame.setSize(500,520); 

                frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame2.setVisible(true);
                frame2.setSize(500,520);
            //  ...
    }

    public void actionPerformed(ActionEvent e) {
        int x = Integer.parseInt(this.t1.getText());
        int y = Integer.parseInt(this.t2.getText());
        int width = Integer.parseInt(this.t3.getText());
        int height = Integer.parseInt(this.t4.getText());

        if(e.getSource() == btn){
             if(fr.isSelected()){
                 this.color = this.newColor.getColor();
                 // ...
                 new FilledRectangle(x, y, width, height, this.color);
                 this.shapes.add("Filled Rectangle");
                 }
             else if(fo.isSelected()){
                 this.color = this.newColor.getColor();
                 new FilledOval(x, y, width, height, this.color);
                 this.shapes.add("Filled Oval");}
             else if(uo.isSelected()){
                 this.color = this.newColor.getColor();
                 new UnfilledOval(x, y, width, height, this.color);
                 this.shapes.add("Unfilled Oval");}
        }
        else if(e.getSource() == btn1){
            for (int i = 0; i < this.shapes.size(); i++) {
                System.out.println(this.shapes.get(i));
            }
        }

}

}

如果有人能帮助我,我将不胜感激。

我不明白你想做什么。您有一个
窗口
,它是一个
JFrame
。然后在
窗口中
类中有2个
JFrame
变量,
frame
frame2
。您已使
frame
frame2
可见,这意味着运行程序时会显示2
JFrame
s。然后你有了这些形状类,它们又是
Frame
s。那么您想做什么呢?在
窗口.actionPerformed
方法中,您可以创建组件,而无需对组件执行任何操作(例如:
新建FilledRectangle(x,y,width,height,this.color)
)。您可能希望将它们添加到
frame2
?1)请参见2)要更快获得更好的帮助,请发布(最简单的完整可验证示例)或(简短、自包含、正确的示例)。3) 当存在
javax.swing.JFrame
时,不要使用
java.awt.Frame
!4) 使用缩进代码行和代码块的逻辑和一致形式。缩进的目的是使代码的流程更易于遵循!5)
公共类窗口扩展JFrame..
不要在类中使用AWT类的名称!我想在
frame2
中添加这个新的圆角角度(x,y,width,height,this.color)。不要扩展
Frame
进行自定义绘制。自定义绘制是通过覆盖JPanel(或JComponent)的
paintComponent()
方法完成的,然后将面板添加到框架中。
 import java.awt.Color;
 import java.awt.Frame;
 import java.awt.Graphics;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;

public class FilledRectangle extends Frame{
private int x, y, width, height;
private Color color;
public String name = "Filled Rectangle";

public FilledRectangle(int x, int  y, int width, int height, Color color){  
    this.x = x;
    this.y = y;
    this.width = width; 
    this.height = height;
    this.color = color;
}
public void paint(Graphics g){
    g.setColor(color);
    g.fillRect(this.x, this.y, this.width, this.height);
}
}