Java 在简单GUI中使用JLabels的JTextField
我正在制作一个简单的地址簿GUI,但我对布局没有很好的掌握。我希望我的GUI看起来像这样 这是我的司机:Java 在简单GUI中使用JLabels的JTextField,java,user-interface,Java,User Interface,我正在制作一个简单的地址簿GUI,但我对布局没有很好的掌握。我希望我的GUI看起来像这样 这是我的司机: import javax.swing.*; public class AddressBookGui { public static void main (String[] args) { userInput addressBook = new userInput(); addressBook.setDefaultCloseOper
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只是一个巨大的文本框。感谢您的帮助。请查看教程:
您需要另一个JPanel来放置标签和文本字段
labelPanel = new JPanel();
labelPanel.add(lastName);
//etc
add(labelPanel, BorderLayout.CENTER);
您可以为面板使用更高级的布局管理器(如GridBagLayout)来正确组织组件 请遵循命名约定:类名以大写字母开头您是否必须为每个JPanel创建一个新容器?JPanel是一个容器。你只需要一个来放置你所有的文本字段和标签。将其添加到JFrame的中心。如果你能做到这一点,那么你可以考虑使用高级布局管理器来布局JPanel中的文本字段。