I have one JFrame that is set to a GridLayout of size NxN. N is given by the user as a command line at the start of the program. JButtons in an NxN pattern are added to the window in JPanels, set in location by GridLayout (I think).
Does each JButton need its own JPanel to use GridLayout? I'm under the impression that you can set up just one JPanel for all the buttons and set the JPanel to a GridLayout for the JButtons. I want to add another JPanel to the left of the button array to display button clicks (JLabel) and a reset button within that same left JPanel.
Here is (a little of) my code, where N is given by the user, system is my background processes class, and ButtonEvent is the class for ActionListener/actionPerformed:
JFrame window = new JFrame("");
GridLayout layout = new GridLayout(N,N);
window.setLayout(layout);
for (int row = 0; row < N; row++){
for (int col = 0; col < N; col++){
JPanel panel = new JPanel();
JButton b = new JButton ("("+row+","+col+")");
window.add(b).setLocation(row, col);
panel.add(b);
b.addActionListener(new ButtonEvent(b, system, row, col));
window.add(panel);
}
}
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
And this is what I have (N=4):
http://i.imgur.com/nbQoM.png
Here is (approximately) what I am looking for (N=4):
http://i.imgur.com/SiVWO.png
All I need/want is two (or more) JPanels that are set up roughly like above, and all the layout managers I've tried don't play nice with the GridLayout layout JFrame.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…