JAVA图形界面问题(采用弹窗报错)
JOptionPane.showMessageDialog("null", "你的输入不合法!");
这种方法是swing里面的 如果用awt里面的方法其实是dialog d = new dialog(f,"提示信息“,true)f表示的是这个对话框是哪个窗体错误时的弹出对话框,true表示的是如果这个弹出对话框不关闭则不能对frame进行操作, 在 d里面再加一个label表示你要提示的信息。给你贴一段代码
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = tf.getText();
tf.setText("");
File file = new File(text);
if(file.exists() && file.isDirectory()){//--------->两种遍历方法。。。记下来吧都
ta.setText("");
/*String[] names = file.list();
for (String name : names) {
ta.append(name + "\r\n");
}*/
traverse(file);
}
else{
String msg = "您输入的" + text +"有误,请重新输入!";
lab.setText(msg);
d.setVisible(true);
}
}
});
这一块的else部分就是实现弹窗的代码
import static java.awt.BorderLayout.NORTH;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
class Demo{
private Frame f;
private TextArea ta;
private Button but;
private TextField tf;
private Dialog d;
private Label lab;//错误操作弹出对话框里面的文字
private Button bt;
private Panel p;
Demo(){
init();
}//endof Demo
public void init(){
lab = new Label();//提示信息,现在还不知道
p = new Panel();
f = new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
ta = new TextArea(25,69);
d = new Dialog(f,"提示信息",true);
bt = new Button("确定");
d.setBounds(400,200,300,100);
//d.setLayout(new FlowLayout());
d.add(lab,NORTH);
p.setLayout(new FlowLayout());
p.add(bt);
d.add(p);
f.add(tf);
f.add(but);
f.add(ta);
f.setVisible(true);
myEvent();
}//endof init()
//实现各种监听事件
public void myEvent(){
tf.addActionListener(new ActionListener(){//按回车实现"转到"按钮
public void actionPerformed(ActionEvent e){
String text = tf.getText();
tf.setText("");
File file = new File(text);
if(file.exists() && file.isDirectory()){//--------->两种遍历方法。。。记下来吧都
ta.setText("");
/*String[] names = file.list();
for (String name : names) {
ta.append(name + "\r\n");
}*/
traverse(file);
}
else{
String msg = "您输入的" + text +"有误,请重新输入!";
lab.setText(msg);
d.setVisible(true);
}
}
});
//关闭窗口
f.addWindowListener((new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(1);
}
}));
//隐藏对话框
d.addWindowListener((new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
}));
//按确定按钮隐藏对话框
bt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
d.setVisible(false);
}
});
//按”转到“按钮实现遍历
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = tf.getText();
tf.setText("");
File file = new File(text);
if(file.exists() && file.isDirectory()){//--------->两种遍历方法。。。记下来吧都
ta.setText("");
/*String[] names = file.list();
for (String name : names) {
ta.append(name + "\r\n");
}*/
traverse(file);
}
else{
String msg = "您输入的" + text +"有误,请重新输入!";
lab.setText(msg);
d.setVisible(true);
}
}
});
}//endof myEvent()
public void traverse(File file){//遍历文件夹
File[] arr = file.listFiles();//返回一个文件路径数组
for(int i = 0; i < arr.length; i++){
if(arr[i].isFile())
ta.append(arr[i] + "\r\n");
else traverse(arr[i]);
}
}//endof traverse()
}//endof Demo{}
public class Main{
public static void main(String[] args) {
new Demo();
}
}
实现界面如图
广告 您可能关注的内容 |