如何正确设置布局?

用户名
public class Gridlayout extends JFrame {

   public Gridlayout()
   {
         setTitle("xxx");

         setSize(540,635);
         this.setResizable(false);
         this.setLocation(500,50);

         initComponents();
         setDefaultCloseOperation(3);
   }

    JLabel etykieta = new JLabel("Poziom trudności: "); 

    JPanel top = new JPanel(new GridLayout(2, 2));
    JPanel mid = new JPanel(new GridLayout(0, 1));
    JPanel bot = new JPanel(new GridLayout(1, 0));

    JButton start= new JButton("Start");
    JButton zakoncz = new JButton("Zakoncz");

    JRadioButton latwy = new JRadioButton("Łatwy");
    JRadioButton sredni = new JRadioButton("Średni");
    JRadioButton trudny = new JRadioButton("Trudny");


    ButtonGroup poziomTrudnosci= new ButtonGroup();


public void initComponents() {


        this.getContentPane().add(top, BorderLayout.NORTH);
        this.getContentPane().add(mid, BorderLayout.CENTER);
        this.getContentPane().add(bot, BorderLayout.SOUTH);

        start.setPreferredSize(new Dimension(1, 40));
        zakoncz.setPreferredSize(new Dimension(1, 40));

         mid.add(etykieta);
         mid.add(latwy);
         mid.add(sredni);
         mid.add(trudny);
         bot.add(start);


        poziomTrudnosci.add(latwy);
        poziomTrudnosci.add(sredni);
        poziomTrudnosci.add(trudny);

     }


    public static void main(String[] args) {

        new Gridlayout().setVisible(true);
    }

}

如何将该按钮从左侧移动到窗口中心?他们应该保持靠近。我正在尝试所有操作,但它们仍在左侧。截屏

那个莫顿

如果我没有理解你的问题,你想水平居中的JRadioButtonsJFrame,是吗?尝试该setHorizontalAlignment()方法。将以下行添加到您的initComponents()函数:

    latwy.setHorizontalAlignment(JRadioButton.CENTER);
    sredni.setHorizontalAlignment(JRadioButton.CENTER);
    trudny.setHorizontalAlignment(JRadioButton.CENTER);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章