import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class DialogDemo
{
private Frame f;
private Button but;
private TextField tf;
private TextArea ta;
DialogDemo()
{
init();
}
private void init()
{
f = new Frame( "窗口" );
f.setVisible( true );
f.setBounds( 300, 100, 600, 500 );
f.setLayout( new FlowLayout() );
but = new Button( "转到" );
f.add( but );
tf = new TextField( 60 );
f.add( tf );
ta = new TextArea( 25, 70 );
f.add( ta );
}
private void myEvent()
{
but.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
String dir = tf.getText();
File dirpath = new File( dir );
if( dirpath.exists() && dirpath.isDirectory() )
{
String[] names = dirpath.list();
for( String name : names )
{
ta.append( name+"\r\n" );
}
}
else
{
final Dialog d = new Dialog( f, "提示", true );
Button butok = new Button( "确定" );
Label lab = new Label( dir+"不存在" );
d.setBounds( 350, 150, 200, 120 );
d.setLayout( new FlowLayout() );
d.add( butok );
d.add( lab );
d.setVisible( true );
butok.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
d.setVisible( false );
}
});
d.addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
d.setVisible( false );
}
});
}
}
});
}
public static void main(String[] args)
{
new DialogDemo().myEvent();
}
}
如果将对话框声明在主类在init()中建立对象,监听器在myEvent中注册点击按钮会将对话框隐藏,在else作用域中注册没反应.
如果是声明在主类,在else的作用域中建立对象,监听器在myEvent中注册,会抛出空指针异常,在else作用域中注册没反应.
求高手解答这是啥原因?
import java.awt.event.*;
import java.io.*;
import java.util.*;
class DialogDemo
{
private Frame f;
private Button but;
private TextField tf;
private TextArea ta;
DialogDemo()
{
init();
}
private void init()
{
f = new Frame( "窗口" );
f.setVisible( true );
f.setBounds( 300, 100, 600, 500 );
f.setLayout( new FlowLayout() );
but = new Button( "转到" );
f.add( but );
tf = new TextField( 60 );
f.add( tf );
ta = new TextArea( 25, 70 );
f.add( ta );
}
private void myEvent()
{
but.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
String dir = tf.getText();
File dirpath = new File( dir );
if( dirpath.exists() && dirpath.isDirectory() )
{
String[] names = dirpath.list();
for( String name : names )
{
ta.append( name+"\r\n" );
}
}
else
{
final Dialog d = new Dialog( f, "提示", true );
Button butok = new Button( "确定" );
Label lab = new Label( dir+"不存在" );
d.setBounds( 350, 150, 200, 120 );
d.setLayout( new FlowLayout() );
d.add( butok );
d.add( lab );
d.setVisible( true );
butok.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
d.setVisible( false );
}
});
d.addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
d.setVisible( false );
}
});
}
}
});
}
public static void main(String[] args)
{
new DialogDemo().myEvent();
}
}
如果将对话框声明在主类在init()中建立对象,监听器在myEvent中注册点击按钮会将对话框隐藏,在else作用域中注册没反应.
如果是声明在主类,在else的作用域中建立对象,监听器在myEvent中注册,会抛出空指针异常,在else作用域中注册没反应.
求高手解答这是啥原因?
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;class DialogDemo { private Frame f;
private Button but;
private TextField tf;
private TextArea ta; DialogDemo() {
init();
} private void init() {
f = new Frame("窗口");
f.setVisible(true);
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());
but = new Button("转到");
f.add(but);
tf = new TextField(60);
f.add(tf);
ta = new TextArea(25, 70);
f.add(ta); } private void myEvent() {
but.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
String dir = tf.getText();
File dirpath = new File(dir);
if (dirpath.exists() && dirpath.isDirectory()) {
String[] names = dirpath.list();
for (String name : names) {
ta.append(name + "\r\n");
}
} else {
final Dialog d = new Dialog(f, "提示", true);
Button butok = new Button("确定");
Label lab = new Label(dir + "不存在");
d.setBounds(350, 150, 200, 120);
d.setLayout(new FlowLayout());
d.add(butok);
d.add(lab);
butok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
d.setVisible(false);
}
});
d.setVisible(true);
}
}
});
} public static void main(String[] args) {
new DialogDemo().myEvent();
}
}