问题
I was just wondering how (and if) this is possible. I read somewhere about an ActionEvent with resize window(int, int) but I wasn't sure if that was an appropriate way to handle it.
Edit: sorry this is in an GUI interface set up by JPanel
回答1:
public static void main(String[] args) {
final JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("Click Me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setSize(200, 200);
}
});
frame.add(panel);
panel.add(button);
frame.pack();
frame.setVisible(true);
}
回答2:
To resize to the smallest possible size(but not minimized), use frame.pack(); for changing the size, use frame.setSize(, ); If you want to minimize, maximize, etc., setState() is supposed to work, but I'm not sure about it
来源:https://stackoverflow.com/questions/8966535/how-to-resize-window-in-java-from-a-click-in-this-case-a-button