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);
}
}
}