当前试图熟悉GridBagLayout。 我试图将某些组件彼此相邻放置。但是,我有一个跨多个单元格的JTable组件,我想彼此相邻的组件应该在JTable组件上方。
我不确定如何告诉Java / GBL将所有其他组件彼此相邻放置。
我了解了有关网格宽度(GridBagConstraints.RELATIVE)的信息,但是它不起作用。
有帮助吗?
谢谢。
尝试的网格宽度= GBC.RELATIVE
公共类PMFrame扩展了DatabaseConnection {
// CONSTRUCTOR
public PMFrame() {
//JFrame
JFrame frame = new JFrame("GridBagLayout");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.setBackground(Color.LIGHT_GRAY);
GridBagConstraints c = new GridBagConstraints();
//JTable
String[] columnNames = {"ID","Anwendung","Password"};
String [][] data = {{"Katy","Facebook","epyo0wj5"},{"Nico","Instagram","djdjdjj"}};
JTable t = new JTable(data,columnNames);
JScrollPane sp = new JScrollPane(t);
//Labels
JLabel labelInsert = new JLabel("Inserting");
JLabel labelUpdate = new JLabel("Updating");
JLabel labelDelete = new JLabel("Deleting");
JLabel labelSearch = new JLabel("Searching");
JLabel labelJTable = new JLabel("Passwort-Tabelle:");
//Buttons
JButton buttonInsert = new JButton("Insert");
JButton buttonUpdate = new JButton("Update");
JButton buttonDelete = new JButton("Delete");
JButton buttonSearch = new JButton("Search");
JButton buttonPopulate = new JButton("Populate");
JButton buttonClose = new JButton("CLOSE");
//Textfields Anwendung
JTextField textfieldAnwendung1 = new JTextField(7);
JTextField textfieldAnwendung2 = new JTextField(7);
JTextField textfieldAnwendung3 = new JTextField(7);
JTextField textfieldAnwendung4 = new JTextField(7);
//Textfields Password
JTextField textfieldPassword1 = new JTextField(7);
JTextField textfieldPassword2 = new JTextField(7);
JTextField textfieldPassword3 = new JTextField(7);
JTextField textfieldPassword4 = new JTextField(7);
//Row 0
c.anchor = GridBagConstraints.LINE_START;
c.gridx = 0;
c.gridy = 0;
panel.add(labelInsert,c);
c.gridx = 1;
c.gridy = 0;
panel.add(labelUpdate,c);
c.gridx = 2;
c.gridy = 0;
panel.add(labelDelete,c);
c.gridx = 3;
c.gridy = 0;
panel.add(labelSearch,c);
//Row 1
c.gridx = 0;
c.gridy = 1;
panel.add(textfieldAnwendung1,c);
c.gridx = 1;
c.gridy = 1;
panel.add(textfieldAnwendung2,c);
c.gridx = 2;
c.gridy = 1;
panel.add(textfieldAnwendung3,c);
c.gridx = 3;
c.gridy = 1;
panel.add(textfieldAnwendung4,c);
//Row 2
c.gridx = 0;
c.gridy = 2;
panel.add(textfieldPassword1,c);
c.gridx = 1;
c.gridy = 2;
panel.add(textfieldPassword2,c);
c.gridx = 2;
c.gridy = 2;
panel.add(textfieldPassword3,c);
c.gridx = 3;
c.gridy = 2;
panel.add(textfieldPassword4,c);
c.gridx = 0;
c.gridy = 5;
panel.add(sp,c);
// FRAME ACTIVATION
frame.add(panel);
frame.setLocation(600, 350);
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);