What is wrong with this?

what is wrong with this code, it doesn’t show me the buttons…:

Code:
package Game; import com.jme3.audio.AudioNode; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ItemEvent; import java.awt.event.ActionListener; import java.awt.event.ItemListener; import javax.swing.*; import Game.Options; class MainGUI{ JFrame frame = new JFrame("My game"); JPanel buttons; JPanel cards; JButton button1; JButton button2; JButton button3; Action l = new Action(); Options Options = new Options(); public void Run(){ javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() { start(); } }); } public void start(){ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MainGUI mg = new MainGUI(); mg.addComponentToPane(frame.getContentPane()); frame.setLayout(null); frame.setSize(1680, 1050); frame.setVisible(true); } public JPanel getCards(){ return cards; } private void addComponentToPane(Container pane){ buttons = new JPanel(); button1 =new JButton("Play"); button2 = new JButton("Options"); button3 = new JButton("Credits"); buttons.add(button1); buttons.add(button2); buttons.add(button3); button1.setBounds(540,525,600,75); button1.setFont(new Font("Prestige Elite Std", Font.PLAIN, 36)); button2.setBounds(540, 625,600, 75 ); button2.setFont(new Font("Prestige Elite Std", Font.PLAIN, 36)); button3.setBounds(540, 725, 600, 75); button3.setFont(new Font("Prestige Elite Std", Font.PLAIN, 36)); buttons.setLayout(null); button1.addActionListener(l); button2.addActionListener(l); button3.addActionListener(l); cards = new JPanel(new CardLayout()); cards.add(buttons, "Main Menu"); pane.add(cards); } private class Action implements ActionListener{ public void actionPerformed(ActionEvent e){ CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, "Main Menu"); if(e.getSource() == button1){ JOptionPane.showMessageDialog(null, "Not Ready"); } if(e.getSource() == button3){ } if(e.getSource() == button2){ cl.last(cards); } } } }

http://www.oracle.com/us/sun/index.htm xD.



Btw it looks your private void addComponentToPane(Container pane) method isn’t being used at all.

yes it is:

Code:
mg.addComponentToPane(frame.getContentPane());

set the card’s background panel to red so you can see if it’s being expanded in frame’s content pane.

i have a different question, if I have a JPanel from one class, how do i add it to a diffrent class? or show it from a different class?

create a getter method for that panel e.g:



[java]

public class Class1 {

private JPanel panel = JPanel();

public JPanel getPanel() {

return panel;

}

}



public class Class2 {

public class Class2() {

JFrame frame = new JFrame(“somehell”);

Class1 class1 = new Class1();

frame.add(class1.getPanel());

}

}

[/java]



www.javabeginner.com

this is the new code:

Code:
package Game; import com.jme3.audio.AudioNode; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ItemEvent; import java.awt.event.ActionListener; import java.awt.event.ItemListener; import javax.swing.*; import Game.Options; class MainGUI extends JFrame{ JFrame frame = new JFrame("My game"); JPanel buttons; JPanel cards; JButton button1; JButton button2; JButton button3; Action l = new Action(); Options Options = new Options(); public void start(){ buttons = new JPanel(); button1 =new JButton("Play"); button2 = new JButton("Options"); button3 = new JButton("Credits"); buttons.add(button1); buttons.add(button2); buttons.add(button3); button1.setBounds(540,525,600,75); button1.setFont(new Font("Prestige Elite Std", Font.PLAIN, 36)); button2.setBounds(540, 625,600, 75 ); button2.setFont(new Font("Prestige Elite Std", Font.PLAIN, 36)); button3.setBounds(540, 725, 600, 75); button3.setFont(new Font("Prestige Elite Std", Font.PLAIN, 36)); buttons.setLayout(null); button1.addActionListener(l); button2.addActionListener(l); button3.addActionListener(l); cards = new JPanel(new CardLayout()); cards.add(buttons, "Main Menu"); frame.add(cards); frame.setVisible(true); frame.setSize(1680, 1050); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
    public JPanel getCards(){
            return cards;
    }
    private class Action implements ActionListener{
            public void actionPerformed(ActionEvent e){
                CardLayout cl = (CardLayout)(cards.getLayout());
                cl.show(cards, null);
                    if(e.getSource() == button1){
                            JOptionPane.showMessageDialog(null, "Not Ready");
                    }
                    if(e.getSource() == button3){
            }
                    if(e.getSource() == button2){
                        cards.add(Options.getPanel());
                        cl.show(Options.getPanel(), null);
                    }
                 }   
            }
    }</div>

and the second class:
Code:
package Game; import java.awt.*; import javax.swing.*;

class Options extends JPanel{
JFrame frame2 = new JFrame("my game - Options");;
private JPanel option = new JPanel();;
JSlider v;
static int V_MIN = 0;
static int V_MID = 5;
static int V_MAX = 10;
JButton button4;
JButton button5;
JButton button6;

    public void selectThing(){
            button4 = new JButton(&quot;Volume&quot;);
            button5 = new JButton(&quot;Display&quot;);
            button6 = new JButton(&quot;Controls&quot;);
            option.add(button4);
            option.add(button5);
            option.add(button6);
            option.setLayout(new CardLayout());
            button4.setBounds(280, 550, 540, 75);
            button4.setFont(new Font(&quot;Prestige Elite Std&quot;, Font.PLAIN,  36));
            button5.setBounds(840, 550, 540, 75);
            button5.setFont(new Font(&quot;Prestige Elite Std&quot;, Font.PLAIN,  36));
            button6.setBounds(1400, 550, 540, 75);
            button6.setFont(new Font(&quot;Prestige Elite Std&quot;, Font.PLAIN,  36));
            option.setVisible(true);
            option.setLayout(null);
    }
    
    public JPanel getPanel(){
            return option;
    }

}


and i get this error:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string at java.awt.CardLayout.addLayoutComponent(CardLayout.java:190) at java.awt.Container.addImpl(Container.java:1074) at java.awt.Container.add(Container.java:365) at Game.MainGUI$Action.actionPerformed(MainGUI.java:59) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6288) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6053) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4651) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4481) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4481) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643) at java.awt.EventQueue.access$000(EventQueue.java:84) at java.awt.EventQueue$1.run(EventQueue.java:602) at java.awt.EventQueue$1.run(EventQueue.java:600) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) at java.awt.EventQueue$2.run(EventQueue.java:616) at java.awt.EventQueue$2.run(EventQueue.java:614) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:613) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

