Creating two buttons at bottom left/right corner

前端 未结 4 1201
梦谈多话
梦谈多话 2021-01-26 03:09
JButton button1 = new JButton(\"Button 1\");
JButton button2 = new JButton(\"Button 2\");
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout(         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 03:32

    You can add a jPanel and then add the two buttons to it, and then call setBounds on the buttons and specify the position. Then add the jPanel to the jFrame.

    JButton button1 = new JButton("Button 1");
            JButton button2 = new JButton("Button 2");
            JFrame frame = new JFrame();
            JPanel p = new JPanel();
            p.setLayout(null);
            button1.setBounds(10, 400, 100, 40);
            p.add(button1);
            button2.setBounds(375, 400, 100, 40);
            p.add(button2);
            frame.getContentPane().add(p);
            frame.setSize(500, 500);
            frame.setVisible(true);
    

    The bounds are set as (x-coord, y-coord, width, height).

提交回复
热议问题