How can I increase / decrease the size of the window on click?

I am developing a simple swing application in which I have a main window with three buttons. When I click on the first button, a new window opens (200,200). When I click on the second button, the newly opened window height should increase, and when I click on the third, the button height should decrease. Can you help me with the code ....

early.

+4
source share
2 answers

You can do the following in recently opened windows that you want to change:

JFrame fr=getNewlyOpenendWindowReference(); // get a reference to the JFrame
fr.setSize(fr.getSize().getWidth() + 10,fr.getSize().getHeight() + 10);
fr.repaint();

this should increase the size of the JFrame and the width by 10 pixels per call.

+3
source

Create a Controller class to handle action events.

FramePanel extends JPanel . . FrameController JButton.addActionListener(). 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);
    }
  }
}
0

All Articles