0

Im using NetBeans to do a work for school. There i have an huge JPanel that contains a huge JFrame. That JFrame as 5 small JFrames, 1 is the menu with buttons, the other ones are boxes with text that will swap when i choose in the buttons. When one box is showing the other ones are invisible im using the following code (dont know if it is the best):

     public ConversorUI() {
            initComponents();
           PanelVazio.setVisible(true);
           PanelTemp.setVisible(false);
           PanelComp.setVisible(false);
           PanelMoedas.setVisible(false);
           this.pack();
        }

My problem is, when i run my program i have a big space with nothing and only below it the components appear. I want them to appear in the top of my window. What can I do ?

ANSWER After some time searching i just realized i could Set Layout from JPanel to Card Layout and create JPanels over each other activating them with the code:

    private void DinheiroButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        //Remove Panels
        CAIXA.removeAll();
        CAIXA.repaint();
        CAIXA.revalidate();

        //Add Panels
        CAIXA.add(DinheiroBox);
        CAIXA.repaint();
        CAIXA.revalidate();
    }        
Marta Azoia
  • 25
  • 1
  • 5
  • 2
    For better help, please create and post a valid [mcve], a **small**, minimal in fact, program that we can compile and run unchanged, that has no outside dependencies (such as database or images) and that directly demonstrates your problem for us. Also consider posting a link to two images, one showing the desired GUI and the other showing the observed. – Hovercraft Full Of Eels May 29 '17 at 21:28
  • 1
    1) Besides the MCVE suggested by @HovercraftFullOfEels, provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 2) The best strategy to using a component that appears only occasionally is to place it and a blank panel into a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson May 29 '17 at 21:48
  • 2
    `There i have an huge JPanel that contains a huge JFrame.` - no you don't because you can't add a JFrame to a JPanel. `That JFrame as 5 small JFrames,` - you can't add a JFrame to a JFrame. So in addition to posting your [mcve] ask a proper question with proper terminology. Also, variable names should NOT start with an upper case character. Follow Java conventions when you post your code. – camickr May 29 '17 at 21:54

3 Answers3

0

You can load one jframe at e time Because every jframe you added have own place and visibility doesnt do anything to remove Try to save every jframe and for changing Delete old one and add new one

0

Looks like you are using Java Swing , right ?

Any way you do something wrong if you have JPanel that contains JFrames. To build correct UI you have to add JPanels inside JFrame.

Also, to reach correct component order and placing you need configure corresponded layout, here is description.

Andriy Rymar
  • 313
  • 3
  • 8
  • [Here](https://docs.oracle.com/javase/tutorial/uiswing/examples/start/) is example how to build correct UI representation on SWING. To implement switching between panels you may remove existed panel and add new one into container, otherwise you can use `.setVisible(false)` instead of remove and for other `.setVisible(true)` to show it. – Andriy Rymar May 29 '17 at 21:41
0

Why are you using multiple JFrames to do this? From what I can see it would be a better idea to use JPanels that can take care of the individual tasks, such as the menu and buttons etc.

I'm relatively new to using javax.swing myself, but from my knowledge you can only have 1 frame at a time per window (like the other person said).

From what i've been able to discern from your project, you possibly don't even need multiple panels. You just need one panel for the menu with buttons, and multiple Labels or JLabels that display text according to the button. You can use the setText method in writing your addActionListener, something like this:

    buttonName.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
             labelName.setText("blah blah blah");
             //and whatever else you may need to do
        }
    });
Emily Liu
  • 1
  • 1