//当按按钮2的时候,在c:\javatest下面创建一个test.wma,我不知道怎么该才可以实现我想要的功能 ,
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;public class myproject1 extends Frame implements ActionListener
{
static myproject1 frm=new myproject1();
static Button btn1=new Button("Yellow");
static Button btn2=new Button("Green");
static Button btn3=new Button("Exit");public static void createFile(String path,String filename) { try {File file=new File(path+"/"+filename);
if(!file.exists()){
file.createNewFile();
}} catch (Exception e) {
message = "nishibaile";}
}
public static void main(String args[])
{
btn1.addActionListener(frm); // 把事件聆听者frm向btn1注册
btn2.addActionListener(frm); // 把事件聆听者frm向btn2注册
btn3.addActionListener(frm); // 把事件聆听者frm向btn3注册frm.setTitle("Action Event");
frm.setLayout(new FlowLayout(FlowLayout.CENTER));
frm.setSize(200,150);
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Button btn=(Button) e.getSource(); // 取得事件源?象
if(btn==btn1) // 如果是按下btn1
frm.setBackground(Color.yellow);
else if(btn==btn2) // 如果是按下btn2
myproject1.createFile("c://javatest/","test.wma");
else // 如果是按下btn3
System.exit(0);
}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;public class myproject1 extends Frame implements ActionListener
{
static myproject1 frm=new myproject1();
static Button btn1=new Button("Yellow");
static Button btn2=new Button("Green");
static Button btn3=new Button("Exit");public static void createFile(String path,String filename) { try {File file=new File(path+"/"+filename);
if(!file.exists()){
file.createNewFile();
}} catch (Exception e) {
message = "nishibaile";}
}
public static void main(String args[])
{
btn1.addActionListener(frm); // 把事件聆听者frm向btn1注册
btn2.addActionListener(frm); // 把事件聆听者frm向btn2注册
btn3.addActionListener(frm); // 把事件聆听者frm向btn3注册frm.setTitle("Action Event");
frm.setLayout(new FlowLayout(FlowLayout.CENTER));
frm.setSize(200,150);
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Button btn=(Button) e.getSource(); // 取得事件源?象
if(btn==btn1) // 如果是按下btn1
frm.setBackground(Color.yellow);
else if(btn==btn2) // 如果是按下btn2
myproject1.createFile("c://javatest/","test.wma");
else // 如果是按下btn3
System.exit(0);
}
}
message = "nishibaile";} 这个地方该怎么改呢 我改成 char message = "nishibaile";} 也报错 改成 alert(e.number)也报错,当然干脆让{}变空就不报错了,可我不想这么做,谁来指导一下啊?
参数传的有问题:
myproject1.createFile("c:/javatest","test.wma");
这样试试
压根没定义这个变量,你可以这样,在类中定义一静态的字符串,再将异常信息赋值给该变量:static String message = "";
... ...
message = e.getMessage();
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;public class myproject1 extends Frame implements ActionListener
{
static myproject1 frm=new myproject1();
static Button btn1=new Button("Yellow");
static Button btn2=new Button("createAnyfile");
static Button btn3=new Button("Exit");public static void createFile(String path,String filename) { try {File file=new File(path+"/"+filename);
if(!file.exists()){
file.createNewFile();
}} catch (Exception e) {}
}
public static void main(String args[])
{
btn1.addActionListener(frm); // 把事件聆听者frm向btn1注册
btn2.addActionListener(frm); // 把事件聆听者frm向btn2注册
btn3.addActionListener(frm); // 把事件聆听者frm向btn3注册frm.setTitle("myproject1");
frm.setLayout(new FlowLayout(FlowLayout.CENTER));
frm.setSize(200,150);
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Button btn=(Button) e.getSource(); // 取得事件源?象
if(btn==btn1) // 如果是按下btn1
frm.setBackground(Color.yellow);
else if(btn==btn2) // 如果是按下btn2
myproject1.createFile("c:/javatest/","test.wma");
else // 如果是按下btn3
System.exit(0);
}
}
这样才不出错