采用简单的Swing用户界面组件实现基本信息采集工作。
TextComponentFrame.java
package text;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class TextComponentFrame extends JFrame {
public static final int TEXTAREA_ROWS = 8;
public static final int TEXTAREA_COLUNS = 20;
final JTextField textfield1 = new JTextField();
final JTextField textfield2 = new JTextField();
final JTextField textfield3 = new JTextField();
final JTextField textfield4 = new JTextField();
final JTextField textfield5 = new JTextField();
int id = 0;
public TextComponentFrame() {
JPanel northpanel = new JPanel();
northpanel.setLayout(new GridLayout(5, 2));
northpanel.add(new JLabel("姓名:", SwingConstants.RIGHT));
northpanel.add(textfield1);
northpanel.add(new JLabel("性别:", SwingConstants.RIGHT));
northpanel.add(textfield2);
northpanel.add(new JLabel("年龄:", SwingConstants.RIGHT));
northpanel.add(textfield3);
northpanel.add(new JLabel("身份证号:", SwingConstants.RIGHT));
northpanel.add(textfield4);
northpanel.add(new JLabel("家庭住址:", SwingConstants.RIGHT));
northpanel.add(textfield5);
add(northpanel, BorderLayout.NORTH);
final JTextArea textArea = new JTextArea("详细信息显示区",TEXTAREA_ROWS, TEXTAREA_COLUNS);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
JPanel southPanel = new JPanel();
JButton insertButton = new JButton("提交");
southPanel.add(insertButton);
insertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(id == 0) {
textArea.setText("");
}
id++;
textArea.append(
"编号:" + id + "\n" +
"姓名:" + textfield1.getText() + "\n" +
"性别:" + textfield2.getText() + "\n" +
"年龄:" + textfield3.getText() + "\n" +
"身份证号:" + textfield4.getText() + "\n" +
"家庭住址:" + textfield5.getText() + "\n" +
"——————————————————————————————————————" + "\n");
resetValue();
}
});
add(southPanel,BorderLayout.SOUTH);
pack();
}
protected void resetValue() {
// TODO Auto-generated method stub
textfield1.setText("");
textfield2.setText("");
textfield3.setText("");
textfield4.setText("");
textfield5.setText("");
}
}
测试程序
TestFrame.java
package text;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class TestFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new TextComponentFrame();
frame.setTitle("FontTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}