[英]how can i increase/decrease size of window on click event?
我正在开发一个简单的摇摆应用程序,其中我有一个带有三个按钮的主窗口。 当我点击第一个按钮时,它会打开(200,200)尺寸的新窗口。 当我点击第二个按钮时,新打开的窗口的高度应该增加,当我点击第三个按钮的高度应该减少。 你能帮我解决代码吗?
提前致谢。
您可以在要打开的新打开的窗口上执行以下操作:
JFrame fr=getNewlyOpenendWindowReference(); // get a reference to the JFrame
fr.setSize(fr.getSize().getWidth() + 10,fr.getSize().getHeight() + 10);
fr.repaint();
这应该会增加JFrame长度和宽度方向的大小每个调用10个像素。
创建一个Controller类来处理动作事件。
定义一个FramePanel extends JPanel
并向其添加按钮。 使用操作事件值在类中设置常量,并在按钮上设置它们。 然后,您可以实例化此FrameController
并使用JButton.addActionListener()
将其作为侦听器添加到这些按钮。 或者,您可以在FrameController
类的构造函数中执行此FrameController
。
public class FrameController implements ActionListener { private JFrame openedFrame; public static final int MINIMUM_HEIGHT = 200; public FrameController(FramePanel panel) { this.panel.getOpenFrameButton().addActionListener(this); this.panel.getIncreaseHeightButton().addActionListener(this); this.panel.getDecreaseHeightButton().addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { String action = e.getActionCommand(); if (action.equals(FramePanel.ACTION_OPEN_FRAME)) { this.openedFrame = new JFrame(); // set it up how you want it } else if (action.equals(FramePanel.ACTION_INCREASE_HEIGHT)) { this.openedFrame.setSize(this.openedFrame.getWidth(), this.openedFrame.getHeight() + 10); } else if (action.equals(FramePanel.ACTION_INCREASE_HEIGHT)) { int newHeight = this.openedFrame.getHeight() - 10; if (newHeight < FrameController.MINIMUM_HEIGHT) newHeight = FrameController.MINIMUM_HEIGHT; this.openedFrame.setSize(this.openedFrame.getWidth(), newHeight); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.