import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TextFieldDemo {
JTextField nameTextField,addressTextField;
JFrame frame;
JPanel contentPane;
JLabel nameLabel,addressLabel;
public TextFieldDemo(){
frame = new JFrame("TextFieldDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
frame.setContentPane(contentPane);
nameLabel = new JLabel("姓名:");
addressLabel = new JLabel("地址:");
nameTextField = new JTextField();
nameTextField.setText("请输入姓名");
nameTextField.setHorizontalAlignment(JTextField.RIGHT);
nameTextField.setBounds(62, 7, 60, 21);
addressTextField = new JTextField();
addressTextField.setText("请输入地址");
addressTextField.setHorizontalAlignment(JTextField.CENTER);
nameTextField.setBounds(62, 35, 120, 21);
}
public void createAndShowGUI(){
contentPane.add(nameLabel);
contentPane.add(nameTextField);
contentPane.add(addressLabel);
contentPane.add(addressTextField);
frame.setLocationRelativeTo(frame);
frame.setPreferredSize(new Dimension(300,200));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new TextFieldDemo().createAndShowGUI();
}
});
}
}
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TextFieldDemo {
JTextField nameTextField,addressTextField;
JFrame frame;
JPanel contentPane;
JLabel nameLabel,addressLabel;
public TextFieldDemo(){
frame = new JFrame("TextFieldDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
frame.setContentPane(contentPane);
nameLabel = new JLabel("姓名:");
addressLabel = new JLabel("地址:");
nameTextField = new JTextField();
nameTextField.setText("请输入姓名");
nameTextField.setHorizontalAlignment(JTextField.RIGHT);
nameTextField.setBounds(62, 7, 60, 21);
addressTextField = new JTextField();
addressTextField.setText("请输入地址");
addressTextField.setHorizontalAlignment(JTextField.CENTER);
nameTextField.setBounds(62, 35, 120, 21);
}
public void createAndShowGUI(){
contentPane.add(nameLabel);
contentPane.add(nameTextField);
contentPane.add(addressLabel);
contentPane.add(addressTextField);
frame.setLocationRelativeTo(frame);
frame.setPreferredSize(new Dimension(300,200));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new TextFieldDemo().createAndShowGUI();
}
});
}
}