Reputation: 261
I am new in swing and trying to set the same size for buttons. However, I didn't find the exact solution on the internet. Please note that I must use setPreferredSize(). Dimension is not the correct solution for now. I want to get the size of calcButton and use in setPreferredSize().
Here is the picture:
and the code:
import javax.swing.*;
/**
The KiloConverter class displays a JFrame that
lets the user enter a distance in kilometers. When
the Calculate button is clicked, a dialog box is
displayed with the distance converted to miles.
*/
public class KiloConverter extends JFrame
{
private JPanel panel; // To reference a panel
private JLabel messageLabel; // To reference a label
private JTextField kiloTextField; // To reference a text field
private JButton calcButton; // To reference a button
private JButton alertButton;
private final int WINDOW_WIDTH = 310; // Window width
private final int WINDOW_HEIGHT = 100; // Window height
/**
Constructor
*/
public KiloConverter()
{
// Set the window title.
setTitle("Kilometer Converter");
// Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Specify what happens when the close button is clicked.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Build the panel and add it to the frame.
buildPanel();
// Add the panel to the frame's content pane.
add(panel);
// Display the window.
setVisible(true);
}
/**
The buildPanel method adds a label, text field, and
and a button to a panel.
*/
private void buildPanel()
{
// Create a label to display instructions.
messageLabel = new JLabel("Enter a distance " +
"in kilometers");
// Create a text field 10 characters wide.
kiloTextField = new JTextField(10);
// Create a button with the caption "Calculate".
calcButton = new JButton("Calculate");
alertButton = new JButton("Alert");
// --------ERROR HERE----------
alertButton.setPreferredSize(new getPreferredSize(calcButton));
// Create a JPanel object and let the panel
// field reference it.
panel = new JPanel();
// Add the label, text field, and button
// components to the panel.
panel.add(messageLabel);
panel.add(kiloTextField);
panel.add(calcButton);
panel.add(alertButton);
}
/**
main method
*/
public static void main(String[] args)
{
new KiloConverter();
}
}
Upvotes: 3
Views: 1648
Reputation: 285405
Yours is an XY Problem where you ask, "how do I do 'X'?" when the better solution is not to do 'X' at all, but rather to do 'Y'.
Here you ask how to set the preferred size of a button in order to make the button sizes equal, when the better and in fact canonically correct solution is not to do this, but rather to use a better layout manager, one that sizes the buttons the same for you -- a GridLayout.
For example, create a new JPanel to hold the buttons, say called buttonPanel, give it a GridLayout, say new GridLayout(1, 0, 3, 0)
, then add the buttons to it, and then add this JPanel to the main JPanel.
e.g.,
import java.awt.GridLayout;
import javax.swing.*;
@SuppressWarnings("serial")
public class LayoutEg extends JPanel {
private static final int KILO_FIELD_COLS = 15;
private static final int GAP = 3;
private JTextField kiloTextField = new JTextField(KILO_FIELD_COLS);
private JButton calcButton = new JButton("Calculate");
private JButton alertButton = new JButton("Alert");
public LayoutEg() {
// add ActionListeners etc....
JPanel enterPanel = new JPanel();
enterPanel.add(new JLabel("Enter a distance in kilometers:"));
enterPanel.add(kiloTextField);
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
buttonPanel.add(calcButton);
buttonPanel.add(alertButton);
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new GridLayout(0, 1));
add(enterPanel);
add(buttonPanel);
}
private static void createAndShowGui() {
LayoutEg mainPanel = new LayoutEg();
JFrame frame = new JFrame("Kilometer Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
Explanation of the code:
Constant for the number of columns in the kiloTextField
private static final int KILO_FIELD_COLS = 15;
Constant for the gap between your JButtons and for the gap around the main JPanel (the empty border)
private static final int GAP = 3;
JTextField that is 15 columns wide
private JTextField kiloTextField = new JTextField(KILO_FIELD_COLS);
Top JPanel that holds a JLabel and a JTextField. It uses FlowLayout by default:
JPanel enterPanel = new JPanel();
enterPanel.add(new JLabel("Enter a distance in kilometers:"));
enterPanel.add(kiloTextField);
Here is the meat of the issue, create a JPanel that uses a GridLayout(1, 0, 3, 0)
, that is a grid layout with 1 row, with a variable number of columns (the 0 second parameter) that has a 3 horizontal gap and no vertical gap. Then add our buttons to it:
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
buttonPanel.add(calcButton);
buttonPanel.add(alertButton);
Put a border around this main JPanel that is 3 pixels wide
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
Give the main JPanel a GridLayout that has variable number of rows (0) and 1 column, and add both the enterPanel and the buttonPanel to it:
setLayout(new GridLayout(0, 1));
add(enterPanel);
add(buttonPanel);
More explanation in comments, especially the key method, .pack()
:
// create the main JPanel
LayoutEg mainPanel = new LayoutEg();
// create a JFrame to put it in, although better to put into a JDialog
JFrame frame = new JFrame("Kilometer Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add the LayoutEg JPanel into the JFrame
frame.getContentPane().add(mainPanel);
// **** key method*** that tells the layout managers to work
frame.pack();
// center and display
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Upvotes: 4