我正在制作一个简单的地址簿GUI,我对布局没有很好的把握。我希望我的GUI看起来像这样......
这是我的驱动程序:
import javax.swing.*;
public class AddressBookGui {
public static void main (String[] args)
{
userInput addressBook = new userInput();
addressBook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //whenever you hit x you will exit the program
addressBook.setSize(750, 600);
addressBook.setVisible(true);
}
}
This is my USERINPUT class
import java.awt.*;
import javax.swing.*;
public class userInput extends JFrame {
private JButton newEntry;
private JButton deleteEntry;
private JButton editEntry;
private JButton saveEntry;
private JButton cancelEntry;
private FlowLayout layout;
private JTextField lastName;
private JTextField middleName;
private JTextField firstName;
private JTextField phone;
public userInput() {
super("My Address Book"); //sets the title!
Container content = getContentPane();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
newEntry = new JButton("New");
deleteEntry = new JButton("Delete");
editEntry = new JButton("Edit");
saveEntry = new JButton("Save");
cancelEntry = new JButton("Cancel");
buttonPanel.add(newEntry);
buttonPanel.add(deleteEntry);
buttonPanel.add(editEntry);
buttonPanel.add(saveEntry);
buttonPanel.add(cancelEntry);
add(buttonPanel, BorderLayout.SOUTH);
content.setLayout(new BorderLayout());
content.add(buttonPanel, "South");
lastName = new JTextField(10); //set the length to 10.
add(lastName); //adds item1 to the window
firstName = new JTextField(10);
add(firstName);
middleName = new JTextField(10);
add(middleName);
phone = new JTextField(10);
add(phone);
setVisible(true);
}
}
目前我的按钮位于底部,但GUI只是一个巨大的文本框。谢谢你的帮助。
答案 0 :(得分:2)
您需要另一个JPanel来放置标签和文本字段。
labelPanel = new JPanel();
labelPanel.add(lastName);
//etc
add(labelPanel, BorderLayout.CENTER);
您可以使用GridBagLayout等更高级的布局管理器来正确组织组件。
答案 1 :(得分:1)