سؤال

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);
    }

}

How can I move this button from the left to the center of the window? They should stay close. I was trying everything but they still are on the left side. Screenshot

هل كانت مفيدة؟

المحلول

If I understand your question correctly, you want to horizontally center the JRadioButtons in the JFrame, yes? Try the setHorizontalAlignment() method. Add the following lines to your initComponents() function:

    latwy.setHorizontalAlignment(JRadioButton.CENTER);
    sredni.setHorizontalAlignment(JRadioButton.CENTER);
    trudny.setHorizontalAlignment(JRadioButton.CENTER);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top