- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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/
private JPanel contentPane; // instance variables private static final int FRAME_WIDTH = 400; privat
最近我发现了这个BackgroundPanel.java我想知道如何将它与 netbeans 一起使用,我创建了一个名为 classes 的包,其中包含该类。 在其他包(vistas)中,我有 jFr
我是一名优秀的程序员,十分优秀!