gpt4 book ai didi

java - BackgroundPanel 我的背景图像覆盖了我的所有组件

转载 作者:行者123 更新时间:2023-12-02 09:40:20 26 4
gpt4 key购买 nike

private JPanel contentPane;
// instance variables
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 350;

private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 150;

private static final int BUTTON_WIDTH = 90;
private static final int BUTTON_HEIGHT = 30;
private JButton readFile;
private JButton exit;
private JButton stats;
private JButton blank;

private JPanel action;
<小时/>
public Potion() {
// desativando logs
LogManager.getLogManager().reset();
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
//-----------------------------------------------
//background color
BufferedImage img = null;
try {
File f = new File("C:\\Users\\Gabriel\\Desktop\\mu.png");
img = ImageIO.read(f);
System.out.println("File " + f.toString());
} catch (Exception e) {
System.out.println("Cannot read file: " + e);
}

BackgroundPanel background = new BackgroundPanel(img, BackgroundPanel.TILED, 0.50f, 0.5f);
//fim
//-----------------------------------------------
JPanel contentPane = (JPanel) getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new MigLayout());

setContentPane(background);

//Set the frame properties
setSize (FRAME_WIDTH, FRAME_HEIGHT);
setResizable (false);
setTitle ("CSCE155A Course Offerings Viewer");
setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

//Create and Place the Buttons on the frame
readFile = new JButton("Read File");
readFile.setBounds(4, 285, BUTTON_WIDTH, BUTTON_HEIGHT);


exit = new JButton("Exit");
exit.setBounds(100, 285, BUTTON_WIDTH, BUTTON_HEIGHT);


stats = new JButton("Stats");
stats.setBounds(195, 285, BUTTON_WIDTH, BUTTON_HEIGHT);


blank = new JButton("Clear");
blank.setBounds(290, 285, BUTTON_WIDTH, BUTTON_HEIGHT);

action = new JPanel(new FlowLayout());
action.setBackground(Color.blue);
action.add(readFile);
action.add(exit);
action.add(stats);
action.add(blank);
contentPane.add(action);

}

我正在使用类:BackgroundPanel 将图像添加到我的框架

//背景颜色

BufferedImage img = null;
try {
File f = new File("C:\\Users\\Gabriel\\Desktop\\mu.png");
img = ImageIO.read(f);
System.out.println("File " + f.toString());
} catch (Exception e) {
System.out.println("Cannot read file: " + e);
}

BackgroundPanel background = new BackgroundPanel(img, BackgroundPanel.TILED, 0.50f, 0.5f);

但是当我要做的时候:

setContentPane(背景);

该图像覆盖了我所有其他面板:

action = new JPanel(new FlowLayout()); action.setBackground(Color.blue); action.add(readFile); action.add(exit); contentPane.add(action);

我的背景覆盖了我的操作面板和所有按钮,我无法解决这个问题

最佳答案

让我们看看你在做什么:

在这里创建您的BackgroundPanel对象:

BackgroundPanel background = new BackgroundPanel(img, BackgroundPanel.TILED, 0.50f, 0.5f);

在这里,您提取 JFrame 的当前 contentPane、交换之前的 contentPane

JPanel contentPane = (JPanel) getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new MigLayout());

现在您交换 contentPanes,使背景 JPanel 成为 contentPane 但 contentPane 变量仍然引用旧的 contentPane,即不再存在的显示

setContentPane(background);

//.... other code

现在创建一个新的 JPanel,向其中添加内容:

action = new JPanel(new FlowLayout());
action.setBackground(Color.blue);
action.add(readFile);
action.add(exit);
action.add(stats);
action.add(blank);

然后将其添加到 contentPane 变量不再显示

contentPane.add(action);

所以当然,你的组件不会被显示。相反,将操作 JPanel 添加到背景 JPanel

关于java - BackgroundPanel 我的背景图像覆盖了我的所有组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57133536/

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