2016-05-01 13 views
1

Ich mag einfaches Menü mit schwierigen Ebenen istWie JLabel und JButton in BoxLayout zum Zentrum

Screen shot

Nächste paar Zeilen Code erstellen Konstruktor.

super(); 

setMinimumSize(new Dimension(600, 300)); 

setMaximumSize(new Dimension(600, 300)); 

setPreferredSize(new Dimension(600, 300)); 

setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); 

addButtons(); 

Methode addButtons() Schaltflächen hinzufügen, die Sie auf Screen sehen:

add(Box.createVerticalGlue()); 

addLabel("<html>Current level <b>" + Game.instance() 
             .getLevelString() + 
     "</b></html>"); 

add(Box.createVerticalGlue()); 

addButton("Easy"); 

add(Box.createVerticalGlue()); 

addButton("Normal"); 

add(Box.createVerticalGlue()); 

addButton("Hard"); 

add(Box.createVerticalGlue()); 

addButton("Back"); 

add(Box.createVerticalGlue()); 

Methode addButton()

private void addButton(String text) 
{ 
    JButton button = new JButton(text); 
    button.setAlignmentX(JButton.CENTER_ALIGNMENT); 
    button.setFocusable(false); 

    add(button); 
} 

Und addLabel()

private void addLabel(String text) 
{ 
    JLabel label = new JLabel(text, JLabel.CENTER); 

    add(label); 
} 

Ich weiß nicht, wie man alle Elemente zur Mitte bringt. Es ist ein Problem für mich. Zusätzliches Problem ist, wenn ich schwieriges Niveau ändere, kann Text auf JLabel in ändern, für einfaches 'aktuelles Niveau EASY'. Dann bewegen JButtons eine Menge Pixel richtig und ich weiß nicht warum.

Antwort

2

Zweiter Parameter im public JLabel(String text, int horizontalAlignment) ist für die Bestimmung der Textposition des Etiketts. Sie müssen die JLabel Komponente der Ausrichtung durch setAlignmentX Methode einstellen.

private void addLabel(String text) { 
    JLabel label = new JLabel(text, JLabel.CENTER); 
    label.setAlignmentX(JLabel.CENTER_ALIGNMENT); 
    add(label); 
} 

Edit:

Ihre zweite Frage ist seltsam. Ich weiß nicht, warum das passiert, aber ich denke, das Erstellen eines zweiten Bedienfelds für Schaltflächen wird Ihr Problem lösen.

in Konstruktor Grenz Layout:

super(); 

//set size 

setLayout(new BorderLayout()); 

addButtons(); 

addButtons() Methode:

//you can use empty border if you want add some insets to the top 
//for example: setBorder(new EmptyBorder(5, 0, 0, 0)); 

addLabel("<html>Current level <b>" + Game.instance() 
            .getLevelString() + 
    "</b></html>"); 

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

buttonPanel.add(Box.createVerticalGlue()); 

buttonPanel.add(createButton("Easy")); 

buttonPanel.add(Box.createVerticalGlue()); 

//Add all buttons 

add(buttonPanel, BorderLayout.CENTER); 

createButton() Methode

private JButton createButton(String text) 
{ 
    JButton button = new JButton(text); 
    button.setAlignmentX(JButton.CENTER_ALIGNMENT); 
    button.setFocusable(false); 

    return button; 
} 

addLabel() Methode

private void addLabel(String text) 
{ 
    JLabel label = new JLabel(text, JLabel.CENTER); 
    add(label, BorderLayout.NORTH); 
} 
+0

Ok, es funktioniert, aber wenn ich die Ebene (und den Text in 'JLabel') ändere, verschieben meine Buttons einige Pixel zur rechten Kante. – ventaquil

+0

@ventaquil Siehe meine Bearbeitung. – rdonuk

+0

es funktioniert, danke :) – ventaquil