我这里有一些代码,当按下按钮时,我正在尝试扩展 JPanel。但是,它仍然保持在以前的高度。有没有办法做到这一点,或者它是否固定在创建时设置的尺寸上?
public class GUITest extends JFrame {
JPanel jp;
JButton one;
public static void main(String[] args) {
new GUITest();
}
public GUITest() {
initWidgets();
}
public void initWidgets() {
setSize(250, 250);
setTitle("Stretch Panel Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
one = new JButton("Click me!");
ActionListener extend = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 80; i++) {
jp.setPreferredSize(new Dimension(200,(i+70)));
}
//System.out.println(jp.getHeight());
}
};
one.addActionListener(extend);
add(one, BorderLayout.NORTH);
jp = new JPanel();
jp.setBackground(Color.BLACK);
jp.setPreferredSize(new Dimension(200,70));
add(jp, BorderLayout.CENTER);
setVisible(true);
}
}