Java 将JPanel构造函数添加到JPanel会显示为灰色
对Java(和一般编程)非常陌生 我正在尝试设置此布局,并尝试基于以下开关构建JPanel:Java 将JPanel构造函数添加到JPanel会显示为灰色,java,swing,jpanel,Java,Swing,Jpanel,对Java(和一般编程)非常陌生 我正在尝试设置此布局,并尝试基于以下开关构建JPanel: public class PrimaryDisplay extends JPanel { private static final long serialVersionUID = 1L; private int option = 0; public int getOption() { return option; } public void setOption(int option) {
public class PrimaryDisplay extends JPanel {
private static final long serialVersionUID = 1L;
private int option = 0;
public int getOption() {
return option;
}
public void setOption(int option) {
this.option = option;
}
public PrimaryDisplay(int primaryChoice) {
setOption(primaryChoice);
switch (getOption()) {
case 1: //fixate
//JPanel primaryScreen = new JPanel();
//primaryScreen.setBackground(Color.BLUE);
//primaryScreen.setLayout(new GridLayout(1,1));
JLabel fixate = new JLabel();
fixate.setText("+");
fixate.setFont(new Font("Verdana", 1, 50));
fixate.setForeground(Color.WHITE);
fixate.setHorizontalAlignment(SwingConstants.CENTER);
fixate.setVerticalAlignment(SwingConstants.CENTER);
//.add(fixate);
break;
case 2: //
}
}
}
并从主节点调用该构造函数:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static JFrame fullFrame = new JFrame();
public static JPanel primaryContainer = new JPanel();
public static JPanel hmdContainer = new JPanel();
static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
static int width = (int) screenSize.getWidth();
static int height = (int) screenSize.getHeight();
public static int primaryChoice = 1;
public static void main(String[] args) {
fullFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
fullFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fullFrame.setUndecorated(true);
fullFrame.setLayout(new GridBagLayout());
primaryContainer.setLayout(new GridLayout(2,1));
primaryContainer.setBackground(Color.YELLOW);
hmdContainer.setBackground(Color.GREEN);
hmdContainer.setLayout(new GridLayout(3,1));
JPanel filler = new JPanel();
filler.setBackground(Color.RED);
primaryContainer.add(filler);
PrimaryDisplay prim = new PrimaryDisplay(primaryChoice);
primaryContainer.add(prim);
HMD hmdDisplay = new HMD();
hmdDisplay.setBackground(Color.ORANGE);
hmdContainer.add(hmdDisplay);
GridBagConstraints m = new GridBagConstraints();
m.fill = GridBagConstraints.BOTH;
m.gridwidth = 1;
m.gridheight = 1;
m.weightx = .7;
m.weighty = 1;
m.gridx = 0;
m.gridy = 0;
fullFrame.add(primaryContainer, m);
m.gridy = 0;
m.gridx = 1;
m.weightx = .3;
fullFrame.add(hmdContainer, m);
fullFrame.setVisible(true);
}
}
它只会显示为一个灰色的盒子。。。。?
可能是一个非常简单的解决方案,但我现在被难住了
谢谢。我“相信”这就是你想要实现的
“+”面板呈灰色的主要原因是,默认情况下,JPanel
是不透明的,因此它覆盖了它后面的任何内容
如果将PrimaryDisplay
中的opaque
属性更改为false
,它将变为透明,以允许父容器通过
public PrimaryDisplay(int primaryChoice) {
setOpaque(false);
首先确保在EDT上下文中启动应用程序,有关更多详细信息,请参阅:
/.add(fixate)代码>深思熟虑?“它只会显示为灰色框…”-当我运行你的代码时,我会得到一个红色和绿色框以及一个显示+
的灰色框(如果我取消注释/.add(fixate);
),你是说+
面板是灰色的吗?是的,我想显示+面板,但是它是灰色的。这些行是否被注释掉似乎没有什么区别。(顺便说一句,应该是primaryScreen.add(fixate);)啊哈。有道理。当我添加那条线时,我看到的是没有+的黄色背景,我只是在猜测你想添加什么;)好啊因此,当我扩展jpanel时,我可以直接用add.xxx添加到它,而不必添加额外的jpanel,我不知道我在想什么。感谢您一直以来的支持,JPanel
只是另一个容器
;)