Reputation: 41
I am developing a simple swing app in which I have a main window with three buttons. When I click on the first button it opens new window of (200,200) dimension. When I click on the second button, the newly opened window's height should get increased and when I click on third button height should get decreased. Can you help me with code....
thanks in advance.
Upvotes: 4
Views: 1310
Reputation: 33082
Create a Controller class to handle the action events.
Define a FramePanel extends JPanel
and add your buttons to it. Set up constants in the class with action event values and set them on your buttons. Then, you can instantiate this FrameController
and add it as the listener to those buttons using JButton.addActionListener()
. Or, you can do this in the constructor of the FrameController
class.
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); } } }
Upvotes: 0
Reputation: 17761
you could do the following on the newly opened windows which you want to resize:
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 length and widthwise by 10 pixels per call.
Upvotes: 3