How to resize window in Java from a click (in this case, a button)?

二次信任 提交于 2019-12-12 21:34:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!