代码Eclipse代码区不显示错误,但是运行代码的时候弹出了引用类的错误,不知道是我的文件损坏了,还是我代码有问题
代码如下:
package f1rame;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class JTextFieldTest extends JFrame{
public JTextFieldTest(){
JPanel panel=new JPanel();
JTextField field =new JTextField();
field.setColumns(20);
field.setHorizontalAlignment(JTextField.CENTER);
field.setText("中对齐20栏");
panel.add(field);
field=new JTextField();
field.setColumns(20);
field.setText("左对齐边框20栏");
field.setBorder(BorderFactory.createEmptyBorder());
panel.add(field);
field= new JTextField();
field.setColumns(40);
field.setHorizontalAlignment(JTextField.RIGHT);
field.setFont(new Font("隶书",Font.BOLD,12));
field.setText("右对齐不可编辑隶书加粗字体");
field.setEditable(false);
panel.add(field);
JPasswordField pField=new JPasswordField(10);
pField.setText("1234567");
pField.add(pField);
char[] password=pField.getPassword();
String passwordStr=new String(password);
field=new JTextField(8);
field.setText(passwordStr);
panel.add(field);
setContentPane(panel);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JTextFieldTest test =new JTextFieldTest();
test.setSize(new Dimension(300,150));
test.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
test.setVisible(true);
}
}
提示错误如下:
Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
at java.awt.Container.checkAddToSelf(Container.java:420)
at java.awt.Container.addImpl(Container.java:1038)
at java.awt.Container.add(Container.java:365)
at f1rame.JTextFieldTest.<init>(JTextFieldTest.java:36)
at f1rame.JTextFieldTest.main(JTextFieldTest.java:50)
|
|