英文:
How to set position to JComponent in BoxLayout?
问题
我想使用2个JPanel
,分别命名为panel
和panel_1
。
我想使用JLabel
自动向panel
添加图像,
并且也想将JButton
添加到panel_1
。
如何根据位于按钮上方的图像调整按钮的大小?
public class Testing extends JFrame {
public Testing() {
this.setSize(590, 327);
this.setTitle("JFrame");
getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(118, 136, 321, 89);
getContentPane().add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JLabel lblImage = new JLabel("image for button1");
panel.add(lblImage);
JLabel lblImage_1 = new JLabel("image for button2");
panel.add(lblImage_1);
JLabel lblImage_2 = new JLabel("image for button3");
panel.add(lblImage_2);
JPanel panel_1 = new JPanel();
panel_1.setBounds(118, 30, 321, 77);
getContentPane().add(panel_1);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
JButton btnNewButton = new JButton("New button 1");
panel_1.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button 2");
panel_1.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button 3");
panel_1.add(btnNewButton_2);
}
public static void main(String[] args) throws Exception {
Testing frame = new Testing();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
英文:
I want to use 2 JPanel
as panel
and panel_1
.
I want to add image automatically to panel using JLabel
and also add JButton
to panel_1
.
How can I resize button according to the image which is above the button?
public class Testing extends JFrame {
public Testing() {
this.setSize(590, 327);
this.setTitle("JFrame");
getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(118, 136, 321, 89);
getContentPane().add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JLabel lblImage = new JLabel("image for button1");
panel.add(lblImage);
JLabel lblImage_1 = new JLabel("image for button2");
panel.add(lblImage_1);
JLabel lblImage_2 = new JLabel("image for button3");
panel.add(lblImage_2);
JPanel panel_1 = new JPanel();
panel_1.setBounds(118, 30, 321, 77);
getContentPane().add(panel_1);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
JButton btnNewButton = new JButton("New button 1");
panel_1.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button 2");
panel_1.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button 3");
panel_1.add(btnNewButton_2);
}
public static void main(String[] args) throws Exception {
Testing frame = new Testing();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
答案1
得分: 3
如果您的目标是使按钮位于图像上方,并且使按钮的宽度随图像展开,那么:
- 摒弃使用空布局和
.setBounds(...)
(这只是良好的一般建议) - 将带有图像的 JLabel 放入使用 BorderLayout 的 JPanel 中,将 JLabel 放在 BorderLayout.CENTER 位置
- 将按钮放在相同的 JPanel 中,使用 BorderLayout.PAGE_START 位置将其置于 JLabel 的上方。
- 然后将该 JPanel 放置在 GUI 中需要的位置,嵌套使用各自的布局管理器的 JPanel。
BorderLayout 将允许中心组件填充该位置,并且会展开 PAGE_START 和 PAGE_END 位置以填充所需的宽度。如果顶部和底部组件更宽,那么这也会展开容器的宽度。
英文:
If your goal is to have the button above the image, and have the button's width expand with the image, then:
- Get rid of your use of null layouts and
.setBounds(...)
(this is just good general advice) - Put the JLabel with the image into a JPanel that uses BorderLayout with the JLabel in the BorderLayout.CENTER position
- Put the button above the JLabel in the same JPanel using the BorderLayout.PAGE_START position.
- Then put that JPanel wherever it is needed in the GUI, nesting JPanels, each using their own layout manager.
The BorderLayout will allow the center component to fill that position, and will expand the PAGE_START and PAGE_END positions to fill the width necessary. If the top and bottom components are wider, then this will also expand the width of the container.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论