当前位置: 技术问答>java相关
关键字:button mouseclicked 在线等待
来源: 互联网 发布时间:2015-11-12
本文导语: 有2个button ,在public void mouseClicked(MouseEvent e)里 怎么判断是哪个button ?最好用事例说明。 我这么做为什么不对? public void mouseClicked(MouseEvent e){ Button jb=(Button)e.getSource(); if (jb==b1) { try{ OutputStream os=ne...
有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()+"rn"+s);
}catch(Exception e1){}
}
}
谢谢!!在线等待,马上给分!!
怎么判断是哪个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()+"rn"+s);
}catch(Exception e1){}
}
}
谢谢!!在线等待,马上给分!!
|
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();
}
}
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();
}
}
|
最好是给两个button个加一个监听addActionListener(),然后写一个actionPerformed(ActionEvent e) 函数,在函数里面判断是对那个button的操作方法是:
if (e.getActionCommand()=="*****")
then
{}
else
{}
if (e.getActionCommand()=="*****")
then
{}
else
{}