JPanel 中的 Jbuttons,右侧有背景图片 |底角
Jbuttons inside JPanel with background image in right | bottom corner
有人可以解释一下如何在 jLabel 中放置几个 jButton,这些 jButton 的背景图像类似于 this 图片吗?主 jFrame 未修饰并设置为全屏。
我看到了很多不同的例子,比如
this or like this,但这些示例仅显示 jPanel 中的单个按钮。
这些例子确实够用了,我觉得你应该多学学swing。
现在,你可以简单地做:
JFrame frame = new JFrame("Hi there");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
frame.add(b1);
frame.add(b2);
b1.setBounds(60, 60, 40, 40);
b2.setBounds(10, 10, 40, 40);
frame.setVisible(true); //in case, add frame.setLayout(null);
您当然可以将按钮添加到 JPanel 而不是 JFrame
就个人而言,我会避免为此目的使用 JLabel
,它不会根据内容计算所需的大小,而是根据 icon
和 text
属性.
这可能是好事也可能是坏事,但如果您没有意识到,它可能会出其不意。
相反,我会使用自定义 JPanel
,它允许您定义调整大小和填充规则等内容,for example and for example
现在,一旦您了解了这些内容,您就需要创建一个按钮面板。我更喜欢创建一个专用的 class,因为它更容易隔离功能和管理,但我就是这样......
public class ButtonPane extends JPanel {
public ButtonPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(8, 8, 8, 8));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JButton("Button 1"), gbc);
add(new JButton("Button 2"), gbc);
add(new JButton("Button 3"), gbc);
}
}
接下来,您需要将此面板添加到背景中
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(backgroundPane);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gbc.insets = new Insets(30, 30, 30, 30);
ButtonPane buttonPane = new ButtonPane();
frame.add(buttonPane, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
可以生成类似...
查看 Laying Out Components Within a Container and How to Use GridBagLayout 了解更多详情
有人可以解释一下如何在 jLabel 中放置几个 jButton,这些 jButton 的背景图像类似于 this 图片吗?主 jFrame 未修饰并设置为全屏。
我看到了很多不同的例子,比如 this or like this,但这些示例仅显示 jPanel 中的单个按钮。
这些例子确实够用了,我觉得你应该多学学swing。 现在,你可以简单地做:
JFrame frame = new JFrame("Hi there");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
frame.add(b1);
frame.add(b2);
b1.setBounds(60, 60, 40, 40);
b2.setBounds(10, 10, 40, 40);
frame.setVisible(true); //in case, add frame.setLayout(null);
您当然可以将按钮添加到 JPanel 而不是 JFrame
就个人而言,我会避免为此目的使用 JLabel
,它不会根据内容计算所需的大小,而是根据 icon
和 text
属性.
这可能是好事也可能是坏事,但如果您没有意识到,它可能会出其不意。
相反,我会使用自定义 JPanel
,它允许您定义调整大小和填充规则等内容,for example and for example
现在,一旦您了解了这些内容,您就需要创建一个按钮面板。我更喜欢创建一个专用的 class,因为它更容易隔离功能和管理,但我就是这样......
public class ButtonPane extends JPanel {
public ButtonPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(8, 8, 8, 8));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JButton("Button 1"), gbc);
add(new JButton("Button 2"), gbc);
add(new JButton("Button 3"), gbc);
}
}
接下来,您需要将此面板添加到背景中
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(backgroundPane);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gbc.insets = new Insets(30, 30, 30, 30);
ButtonPane buttonPane = new ButtonPane();
frame.add(buttonPane, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
可以生成类似...
查看 Laying Out Components Within a Container and How to Use GridBagLayout 了解更多详情