2014-05-15 51 views
0
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); 
    } 

} 

如何将此按钮从左侧移动到窗口中央?他们应该保持关闭。我正在尝试一切,但他们仍然在左侧。 Screenshot如何正确设置布局?

回答

0

如果我正确理解你的问题,你想在JFrame水平居中的JRadioButtons,是吗?尝试使用setHorizontalAlignment()方法。将以下几行添加到您的initComponents()功能中:

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