I want to create a little Java game using Netbeans. At the moment I have a JFrame and two JPanels.
JFrame contains both JPanels and a button. I intend to click this button and resize one of the JPanels (from 0 to> 0).
So far I have managed to resize the frame, but I cannot figure out how to resize the JPanel.
This is what I have done so far:
Structure frame |_ panel 1 |_ panel 2 |_ button __________________ | _ _ | | | | | | _| | | | | | | | | | | | | |>| | | | | | |_| | |_| |_| | |__________________| on click should expand frame and panel ______________________ | _ _____ | | | | | | _| | | | | | | | | | | | | |>| -> | | | | | |_| | |_| |_____| | |______________________|
This is a JPanel for resizing
public class ToResize extends javax.swing.JPanel { ... public void resize(int width) { this.setSize(new Dimension(this.getWidth() + width, this.getHeight())); } }
This is a jframe with a button
public class MyFrame extends javax.swing.JFrame { ... private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if (panelToResize.getWidth() == 0) { panelToResize.resize(100); } else { panelToResize.resize(-100); } validate(); } }
source share