[英]Adding JPanel to another JPanel in a different class
I'm trying to add a JPanel to another JPanel from another class. 我试图将一个JPanel从另一个类添加到另一个JPanel。 The program does not longer throw an error and all methods have been run, but the new panel just has a black screen.
该程序不再引发错误,并且所有方法都已运行,但是新面板只有一个黑屏。 A basic version of the program looks as follows:
该程序的基本版本如下所示:
package ninjadragon;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NinjaDragon extends JFrame implements ActionListener{
public JPanel panelMain;
public JPanel panelTurnBase;
public static void main(String[] args) {
NinjaDragon();
}
public static void NinjaDragon() {
NinjaDragon frame;
frame = new NinjaDragon();
frame.CreateMenuScreen();
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setSize(750, 750);
frame.show();
frame.setResizable(false);
frame.pack();
}
private void CreateMenuScreen() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container window = getContentPane();
panelMain =new JPanel();
panelMain.setPreferredSize(new Dimension(750,750));
panelMain.setBackground(Color.BLACK);
panelMain.setLayout (new FlowLayout());
window.add(panelMain);
PanelTop();
PanelButtons();
PanelIcon();
}
@Override
public void actionPerformed(ActionEvent event) {
Object eventSource = event.getSource();
if (eventSource == buttonStart) {
panelMain.removeAll();
TurnBase TB = new TurnBase();
TB.CreateTurnBase();
}
}
The other class looks something like this: 另一个类如下所示:
public void CreateTurnBase() {
panelMain=new JPanel();
panelTurnBase =new JPanel();
setLayout(new FlowLayout());
setPreferredSize(new Dimension(750,750));
setBackground(Color.BLUE);
panelTurnBase.setLayout (new FlowLayout());
panelMain.add(panelTurnBase);
System.out.println("1");
PanelTurnBaseTop();
PanelGameScreen();
PanelTurnBaseBottom();
repaint();
revalidate();
buttonAttack = new JButton("Attack");
buttonAttack.addActionListener(this);
panelTurnBase.add(buttonAttack);
System.out.println("2");
}
The reason the panel has "just a black screen" is because you dont add anything to it, and you tell it to have a black screen. 面板具有“仅黑屏”的原因是因为您没有向面板添加任何内容,而是告诉它具有黑屏。
ie 即
panel.setBackground(Color.BLACK);
You never actually do anything to that first panel inside of any of those methods, which I can assume based on your representation of your second "class" (it's a method). 您实际上从未对任何这些方法的第一个面板执行任何操作,我可以根据您对第二个“类”的表示(这是一个方法)来假设。 Hence why it stays black.
因此,为什么它保持黑色。
You say: 你说:
panelMain=new JPanel();
panelTurnBase =new JPanel();
You're creating new JPanel
s every time and just call them panelMain
and they just sit inside of that method, never leaving. 您每次都在创建新的
JPanel
,只需将它们panelMain
,它们就位于该方法内部,永不离开。 You either need to return a JPanel
or give it a JPanel
as an argument. 您需要返回一个
JPanel
或给它一个JPanel
作为参数。
The program is doing exactly what you tell it to do. 该程序完全按照您的指示执行。
Also, do not compare Objects like this: 另外, 不要比较喜欢这样的对象:
eventSource == buttonStart
You should use: 您应该使用:
eventSource.equals(buttonStart);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.