B
burrise
Can someone explain why statements A and B below are not equivalent?
When you compile and run the program the first button is at its
preferred size and the second is resized to fill the grid cell. It's as
if the second button is added without the JPanel around it. I'm really
confused. They look like equivalent ways of doing the same thing.
Eddie
import java.awt.*;
import javax.swing.*;
public class test extends JFrame {
public test() {
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(1,2));
/* A */ JPanel tempPanel = new JPanel();
tempPanel. add(new JButton("First"));
add(tempPanel);
/* B */ add((new JPanel()).add(new JButton("Second")));
}
public static void main(String[] args) {
JFrame frame = new test();
frame.pack();
frame.setVisible(true);
}
}
When you compile and run the program the first button is at its
preferred size and the second is resized to fill the grid cell. It's as
if the second button is added without the JPanel around it. I'm really
confused. They look like equivalent ways of doing the same thing.
Eddie
import java.awt.*;
import javax.swing.*;
public class test extends JFrame {
public test() {
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(1,2));
/* A */ JPanel tempPanel = new JPanel();
tempPanel. add(new JButton("First"));
add(tempPanel);
/* B */ add((new JPanel()).add(new JButton("Second")));
}
public static void main(String[] args) {
JFrame frame = new test();
frame.pack();
frame.setVisible(true);
}
}