我必须为学校做这个:
这是我到目前为止的代码:
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);
}
}
(我仍然需要处理填充和插入。我已经让它们在一个更简单的程序中工作,所以我想我可以处理这些东西)
我已经尝试过 GridBagLayout Oracle 教程,但我不确定我做错了什么。有人可以帮我让它看起来更像它应该的样子吗?专门使文本字段和文本区域跨越 2 个单元格。