我对Java GUI编程比较陌生,虽然这些概念在我的脑海中很顺利,但我在删除JFrame中的空白空间时遇到了这个小问题。
如果你运行我下面的代码,你会注意到JTextFields和JButton之间存在相当大的差距。我希望删除此间隙,以便按钮触及最底层的JTextField。
另外,另一个非常小的问题。如何增加JTextField的高度,使它们有三行而不是一行?
无论如何这里是代码。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class HW4GUI extends JFrame
{
private JButton jbtAction;
private JTextField jtfFName;
private JTextField jtfLName;
private JTextField jtfLibNo;
private static int nextLibNo;
private JPanel textPanel;
public HW4GUI()
{
super("HW4GUI");
makeFrame();
showFrame();
}
public void makeFrame()
{
setLayout(new BorderLayout());
setResizable(false);
textPanel = new JPanel();
textPanel.setLayout(new GridLayout(3,2));
jbtAction = new JButton("Add Borrower");
JLabel FirstNameLabel = new JLabel("FirstName:");
jtfFName = new JTextField(3);
JLabel LastNameLabel = new JLabel("LastName:");
jtfLName = new JTextField(3);
JLabel LibNoLabel = new JLabel("Library Number:");
jtfLibNo = new JTextField(3);
FirstNameLabel.setHorizontalAlignment(JTextField.RIGHT);
LastNameLabel.setHorizontalAlignment(JTextField.RIGHT);
LibNoLabel.setHorizontalAlignment(JTextField.RIGHT);
jtfLibNo.setEditable(false);
textPanel.add(FirstNameLabel);
textPanel.add(jtfFName);
textPanel.add(LastNameLabel);
textPanel.add(jtfLName);
textPanel.add(LibNoLabel);
textPanel.add(jtfLibNo);
add(textPanel, BorderLayout.NORTH);
add(jbtAction, BorderLayout.SOUTH);
}
public void showFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
}
}