nvm i fixed it , but it doesn’t show me the buttons

Oh! You want to add the Options panel to the frame ok? then do not call that getter method, add it directly, because it’s a panel.



[java]

cards.add(options);

cl.show(options, null);

[/java]

nvm i fixed it , but it doesn’t show me the buttons

@glaucomardano said:
set the panel's background panel to red so you can see if it's being expanded in frame's content pane.


Debug it:
[java]
panel.setBackgroundColor(Color.red);
[/java]

its isn’t colored red, when i press the second button

. You said the buttons wasn’t being showed up. Btw try using the repaint() method.

there are 3 buttons in the main menu, and one of them is moving to the other panel that is located in the other class… and it still doesn’t work…

The other class panel is being showed up properly? Change it’s background color so you can see if it is showed up. About the button, make sure you are removing that from its parent before adding that to another one. And use the repaint() to repaint the buttons. And use pack() again.

can you write that in code? i cant understand it like that ><""" and what do you mean by: " The other class panel is being showed up properly? "?

I meant if it’s displayed. About the method that I said were theses:



[java]

myPanel.remove(myButton);

myOtherPanel.add(myButton);

myOtherPanel.repaint();

myFrame.pack();

[/java]

here is the code of the other panel: [java]package Game;

import java.awt.;

import javax.swing.
;





class Options{

JFrame frame2 = new JFrame(“my game - Options”);;

private JPanel option = new JPanel();

JSlider v;

static int V_MIN = 0;

static int V_MID = 5;

static int V_MAX = 10;

JButton button4;

JButton button5;

JButton button6;



public void selectThing(){

button4 = new JButton(“Volume”);

button5 = new JButton(“Display”);

button6 = new JButton(“Controls”);

option.add(button4);

option.add(button5);

option.add(button6);

option.setLayout(null);

button4.setBounds(280, 550, 540, 75);

button4.setFont(new Font(“Prestige Elite Std”, Font.PLAIN, 36));

button5.setBounds(840, 550, 540, 75);

button5.setFont(new Font(“Prestige Elite Std”, Font.PLAIN, 36));

button6.setBounds(1400, 550, 540, 75);

button6.setFont(new Font(“Prestige Elite Std”, Font.PLAIN, 36));

option.setVisible(true);

option.setBackground(Color.red);

option.repaint();

}



public JPanel getPanel(){

return option;

}

}

[/java]

there aren’t shared buttons, each panel has it’s own set of buttons.

the repaint() method is only needed when you add a button to a panel after that panel’s been added to a parent. What “doesn’t work” like you said? Is it just not displayed? Again, make sure you remove the button from its parent before adding to another one. And after adding it to another parent, call the repaint() method for that parent.

it is just not displaying the buttons… :confused: