2016-04-12 9 views
0

JFrame ve BoxLayout'ı, gösterilene benzer bir GUI elde etmek için kullanmaya çalışıyorum, ancak Stop ve Play düğmelerimi nasıl merkezleyeceğimi bilmiyorum. Baska öneri? Henüz ortalama anlamaya edemedik olarak henüz metin kutusu ve yük düğmesini kodlu değilJFrame'de BoxLayout'u Kullanma

JFrame frame = new JFrame(); 

    Box box = Box.createHorizontalBox(); 
    box = Box.createHorizontalBox(); 
    box.add(new JButton("Play")); 
    box.add(new JButton("Stop")); 
    box.add(Box.createHorizontalGlue()); 
    frame.add(box, BorderLayout.SOUTH); 

    frame.setSize(500, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    frame.setVisible(true); 

:

enter image description here

İşte benim kodudur.

cevap

2

Düğmeler için ayrı bir panel oluşturun. Yatay yapıştırıcı ile düğmelerinizi ortalayabilirsiniz.

JPanel buttonPanel = new JPanel(); 
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); 

buttonPanel.add(Box.createHorizontalGlue()); 

buttonPanel.add(new JButton("Play")); 
buttonPanel.add(new JButton("Stop")); 

buttonPanel.add(Box.createHorizontalGlue()); 

frame.add(buttonPanel, BorderLayout.SOUTH); 

Ayrıca

JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); 

buttonPanel.add(new JButton("Play")); 
buttonPanel.add(new JButton("Stop")); 

frame.add(buttonPanel, BorderLayout.SOUTH); 
kolayca FlowLayout ile yapabilirsiniz