简体   繁体   中英

How do I get my custom JPanel to show in another class?

I am writing a small program just for personal use to double check strategies on a mobile based game. I created a Board class and frame and whatnot, then I created a new Panel class in my package (I use Eclipse). I figured it would be as simple as creating the two and implementing the Panel and adding it to the main frame of the Board class. Alas, I was mistaken.

TubePanel - my secondary panel

package ballSort;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.JLabel;

public class TubePanel extends JPanel {
    
    /**
     * Create the panel.
     */
    public TubePanel() {
        setBackground(new Color(47, 79, 79));
        setBounds(10, 10, 30, 40);
    }
}

**This is an excerpt from my main Board class.... **

    panel_1 = new JPanel();
    panel_1.setBounds(118, 164, 726, 466);
    frame.getContentPane().add(panel_1);

    JButton btnNewButton_1 = new JButton("X");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    });
    btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 89));
    btnNewButton_1.setBounds(10, 122, 85, 124);
    frame.getContentPane().add(btnNewButton_1);
}
    
public void Create(int tubes) {
    TubePanel tube = new TubePanel();
    panel_1.add(tube);

    System.out.println(tube);
    System.out.println(panel_1);
}

Am I the idiot here? This should've worked, as far as I'm concerned, but clearly I know not the ways of the Java. [Only been coding java for 4 years.] Anyway the problem is that it doesn't show the tubePanel . It doesn't give any errors either so I had to use Sysout to see if there was anything wrong with it and panel_1 for a comparison...

ballSort.TubePanel[,10,10,30x40,**invalid**,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
javax.swing.JPanel[,118,164,726x466,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

Aside from the invalid (to which I tried to follow it to its root, I couldn't figure it out) there's nothing different from panel_1 compared to tube .

Any help is much appreciated.

Refer to the lesson entitled Laying Out Components Within a Container from trail Creating a GUI With JFC/Swing which is part of Oracle's Java tutorials.

The default layout of the content pane of a JFrame is BorderLayout . When you add a component to the content pane, it gets added to the center area of the BorderLayout . The center area (and all other areas) of BorderLayout can only contain a single component. So when you add two components to the center area, only one will be displayed. Refer to method add(Component, Object) in class java.awt.Container

So study the layout managers and decide which is suitable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM