Java 向JFrame添加2个或更多对象

Java 向JFrame添加2个或更多对象,java,swing,layout,jframe,Java,Swing,Layout,Jframe,我想在JFrame上显示2个对象。我尝试将对象添加到JPanel,然后将JPanel添加到JFrame,但也没有成功。我还尝试将ball和ball1对象直接添加到JFrame,但它只显示最后添加的对象。我想一次在JFrame上显示两个对象。下面给出的代码仅显示ball1对象 JFrame f = new JFrame("Moving"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //making 2 obje

我想在JFrame上显示2个对象。我尝试将对象添加到JPanel,然后将JPanel添加到JFrame,但也没有成功。我还尝试将ball和ball1对象直接添加到JFrame,但它只显示最后添加的对象。我想一次在JFrame上显示两个对象。下面给出的代码仅显示ball1对象

    JFrame f = new JFrame("Moving"); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //making 2 objects 
    Ballbewegung2 ball = new Ballbewegung2();
    Ballbewegung3 ball1 = new Ballbewegung3(); 
    JPanel contentPane = new JPanel(new BorderLayout());
    JPanel contentPane1 = new JPanel(new BorderLayout());

    //adding objects to JPanel
    contentPane.add(ball, BorderLayout.CENTER);                 
    contentPane1.add(ball1, BorderLayout.CENTER);                 

    //Adding JPanel to JFrmae
    f.getContentPane().add(contentPane);
    f.getContentPane().add(contentPane1);
    f.setSize(500, 500);
    f.setVisible(true);  

默认情况下,
JFrame
的内容窗格具有
BorderLayout
layout管理器。这意味着,如果向其添加组件,它将被放置在中心。如果向其添加另一个零部件,它将再次放置在中心,并替换先前添加的零部件

如何添加多个组件的示例:

JFrame f = new JFrame();

JPanel p = new JPanel();

p.add( new JButton( "One" ) );
p.add( new JButton( "Two" ) );

f.getContentPane().add( p, BorderLayout.CENTER );
或者在将组件添加到内容窗格时,指定放置组件的位置(并指定不同的位置):


我建议您使用一个JPanel将两个JPanel作为子对象,并将一个JPanel添加到JFrame内容窗格中

如果没有明确指定不同的布局位置,那么对JFrame.add()方法的第二个调用将替换第一个添加的JPanel

使用BoxLayout的一个简单示例:

JPanel mainPanel= new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

mainPanel.add(contentPane);
mainPanel.add(contentPane1);
contentPane.add(mainPanel);

下面是一个实现类似这样的UI的示例

使用的Java组件如下所示

代码:

类主面板

public class MainPanel extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;
    private int m_nX, m_nY;
    private int m_nHeight = 30, m_nWidthLabel = 500, m_nPadding = 2;

    private JLabel m_lblFilename, m_lblFileGen;

    public MainPanel() {
        // TODO Auto-generated constructor stub
        super(new BorderLayout());
        try {
            this.setLayout(null); 
            this.setBorder(new TitledBorder(new EtchedBorder(),
                "Dynamic Time Warping"));

            m_nX = this.getX() + 12;
            m_nY = this.getY() + 24;

            // Add the Filename Label
            m_lblFilename = new JLabel("Label1");
            m_lblFilename.setBorder(new LineBorder(Color.BLUE, 2));
            m_lblFilename.setBounds(nX, nY, m_nWidthLabel, m_nHeight);
            this.add(m_lblFilename);

            // Adding a Label
            nY += m_lblFilename.getHeight() + m_nPadding;
            m_lblFileGen = new JLabel("Label1");
            m_lblFileGen.setBorder(new LineBorder(Color.RED, 2));
            m_lblFileGen.setBounds(nX, nY, m_nWidthLabel, 3 * m_nHeight);
            m_lblFileGen.setForeground(Color.BLUE);
            this.add(m_lblFileGen);
        } catch (Exception e) {
        e.printStackTrace();
    }
}

我为显示它们的对象指定了不同的坐标。我还尝试了第二种方法,它仍然覆盖第一个对象。错误,您调用了两次
f.getContentPane()
,传递了两个不同的组件,但没有指定约束。一个带有BorderLayout且没有明确指定约束的面板将把组件添加到中心位置,第二个组件将替换第一个组件。当我在JFrame上添加超过1个按钮时,代码运行良好,但它不适用于我类的对象。我还尝试使用3个JPanles,然后将mainPanel添加到JFrame,但输出仍然相同。
// Call this function from the main
private static void createAndShowGUI() {
    // Create and set up the content pane.
    MainPanel panel = new MainPanel();
    panel.setOpaque(true); // content panes must be opaque

    // Display the window.
    JFrame frmConsole = new JFrame("ITSME");
    frmConsole.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmConsole.setPreferredSize(new Dimension(640, 480));
    frmConsole.add(panel);
    frmConsole.pack();
    frmConsole.setLocationRelativeTo(null);
    frmConsole.setVisible(true);
}
public class MainPanel extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;
    private int m_nX, m_nY;
    private int m_nHeight = 30, m_nWidthLabel = 500, m_nPadding = 2;

    private JLabel m_lblFilename, m_lblFileGen;

    public MainPanel() {
        // TODO Auto-generated constructor stub
        super(new BorderLayout());
        try {
            this.setLayout(null); 
            this.setBorder(new TitledBorder(new EtchedBorder(),
                "Dynamic Time Warping"));

            m_nX = this.getX() + 12;
            m_nY = this.getY() + 24;

            // Add the Filename Label
            m_lblFilename = new JLabel("Label1");
            m_lblFilename.setBorder(new LineBorder(Color.BLUE, 2));
            m_lblFilename.setBounds(nX, nY, m_nWidthLabel, m_nHeight);
            this.add(m_lblFilename);

            // Adding a Label
            nY += m_lblFilename.getHeight() + m_nPadding;
            m_lblFileGen = new JLabel("Label1");
            m_lblFileGen.setBorder(new LineBorder(Color.RED, 2));
            m_lblFileGen.setBounds(nX, nY, m_nWidthLabel, 3 * m_nHeight);
            m_lblFileGen.setForeground(Color.BLUE);
            this.add(m_lblFileGen);
        } catch (Exception e) {
        e.printStackTrace();
    }
}