I am making a basic calculator for a school project, and one of the requirements I need to meet is to change the size of a JButton inside of a JPanel. I have tried to do this, but it doesn't change.
I have tried to use both setSize() and setPreferredSize() to change it, but neither of those methods work. I have also tried to set its size without adding it to the JPanels (explained in the code) and just adding it to the frame and that doesn't work either.
For online help, I have looked at this question here on Stack Overflow, and I (somewhat) copied it's code but it doesn't work.
Can someone tell me what I am doing wrong?
Code
Imports
import java.awt.GridLayout ;
import java.awt.Dimension ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import javax.swing.JFrame ;
import javax.swing.JPanel ;
import javax.swing.JButton ;
import javax.swing.BoxLayout ;
Class
public class Main {
public JFrame frame = new JFrame("Calculator") ;
public JButton one = new JButton("1") ;
public JPanel mainPanel = new JPanel() ;
public JPanel buttonsPanel = new JPanel() ;
public static void main(String[] args) {
Main main = new Main() ;
main.one.setPreferredSize(new Dimension(500, 500)) ;
main.one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Take that fat L") ;
}
}) ;
main.buttonsPanel.setLayout(new GridLayout(5, 3)) ;
main.buttonsPanel.setMaximumSize(new Dimension(2500, 1500)) ;
main.buttonsPanel.add(main.one) ;
main.mainPanel.setLayout(new BoxLayout(main.mainPanel, BoxLayout.Y_AXIS)) ;
main.mainPanel.add(main.buttonsPanel) ;
main.frame.setContentPane(main.mainPanel) ;
main.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
main.frame.setSize(500, 700) ;
main.frame.setLocationRelativeTo(null) ;
main.frame.setVisible(true) ;
}
}
The combination of
setPreferredSize,setMinimumSizeandsetSize(on the window) are working against you.I got rid of
buttonsPanel.setMaximumSize(new Dimension(2500, 1500));and replacedframe.setSize(500, 700)withframe.pack()and this seems to have fixed the immediate problem.However, this leaves you with another problem.
setPreferredSizeis overriding the internal logic used by the button to calculate it's preferred size based on it's internal properties.This might not seem like a big issue, but when you have users with unusual font settings, it quickly comes back to haunt you.
Swing has a good layout management system and you should spend some time trying to work out how to make use of it.
For example, if we start with something like...
Now, this is making use of
GridBagLayout, not the simplest of layouts, but defiantly one of the most flexible. The reason for choosing it is it allows the possibility of applying an "internal" padding to the components, in this example, adding50to both the width and height.This will generate something like...
Okay, big deal you say. But consider what happens when the font size changes (in this example I manually modify the font size, but it could also come from system settings/preferences)
Which outputs something like...
And, without any modification to the original code (other then adding in the font sizing), the code just works.
This is why we generally discourage the use of
setPreferred/Minimum/MaximumSizeNow, if you "really" want square buttons, I recommend having a look at Creating Square Buttons in Swing. You could also try something like this, but to be honestly, I prefer the first approach.