实例如下:
代码如下 | 复制代码 |
importjava.util.*; importjavax.swing.*; importjava.awt.*; importjava.io.*; importjava.awt.event.*; classMyWindow{ privateFrame f1; privateButton bt1; privateTextField tx1; privateTextArea tx2; MyWindow(){ init(); } publicvoidinit(){ f1=newFrame("窗体"); f1.setBounds(300,100,600,500); f1.setLayout(newFlowLayout()); bt1=newButton("按钮"); tx1=newTextField(30); tx2=newTextArea(25,70); f1.add(tx1); f1.add(bt1); f1.add(tx2); event(); f1.setVisible(true);
}
privatevoidevent(){ bt1.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvent e){
String dirpath=tx1.getText(); File dir =newFile(dirpath); if(dir.exists()&& dir.isDirectory()){ String names[] =dir.list(); for(String name:names){ tx2.setText(name+"rn"); } } // System.out.println(s); //tx2.append(s);
} }); f1.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvent E){ System.exit(0); } }); } publicstaticvoidmain(String args[]){ newMyWindow(); } } |