关键字:button mouseclicked 在线等待
有2个button ,在public void mouseClicked(MouseEvent e)里
怎么判断是哪个button ?最好用事例说明。
我这么做为什么不对?
public void mouseClicked(MouseEvent e){
Button jb=(Button)e.getSource();
if (jb==b1)
{
try{
OutputStream os=new FileOutputStream("test.txt");
PrintWriter pw=new PrintWriter(os);
pw.println(t1.getText());
pw.println(t2.getText());
pw.close();
os.close();
}else {
DataInputStream dis=new DataInputStream(new FileInputStream("text.txt"));
String s="";
while((s=dis.readLine())!=null)
ta.setText(ta.getText()+"\r\n"+s);
}catch(Exception e1){}
}
}
谢谢!!在线等待,马上给分!!
问题点数:20、回复次数:7Top
1 楼lif2000(阿飞)回复于 2002-11-23 19:30:09 得分 3
最好是给两个button个加一个监听addActionListener(),然后写一个actionPerformed(ActionEvent e) 函数,在函数里面判断是对那个button的操作方法是:
if (e.getActionCommand()=="*****")
then
{}
else
{}
Top
2 楼beyond_xiruo(CorruptionException)回复于 2002-11-23 19:40:49 得分 17
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class bean7 extends JFrame implements ActionListener {
JPanel contentPanel=(JPanel)this.getContentPane();
JButton jb1=new JButton("myJButton1");
JButton jb2=new JButton("myJButton2");
public bean7() {
super("myFrame");
this.setSize(400,300);
this.setResizable(false);
this.setLocation(this.getToolkit().getScreenSize().width/2-200,this.getToolkit().getScreenSize().height/2-150);
contentPanel.setLayout(new FlowLayout());
contentPanel.add(jb1);
contentPanel.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton myJB=(JButton)e.getSource();
javax.swing.JOptionPane.showMessageDialog(contentPanel,myJB.getText());
}
public static void main(String[] args) throws Exception {
new bean7();
}
}Top
3 楼beyond_xiruo(CorruptionException)回复于 2002-11-23 19:41:54 得分 0
按钮的点击时间最好使用actionPerformed事件Top
4 楼beyond_xiruo(CorruptionException)回复于 2002-11-23 19:42:34 得分 0
sorry
应该是,
按钮的点击动作最好使用actionPerformed事件侦帧听Top
5 楼mychao2002(坚持,成功就在下一个转弯处!)回复于 2002-11-23 20:03:16 得分 0
为什么我这么写就错?
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class w implements ActionListener{
Frame f;
Button b1;
Button b2;
TextField t1;
TextField t2;
TextArea ta;
public static void main(String args[]){
w tt=new w();
tt.go();
}
public void go(){
try{
f=new Frame("test");
b1=new Button("input");
b2=new Button("output");
t1=new TextField();
t2=new TextField();
ta=new TextArea();
f.setLayout(new GridLayout(3,2));
f.add(t1);
f.add(t2);
f.add(b1);
f.add(b2);
f.add(ta);
f.setSize(300,300);
f.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
}catch(Exception e){}
}
public void actionPerformed(ActionEvent e){
try{
if (e.getActionCommand()=="input")
{ //w w1=new w();
OutputStream os=new FileOutputStream("test.txt");
PrintWriter pw=new PrintWriter(os);
pw.println(t1.getText());
pw.println(t2.getText());
pw.close();
os.close();
}
else
{DataInputStream dis=new DataInputStream(new FileInputStream("test.txt"));
String s="";
while((s=dis.readLine())!=null)
ta.setText(ta.getText()+"\r\n"+s);
}
}catch(Exception e1){}
}
}
Top
6 楼mychao2002(坚持,成功就在下一个转弯处!)回复于 2002-11-23 20:04:57 得分 0
给的错误是:w.java uses or overrides a deprecated api;
recompile with -deprecation for details.Top
7 楼beyond_xiruo(CorruptionException)回复于 2002-11-23 20:16:20 得分 0
这个不是错误,是你使用了不鼓励使用的api
编译的时候这样:
javac w.java -deprecation
会给出警告,不过仍然是编译成功了的,然后运行
java w
就可以了Top




