でのJLabelとのJButtonを中央にどのように困難なレベルBoxLayout
コードの次の数行でシンプルなメニューを作成するコンストラクタです。あなたがscreenshoot上で見ることができるボタンを追加addButtons()
super();
setMinimumSize(new Dimension(600, 300));
setMaximumSize(new Dimension(600, 300));
setPreferredSize(new Dimension(600, 300));
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
addButtons();
方法:
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());
方法addButton()
private void addButton(String text)
{
JButton button = new JButton(text);
button.setAlignmentX(JButton.CENTER_ALIGNMENT);
button.setFocusable(false);
add(button);
}
そしてaddLabel()
private void addLabel(String text)
{
JLabel label = new JLabel(text, JLabel.CENTER);
add(label);
}
私はすべての要素をどのように中心に合わせるかを知らない。それは私にとっては問題です。追加の問題は、JLabel
の難しいレベルのテキストを簡単な「現在のレベルEASY」に変更することができます。その後、JButtons
は多くのピクセルを右に動かしているのですが、その理由はわかりません。
これは機能していますが、レベル(および 'JLabel'のテキスト)を変更すると、ボタンによってピクセルが右端に移動します。 – ventaquil
@ventaquil私の編集をご覧ください。 – rdonuk
それは働いています、ありがとう:) – ventaquil