Я пытаюсь центрировать эти две кнопки Пуск и Назад , но каждый раз, когда я пытаюсь что-либо, например BoxLayout
, .SetLocation
, SwingConstant.CENTER
, .setVerticalAlignment
, .setHorizonatalAlignment
это не работает. Может ли кто-нибудь помочь мне с тем, как установить две кнопки в центре по центру и заголовок «Змея» в верхней части кадра? Спасибо.
package snake;
public class Start {
public static void main(String[] args) {
startScreen startFrame = new startScreen();
}
}
class startScreen extends JFrame {
// constructor
public startScreen() {
// fonts
Font snakeTitleFont = new Font("Arial", Font.BOLD, 50);
Font buttonFont = new Font("Arial", Font.CENTER_BASELINE, 20);
// text
JLabel snakeTitle = new JLabel("Snake", SwingConstants.CENTER);
snakeTitle.setFont(snakeTitleFont);
add(snakeTitle);
// start button
JButton startButton = new JButton("Start");
startButton.setBackground(Color.MAGENTA);
startButton.setOpaque(true);
startButton.setBorderPainted(false);
startButton.setFont(buttonFont);
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.LINE_AXIS));
add(startButton);
// action listener for start btn
startButton.addActionListener(new ActionListener() {
// once this is clicked on, it should call the GUI
@Override
public void actionPerformed(ActionEvent e) {
new Frame();
dispose(); // closes the old form after start is clicked
}
});
// back button
JButton backButton = new JButton("Back");
backButton.setLayout(null);
backButton.setBackground(Color.YELLOW);
backButton.setOpaque(true);
backButton.setBorderPainted(false);
backButton.setFont(buttonFont);
backButton.setBounds(getBounds());
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.LINE_AXIS));
add(backButton);
// action listener for start btn
startButton.addActionListener(new ActionListener() {
// once this is clicked on, it should call the GUI
@Override
public void actionPerformed(ActionEvent e) {
new Frame();
dispose(); // closes the old form after start is clicked
}
});
this.setVisible(true);
this.setSize(800, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
}
}