gpt4 book ai didi

java - 将 JPanel 构造函数添加到 JPanel 显示灰色

转载 作者:行者123 更新时间:2023-12-02 05:39:36 24 4
gpt4 key购买 nike

对 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) {
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: //

}
}
}

并从 main 调用该构造函数:

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);

}

}

它只显示为灰色框......?可能非常简单的解决方案,但我现在很困惑。

谢谢。

最佳答案

我“相信”这就是您想要实现的目标......

Boxes

“+”面板呈灰色的主要原因是,默认情况下 JPanel 是不透明的,因此它覆盖了其后面的所有内容...

如果您将PrimaryDisplay中的opaque属性更改为false,它将变得透明,允许父容器透过...

public PrimaryDisplay(int primaryChoice) {
setOpaque(false);

关于java - 将 JPanel 构造函数添加到 JPanel 显示灰色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24622179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com