有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使组件跨越GridBagLayout中的多个单元格

我必须为学校做这个:

GUI

这是我目前掌握的代码:

import javax.swing.*;
import java.awt.*;

public class AddressBookGui1 extends JFrame {
public AddressBookGui1(){

    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    setLayout(gbl);

    JLabel label;
    JButton button;
    JTextField textField;
    JTextArea textArea = new JTextArea(10, 20);

    gbc.weightx = 1;
    label = new JLabel("text");
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 0;
    add(label ,gbc);

    textField = new JTextField();
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 0;
    add(textField ,gbc);

    label = new JLabel("text");
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    add(label ,gbc);

    textField = new JTextField();
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridwidth = 2;
    add(textField, gbc);

    label = new JLabel("text");
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    add(label ,gbc);

    textField = new JTextField();
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    add(textField, gbc);

    label = new JLabel("text");
    gbc.weightx = 1;
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    add(label ,gbc);



    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.gridwidth = 2;
    gbc.gridx = 1;
    gbc.gridy = 3;

    add(textArea, gbc);

    gbc.weightx = 1;
    button = new JButton("text");
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridwidth = 1;
    gbc.gridx = 0;
    gbc.gridy = 4;
    add(button ,gbc);

    gbc.weightx = 1;
    button = new JButton("text");
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 3;
    gbc.gridy = 4;
    add(button ,gbc);



}
public static void main(String[] args){
  AddressBookGui1 frame = new AddressBookGui1();
  frame.setTitle("Address Book");
  frame.setSize(400, 300);
  frame.setLocationRelativeTo(null);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true); 
}

}

This is the result

(我仍然需要处理填充和插入。我已经让它们在一个更简单的程序中工作,所以我想我已经掌握了这些东西)

我已经尝试过GridBagLayout Oracle教程,但我不确定我做错了什么。有人能帮我让它看起来更像是应该的吗?特别是使文本字段和文本区域跨越2个单元格


共 (2) 个答案

  1. # 1 楼答案

    关于你的代码,我注意到了一些事情

    • 不要使用JFrame的setSize()。这将导致异常行为。 取而代之的是,让框架根据其自身的大小来调整自身的大小 组件。如果希望框架更大,请不要调整尺寸 但框架内的组件。你也可以 设置PreferredSize或覆盖组件的getpreferredsize,如果 您确实需要调整is大小,因为GridBagLayout是那些尊重 首选组件的大小。使用pack()删除不必要的 空间

    • 不要扩展JFrame,使您的UI类具有主面板和 在那里添加所有组件。为该面板提供一个吸气剂(例如。 getUI())用于提取该类的UI

    • 无论何时,始终重新实例化GridBagConstraints对象 将应用于另一个组件。这样就更安全了 可读的

    • 使用插图在零部件周围放置填充物

    • 不要对不同的组件重复使用相同的引用

    • 使用Initial Thread

    • 这不是标准的,但我发现它在工作时真的很有用 使用GridBagLayout,在设置gbc的约束时,将其设置为 按字母顺序排列

    为了解决您的问题,下面是修改后的代码,其中包含了我所指出的关于applicated的内容

    public class AddressBook {
    
        private JPanel pnlMain;
    
        public AddressBook() {
            pnlMain = new JPanel();
            pnlMain.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
    
            JLabel lblName = new JLabel("Name");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(0, 10, 0, 0);
            gbc.weightx = 1;
            pnlMain.add(lblName, gbc);
    
            JTextField txtName = new JTextField();
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = 3;
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.insets = new Insets(5, 0, 0, 10);
            gbc.weightx = 1;
            pnlMain.add(txtName, gbc);
    
            JLabel lblPhone = new JLabel("Phone");
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = 1;
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.insets = new Insets(0, 10, 0, 0);
            gbc.weightx = 1;
            pnlMain.add(lblPhone, gbc);
    
            JTextField txtPhone = new JTextField();
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = 3;
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.insets = new Insets(5, 0, 0, 10);
            gbc.weightx = 1;
            pnlMain.add(txtPhone, gbc);
    
            JLabel lblEmail = new JLabel("Email");
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = 1;
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.insets = new Insets(0, 10, 0, 0);
            gbc.weightx = 1;
            pnlMain.add(lblEmail, gbc);
    
            JTextField txtEmail = new JTextField();
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = 3;
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.weightx = 1;
            gbc.insets = new Insets(5, 0, 0, 10);
            pnlMain.add(txtEmail, gbc);
    
            JLabel lblAddress = new JLabel("Address");
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = 1;
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.insets = new Insets(0, 10, 0, 0);
            gbc.weightx = 1;
            pnlMain.add(lblAddress, gbc);
    
            JTextArea txtAreaAddress = new JTextArea(10, 20);
            JScrollPane pane = new JScrollPane(txtAreaAddress);
            gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.gridwidth = 3;
            gbc.gridx = 1;
            gbc.gridy = 3;
            gbc.insets = new Insets(5, 0, 0, 10);
            gbc.weightx = 1;
            pnlMain.add(pane, gbc);
    
            JButton btnSave = new JButton("Save");
            gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.NONE;
            gbc.gridwidth = 1;
            gbc.gridx = 0;
            gbc.gridy = 4;
            gbc.insets = new Insets(10, 10, 10, 0);
            gbc.weightx = 1;
            pnlMain.add(btnSave, gbc);
    
            JButton btnCancel = new JButton("Cancel");
            gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.EAST;
            gbc.gridwidth = 1;
            gbc.gridx = 3;
            gbc.gridy = 4;
            gbc.insets = new Insets(10, 0, 10, 10);
            gbc.weightx = 1;
            pnlMain.add(btnCancel, gbc);
    
        }
    
        public JPanel getUI(){
            return pnlMain;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame("Address Book");
                    frame.getContentPane().add(new AddressBook().getUI());
                    frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    
    }
    
  2. # 2 楼答案

    gbc。gridwidth是允许组件跨越多个列的参数。例如,如果有3列4行,并且希望标签占据完整的顶行,则需要为标签指定第一个单元格。并设置gbc。网格宽度=3