Some Problems designing the GUI
So, I've started designing a TicTacToe GUI in Java & I'm stuck.
Here's the code I'm working on
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class gameWindow {
int b;
JFrame frameX;
JPanel panel1, panel2, panel3, panel4;
JLabel[] labelX = new JLabel[10];
JTextField inputter;
JButton input;
JButton exit;
public static void main(String[] args) {
gameWindow xyz = new gameWindow();
xyz.go();
}
public void go() {
frameX = new JFrame();
//frameX.setResizable(false);
LayoutManager lay1 = new BoxLayout(panel1, BoxLayout.X_AXIS);
LayoutManager lay2 = new BoxLayout(panel2, BoxLayout.X_AXIS);
LayoutManager lay3 = new BoxLayout(panel3, BoxLayout.X_AXIS);
panel1 = new JPanel(/*lay1*/);
panel2 = new JPanel(/*lay2*/);
panel3 = new JPanel(/*lay3*/);
panel4 = new JPanel();
//
for (b=1; b<10; b++) {
labelX[b] = new JLabel();
}
//
inputter = new JTextField();
input = new JButton();
exit = new JButton();
frameX.setSize(300,300);
frameX.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameX.setVisible(true);
//
for(b=1; b<10; b++) {
labelX[b].setForeground(Color.BLUE开发者_JAVA技巧);
labelX[b].setPreferredSize(new Dimension(50,50));
labelX[b].setText("X");
if (b<4) {
panel1.add(labelX[b]);
} else if(b>3 && b<7) {
panel2.add(labelX[b]);
} else if(b>6) {
panel3.add(labelX[b]);
}
}
//
inputter.setPreferredSize(new Dimension(50,50));
inputter.setText("Enter box number here");
input.setPreferredSize(new Dimension(50,50));
input.setText("Play");
exit.setPreferredSize(new Dimension(50,50));
exit.setText("Exit");
exit.addActionListener(new forExit());
panel4.add(inputter);
panel4.add(input);
panel4.add(exit);
//Adding Panels to the frame
frameX.getContentPane().add(BorderLayout.NORTH, panel1);
frameX.getContentPane().add(BorderLayout.CENTER, panel2);
frameX.getContentPane().add(BorderLayout.SOUTH, panel3);
frameX.getContentPane().add(BorderLayout.EAST, panel4);
}
class forExit implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
}
1) The first problem that I'm getting is that whenever I compile & run this, no component shows up on the JFrame. But then, if I maximize the frame all components appear. What's that?
2) If I uncomment the lay1, lay2 & lay3 in the JPanels declaration to use the JPanel's (Layout) constructor, I get the BoxLayout can't be shared
error(runtime) & nothing appears on the frame. In this case, even maximizing doesn't helps.
What am I doing wrong & do you have a better way to create the GUI?
For your first problem, set the visibility status (frameX.setVisible(true);
) after the addition of all the components inside the frame. In others words, put this line at the end of the go
method.
Your problem is that the JFrame
is displayed, and after that, you change his content. So the frame must be refreshed to be displayed with his new content. If you set the visibility status to true
after adding the content, you will not get this problem.
Regarding your second problem, I never use the BoxLayout
, but maybe you can have a look at this page that explains how to use it. Otherwise, maybe you can consider using another layout?
BoxLayout does cause these types of problems. You have to do this:
panel1 = new JPanel();
panel1.setLayout(lay1);
So the problem stems from the order in which you are doing things.
When you do this line:
LayoutManager lay1 = new BoxLayout(panel1, BoxLayout.X_AXIS);
panel1 is not yet initialized so it is null. Unfortunately BoxLayout will complain about this. To solve the problem you need:
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
LayoutManager lay1 = new BoxLayout(panel1, BoxLayout.X_AXIS);
LayoutManager lay2 = new BoxLayout(panel2, BoxLayout.X_AXIS);
LayoutManager lay3 = new BoxLayout(panel3, BoxLayout.X_AXIS);
panel1.setLayout(lay1);
panel2.setLayout(lay2);
panel3.setLayout(lay3);
精彩评论