Resizing of JFrame while the font on buttons increases
Hey,
I would like to ask you how to resize my JFrame ,when the size of buttons put in this frame increases.
I mean the font on the buttons increases by 1 point every time I press the button. My goal is to make the two buttons visible all the time. Therefore I need to resize my JFrame. I tried in the following method, but it did not work :(
(cp is contentPane() of my JFrame - frame)
public void actionPerformed(ActionEvent e){
Component c = (Component) e.getSource();
Integer nazwa= c.getFont().getSize()+1;
int width = (int) cp.getPreferredSize().getWidth();
if(cp.getPreferredSize().getWidth()>cp.getWidth()) {
cp.setSize(new Dimension(width, cp.getHeight()));
cp.validate();
}
int height = (int) cp.getPreferredSize().getHeight();
if(cp.getPreferredSize().getHeight()>cp.getHeight()) {
cp.setSize(newDimension(cp.getWidth(),height));
cp.validate();
}
c.setFont( new Font(c.getName(), c.getFont().getStyle(), nazwa));
frame.validate();
}
[1074 byte] By [
Pabloooa] at [2007-11-27 4:53:47]

# 2
pack() does not help, as I have already used it.
Pls find below the whole code of the program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GUI implements ActionListener{
final int BNUM = 2;
static JFrame frame = new JFrame()
static Container cp = frame.getContentPane();
GUI() {
cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
for (int i = 1; i <= BNUM; i++) {
JButton b = new JButton("Przycisk " + i);
b.addActionListener(this);
cp.add(b);
}
frame.pack();
frame.show();
}
public void actionPerformed(ActionEvent e){
Component c = (Component) e.getSource();
Integer nazwa= c.getFont().getSize()+1;
int width = (int) Integer.parseInt(cp.getPreferredSize().getWidth().toString());
if(cp.getPreferredSize().getWidth()>cp.getWidth())
cp.setSize(new Dimension(width, cp.getHeight()));
if(cp.getPreferredSize().getHeight()>cp.getHeight()) {
cp.setSize(new Dimension(cp.getWidth(),cp.getPreferredSize().getHeight()));
cp.validate();
c.setFont( new Font(c.getName(), c.getFont().getStyle(), nazwa));
}
public static void main(String[] a) {
//cp.setPreferredSize(new Dimension(200,200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new GUI(); }
}
# 5
validate() and pack() do not do the same thing.
validate() wil recalculate the preferedSize of a component, and lay out the child components in the component, but if the frame isn't large enough to display the entire component then the component will need to be truncated in some form.
pack() will recalulate the preferred size of the components on the frame and then resize the frame to that every component is displayed at its preferred size.