黑馬程式設計師 【】java學習之路——GUI開始簡析三
阿新 • • 發佈:2019-01-10
-------
android培訓、java培訓、期待與您交流! ----------
需求:實現如下視窗,功能:能夠轉到相應的碟符下顯示該盤內的內容
<span style="font-size:18px;">import java.awt.*; import java.awt.event.*; import java.io.*; class MyWindows { private Frame f; private TextField tf; private Button b ; private TextArea ta; MyWindows() { init(); } private void init() { f = new Frame("我的視窗"); f.setBounds(300,100,600,500); f.setLayout(new FlowLayout()); tf = new TextField(50); b = new Button("轉到"); ta = new TextArea(25,70); f.add(tf); f.add(b); f.add(ta); myEvent(); f.setVisible(true); } private void myEvent() { b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String dirPath = tf.getText(); File dir = new File(dirPath); if (dir.exists() && dir.isDirectory()) { ta.setText(""); String[] names = dir.list(); for(String name : names) { ta.append(name+"\r\n"); } } } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { new MyWindows(); } } </span>