Reputation: 37
Just getting started with GUI's and am really baffled with this bit of code, for some reason when I set the default close operation, it does not work, and same when adding components like the JLabel and the TextField. I have extended my class with JFrame, and I have no other JFrames or JPanels with the same name in my program. Oh and also, the setVisible and the setSize still work, it's just when adding components or setting the close operation. Here is the class that calls the method that contains the call for makeWindow().
public static class Play implements ActionListener{
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
game.playGame();
}
And here is the actual method:
private void makeWindow() {
JFrame window = new JFrame("Battleships 2.0");
JPanel canvas = new JPanel();
JLabel title = new JLabel("Battleships 2.0");
title.setFont(font);
JTextField userCoordinates = new JTextField();
window.setSize(500,500);
window.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
window.setResizable(false);
window.setVisible(true);
canvas.setLayout(new BoxLayout(canvas, BoxLayout.Y_AXIS));
window.add(canvas);
canvas.add(title);
canvas.add(userCoordinates);
}
Upvotes: 0
Views: 1209
Reputation: 688
If you have extended JFrame in your class then you don't need to create a JFrame object again in your method.You have used BOXLayout,so your textfield is occupying the entire panel,with the Label appearing on top. I just modified your code to use FlowLayout and the controls are clearly visible. Check this:
private void makeWindow() {
// JFrame window = new JFrame("Battleships 2.0");
setTitle("Battleships 2.0");
JPanel canvas = new JPanel();
JLabel title = new JLabel("Battleships 2.0");
// title.setFont(font);
JTextField userCoordinates = new JTextField(10);
setSize(500, 500);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setResizable(false);
setVisible(true);
canvas.setLayout(new FlowLayout());
add(canvas);
canvas.add(title);
canvas.add(userCoordinates);
}
Upvotes: 4