我得到了这个代码:
JFrame frame = new JFrame("FilesComparator");
frame.setVisible(true);
frame.setSize(1000, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height
/ 2 - frame.getSize().height / 2);
JButton buttonStart;
JButton buttonCancel;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
buttonStart = new JButton("Start");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
buttonStart.addActionListener(new ActionStart());
pane.add(buttonStart, c);
buttonCancel = new JButton("Cancel");
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 15.0;
c.anchor = GridBagConstraints.PAGE_END;
buttonCancel.addActionListener(new ActionCancel());
pane.add(buttonCancel, c);
}}
一切正常。但是当我想添加图像背景(image.jpeg)时它永远不会工作。我想这是因为这个布局。所以我的问题是,如何为我的框架设置背景图像?
感谢帮助
最佳答案
But when i want to add an image background ( image.jpeg) its never work
我没有看到您尝试添加背景图像的任何代码。
参见Background Panel您可以根据自己的要求使用几种方法。
关于java - 在 GridBagConstraints 容器上设置背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19184508/