如何解决为什么我的对象在 Swing UI 上重叠?
我使用 gridbag 布局在 Java 中创建了一个 Swing UI。这是添加组件的代码;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth=2;
this.add(loggedInLabel,c);
// c.fill = GridBagConstraints.HORIZONTAL;
c.anchor =GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 1;
this.add(inputCommand,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
this.add(confirmCommandButton,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
this.add(replyField,c);
// c.fill = GridBagConstraints.VERTICAL;
c.anchor =GridBagConstraints.EAST;
c.gridx = 1;
c.gridy = 1;
this.add(screens,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=4;
c.gridwidth=2;
c.anchor = GridBagConstraints.soUTHWEST;
c.weightx = 0.25;
c.weighty = 0.25;
this.add(scrollPane,c);
出于某种原因,屏幕对象 (JComboBox) 与输入命令对象 (JTextField) 重叠。有谁知道为什么?它们应该在不同的 x 坐标上
解决方法
分析你的代码,首先你将gridwidth设置为2,这意味着所有后续组件的x网格跨度都是2,包括inputCommand
:
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth=2; //Span 2 columns
this.add(loggedInLabel,c);
c.anchor =GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 1;
this.add(inputCommand,c); //inputCommand is two columns wide,cells (0,1)-(1,1)
.....
c.anchor =GridBagConstraints.EAST;
c.gridx = 1;
c.gridy = 1;
this.add(screens,c); //Screens is also two columns wide,starting from 1 so
//it will partially overlap inputCommand on cell (1,1)
...
,
在将所有缺失的行添加到您的代码中后,我想出了以下 GUI。
我不得不对您其余代码的样子进行了很多猜测,我不知道这是否与您所做的相符。
我添加了一个 Insets
实例来分隔组件。
这是我使用的完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class GridBagLayoutExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GridBagLayoutExample());
}
@Override
public void run() {
JFrame frame = new JFrame("GirdBagLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(),BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5,5,5));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5,5);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
JLabel loggedInLabel = new JLabel("Logged in");
panel.add(loggedInLabel,c);
// c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 1;
JTextField inputCommand = new JTextField(10);
panel.add(inputCommand,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
JButton confirmCommandButton = new JButton("Confirm Command");
panel.add(confirmCommandButton,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
JTextField replyField = new JTextField(10);
panel.add(replyField,c);
// c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.EAST;
c.gridx = 1;
c.gridy = 1;
JPanel screens = new JPanel();
panel.add(screens,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2;
c.anchor = GridBagConstraints.SOUTHWEST;
c.weightx = 0.25;
c.weighty = 0.25;
JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane,c);
return panel;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。