Creating two buttons at bottom left/right corner

前端 未结 4 1121
梦谈多话
梦谈多话 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:26

    Create a container for both.

    JPanel south = new JPanel(new AxisLayout(AxisLayout.HORIZONTAL));
    south.add(button1);
    south.add(button2);
    frame.getContentPane().add(south, BorderLayout.SOUTH);
    

    Obs: Sorry dont remember exactly Swing layout manangers, but you will find the AxisLayout to solve this

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2021-01-26 03:38

    You might want to consider using BoxLayout's horizontalGlue:

    import java.awt.BorderLayout;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class ButtonsLeftAndRight {
        private JFrame frame;
        private JPanel pane;
        private JButton button1;
        private JButton button2;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new ButtonsLeftAndRight()::createAndShowGui);
        }
    
        public void createAndShowGui() {
            frame = new JFrame(getClass().getSimpleName());
    
            pane = new JPanel();
            pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS));
    
            button1 = new JButton("Button1");
            button2 = new JButton("Button2");
    
            pane.add(button1);
            pane.add(Box.createHorizontalGlue());
            pane.add(button2);
    
            frame.add(pane, BorderLayout.SOUTH);
    
            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    

    This might get you this, before and after resizing:

    0 讨论(0)
  • 2021-01-26 03:43

    Another alternative is to use the GUI builder, and modify the code accordingly.

    JButton button1 = new JButton("Button 1");
            JButton button2 = new JButton("Button 2");
            JFrame frame = new JFrame();
            GroupLayout layout = new GroupLayout(frame.getContentPane());
            frame.getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                            .addGap(25, 25, 25)
                            .addComponent(button1)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 215, Short.MAX_VALUE)
                            .addComponent(button2)
                            .addContainerGap())
            );
            layout.setVerticalGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addContainerGap(256, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(button1)
                                    .addComponent(button2))
                            .addGap(25, 25, 25))
            );
            frame.setSize(500, 500);
            frame.setVisible(true);
    

    0 讨论(0)
提交回复
热议问题