public class Mainextends JFrame{
privateLogger logger=LoggerFactory.getLogger(Main.class);
privateJTextField textField;
privateJTextArea textArea;
publicMain(){
super("FreeBird");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(newFlowLayout());
textField=newJTextField();
textField.setColumns(25);
textArea=newJTextArea(30,25);
this.add(textField);
this.add(textArea);
initComponent();
this.setSize(350,400);
this.setVisible(true);
}
private voidinitComponent(){
this.textField.addKeyListener(newKeyAdapter() {
public voidkeyPressed(KeyEvent e) {
intkeycode=e.getKeyCode();
if(keycode==KeyEvent.VK_ENTER){
//按下Enter执行
}
}
});
}
public static voidmain(String[] args) {
Main main=newMain();
}
}
注意
1. 布局:this.setLayout(newFlowLayout());这句,如果不设置布局方式,各组件会重合在一起,效果就是一个组件占了整个窗口。
2. TextField大小:textField.setColumns(25);不写这个而是去写setSize()是不起作用的。