Reputation: 27
I am having problems with drawing a 2D JButton
array to a JFrame
. All the JButtons
draw properly except for the last one, the last JButton
to be rendered is fitted to the JFrame
. I have set the width and height of all the JButtons
to 100x100
, but the last JButton
to render does not have a width and height of 100x100
. I printed the properties of the console, and it said the button height and width was 494X496
.
Main class that runs everything:
package gameData;
import javax.swing.*;
public class GameRun {
JFrame frame;
ActionHandle AH = new ActionHandle();
Screen screen = new Screen();
public GameRun() {
beginSession();
screen.renderScreen(frame, AH);
}
public void beginSession() {
JFrame frame = new JFrame();
frame.setSize(500, 525);;
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Game");
this.frame = frame;
}
public static void main(String[] args) {
new GameRun();
}
}
how the JButtons
are drawn:
package gameData;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Screen {
public void renderScreen(JFrame frame, ActionListener AL){
JButton[][] button = new JButton[5][5];
for(int i =0; i<button.length;i++){
for(int j = 0; j<button[i].length; j++){
button[i][j] = new JButton(i+" "+j);
button[i][j].setBounds(i*100, j*100, 100, 100);
button[i][j].addActionListener(AL);
frame.add(button[i][j]);
}
}
}
}
Upvotes: 1
Views: 215
Reputation: 347332
JFrame
uses a BorderLayout
by default, so you are adding all the buttons to the same location on the frame, possibly overlapping them. Instead, you should consider using a GridBagLayout
instead. See How to Use GridBagLayout for more detailsgetPreferredSize
method if you want to affect they way in which the buttons are laid out.setResizable
before you set the size of the framepack
instead of setSize
, frames have window decorations which get fitted inside the window, not added to it, this will affect the amount of viewable space you have for the content, pack
will do all these calculations for youFor example...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
JButton[][] button = new JButton[5][5];
for (int i = 0; i < button.length; i++) {
gbc.gridx = 0;
for (int j = 0; j < button[i].length; j++) {
button[i][j] = new JButton(i + " " + j) {
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
};
add(button[i][j], gbc);
gbc.gridx++;
}
gbc.gridy++;
}
}
}
}
Beware when playing with getPreferredSize
, fonts are generally not rendered the same on all platforms and this will affect what you program looks look on different platforms. In you case, chaning the margins
of the button might be safer
Upvotes: 1