简体   繁体   English

JButton和JLabel彼此相邻

[英]JButton and JLabel next to each other

I want to have a JLabel and a JButton next to each other, in the next row an other Jlaben and in the next row a JButton that is in the center. 我想要一个JLabel和一个JButton彼此相邻,在下一行中使用另一个Jlaben ,在下一行中使用一个位于中心的JButton

The leftPanel is 350 wide. leftPanel为350。

The first JButton has an ImageIcon and that should be in the right top corner, that image is 25x25. 第一个JButton有一个ImageIcon ,应该在右上角,该图像为25x25。

This code returns a panel to get then shown 此代码返回一个面板,然后显示

JPanel leftPanel = JPanel();

leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));

JLabel userLabel = new JLabel();
userLabel.setPreferredSize(new Dimension(170, 25));
userLabel.setMaximumSize(new Dimension(170, 25));
leftPanel.add(userLabel);

// class that creats a JButton with an ImageIcon and removes border
JButton updateButton = DefaultLayouts.imageButton(new ImageIcon("src/ressources/images/icons/update.png"));
updateButton.setPreferredSize(new Dimension(170, 25));
updateButton.setMaximumSize(new Dimension(170, 25));

leftPanel.add(updateButton);

timeStampLabel.setPreferredSize(new Dimension(350, 25));
timeStampLabel.setMaximumSize(new Dimension(350, 25));
leftPanel.add(timeStampLabel);

unlockButton = DefaultLayouts.imageButton(new ImageIcon("src/ressources/images/icons/unlock.png"));
unlockButton.setPreferredSize(new Dimension(350, 25));
unlockButton.setMaximumSize(new Dimension(350, 25));

leftPanel.add(unlockButton);

panel.add(leftPanel();

Now it's just in the center and I don't know what to do with the blue button. 现在它只是在中间,我不知道该使用蓝色按钮怎么办。 image 图片

Updated Code: 更新的代码:

JPanel panel = DefaultLayouts.panel();
panel.setLayout(new GridLayout(0, 2));

JPanel leftPanel = DefaultLayouts.marginPanel(0, 10, 0, 10);
leftPanel.setLayout(new GridBagLayout());

GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;

gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
leftPanel.add(userLabel, gridBagConstraints);

JButton updateButton = DefaultLayouts.imageButton(new ImageIcon("src/ressources/images/icons/update.png"));
gridBagConstraints.gridwidth = 1;
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
leftPanel.add(updateButton, gridBagConstraints);

gridBagConstraints.gridwidth = 2;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
leftPanel.add(timeStampLabel, gridBagConstraints);

unlockButton = DefaultLayouts.imageButton(new ImageIcon("src/ressources/images/icons/unlock.png"));
gridBagConstraints.gridwidth = 3;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
leftPanel.add(unlockButton, gridBagConstraints);

panel.add(leftPanel);

You can use a FlowLayout or GridLayout. 您可以使用FlowLayout或GridLayout。

JPanel panel = new JPanel();

panel.setLayout(new FlowLayout());
//or panel.setLayout(new GridLayout(rows, cols));

panel.add(new JLabel("Label"));
panel.add(new JButton("Click"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM