我已将图像加载到 JLabel
中,但是当我将标签添加到容器中时,它会填充整个 JFrame
这很好,但其他 2 个按钮则不然转到图像顶部,当我单击按钮应该在的位置时,什么也没有发生,所以就像按钮不在那里一样,即使它是为了添加它们。您所能看到的只是图像。
下面是我的代码:
public void add(Container pane){
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
URL resource = getClass().getResource("Graphitebackground.v2.jpg");
ImageIcon i2 = new ImageIcon(resource);
JLabel label = new JLabel(i2);
label.setPreferredSize(new Dimension(1000,1000));
label.setVisible(true);
pane.add(label);
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
JLabel label2 = new JLabel("");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(label2, c);
JLabel Label3 = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(Label3, c);
JLabel Label4 = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(Label4, c);
JButton b1 = new JButton("Yes");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50; //make this component tall
c.weightx = 0.0;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 2;
pane.add(b1, c);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Command();
} catch (AWTException ex) {
} catch (InterruptedException ex) {
}
}
});
JButton b2= new JButton("No");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50; //make this component tall
c.weightx = 0.0;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 2;
pane.add(b2, c);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (Window window : Window.getWindows()) {
window.dispose();
}
}
});
JLabel button1 = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 1; //2 columns wide
c.gridy = 2;
//third row
pane.add(button1, c);
}
public void Start1(){
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
add(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
最佳答案
ImageIcon i2 = new ImageIcon(resource);
JLabel label = new JLabel(i2);
label.setPreferredSize(new Dimension(1000,1000));
label.setVisible(true);
pane.add(label);
...
pane.setLayout(new GridBagLayout());
对上述代码的评论:
不要设置首选尺寸。每个组件将根据组件的属性确定自己的首选大小。在这种情况下,首选尺寸将是图像的尺寸。
不需要 setVisible(true)。 Swing 组件默认是可见的(除了顶级容器,如 JFrame、JDialog...)。
在设置布局管理器之前,不要尝试向面板添加组件。它可能适用于某些布局管理器,但这是一个坏习惯,可能会导致问题。
when I add the label to the container it fills the whole JFrame which is good but the other 2 Buttons don't go on top of the image
Swing 实际上按照组件添加到面板的相反顺序绘制组件。因此,因为标签是第一个添加的,所以它是最后一个绘制的,并且它绘制在按钮的顶部。
问题是您需要更改组件的层次结构。应该是:
- 框架
- 背景图像(标签)
- 按钮 1
- 按钮 2
- 背景图像(标签)
所以你的逻辑应该是这样的:
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new GridBagLayout() );
background.add(button1, constraint...);
background.add(button2, constraint...);
frame.add(background);
请注意,JLabel 不是真正的容器,因此按钮必须适合图像的大小,否则将无法正确显示。
对于真正的背景图像,您应该在 JPanel 上绘制图像,然后将按钮添加到面板中。请参阅Background Panel有关此方法的示例。
关于Java JButton 不会遍历容器中的 JLabel(带有图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51907367/