Creating two buttons at bottom left/right corner

前端 未结 4 1197
梦谈多话
梦谈多话 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: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);
    

提交回复
热议问题