gpt4 book ai didi

java - 界面不在列中

转载 作者:行者123 更新时间:2023-12-01 15:07:32 24 4
gpt4 key购买 nike

我目前正在编写一个产品系统并正在设计界面。我的问题是,接口(interface)不在一个列中。 enter image description here我希望它与我的代码代码放在一列中,并且最好在“suche nach waren”行中再添加一个按钮这是代码:

GroupLayout tLayout = new GroupLayout(mainFrame.getContentPane());
mainFrame.getContentPane().setLayout(tLayout);
tLayout.setAutoCreateGaps(true);
tLayout.setAutoCreateContainerGaps(true);

tLayout.setHorizontalGroup(tLayout.createSequentialGroup()
.addGroup(tLayout.createParallelGroup()
.addComponent(tLabel0))
.addGroup(tLayout.createParallelGroup()
.addComponent(tLabel1)
.addComponent(tLabel2)
.addComponent(tLabel3)
.addComponent(tLabel4))
.addGroup(tLayout.createParallelGroup()
.addComponent(tTextField1)
.addComponent(tTextField2)
.addComponent(tTextField3)
.addComponent(tCombo)
.addComponent(tButton1))
.addGroup(tLayout.createParallelGroup()
.addComponent(tTable))
);

tLayout.setVerticalGroup(tLayout.createSequentialGroup()
.addComponent(tLabel0)
.addGroup(tLayout.createParallelGroup()
.addComponent(tLabel1)
.addComponent(tTextField1))
.addGroup(tLayout.createParallelGroup()
.addComponent(tLabel2)
.addComponent(tTextField2))
.addGroup(tLayout.createParallelGroup()
.addComponent(tLabel3)
.addComponent(tTextField3))
.addGroup(tLayout.createParallelGroup()
.addComponent(tLabel4)
.addComponent(tCombo))
.addGroup(tLayout.createParallelGroup()
.addComponent(tButton1))
.addGroup(tLayout.createParallelGroup()
.addComponent(tTable)));

我的代码有什么问题吗?感谢您的回答!

PS.:我不想使用布局编辑器,因为这是我第一次,我想了解 SWING!

PPS:

|Geben sie bitte die Kriterien für die Suche an:  
|Name: (textfield)
|Maximaler Preis: (textfield)
|Alter des Kunden:(textfield)
|Kategorie: (Combo)
| (Button)
|Table....

除了上面的标签和表格之外,一切都在正确的位置

最佳答案

GridBagLayout 示例...

enter image description here

您确实应该将 JTable 放入 JScrollPane 中,它会为您处理标题,但是干草

public class TestLayout {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new FormPane());
frame.setSize(600, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

protected static class FormPane extends JPanel {

public FormPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;

add(new JLabel("Geben sie bitte die Kriterien für die Suche an:"), gbc);

gbc.gridwidth = 1;
gbc.gridy++;
add(new JLabel("Name:"), gbc);
gbc.gridy++;
add(new JLabel("Maximaler Preis:"), gbc);
gbc.gridy++;
add(new JLabel("Alter des Kunden:"), gbc);
gbc.gridy++;
add(new JLabel("Kategorie:"), gbc);

gbc.gridx++;
gbc.gridy = 1;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JComboBox(), gbc);
gbc.gridy++;
add(new JButton("Click me"), gbc);

gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;

DefaultTableModel model = new DefaultTableModel(
new Object[][] {
{"id", "Name", "Price", "Something", "Something", "Something"},
{"NA", "NA", "NA", "NA", "NA", "NA"}
},
new Object[]{"id", "Name", "Price", "Something", "Something", "Something"});


add(new JTable(model), gbc);

}
}
}

关于java - 界面不在列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12777639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com