新手 /java 总说 “public void actionPerformed(ActionEvent e)”找不到符号 ,可我实在是找不到错在哪
packagejianting;importjava.awt.*;importjava.awt.event.ActionListener;importjavax.swin...
package jianting;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
class Demo8_8 extends JFrame implements ActionListener
{
JPanel mp=null;
JButton jb1=null;
JButton jb2=null;
public static void main(String[] args)
{
Demo8_8 demo8_8=new Demo8_8();
}
public Demo8_8()
{
mp=new JPanel();
jb1=new JButton("balck");
jb2=new JButton("red");
this.add(jb1,BorderLayout.NORTH);
mp.setBackground(Color.black);
this.add(mp);
this.add(jb2,BorderLayout.SOUTH);
//注册监听
jb1.addActionListener(this);
jb1.setActionCommand("black");
jb2.addActionListener(this);
jb1.setActionCommand("red");
this.setSize(200,150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//对事件处理的方法
public void actionPerformed(ActionEvent e)
{
//判断那个按钮被点击
if (e.getActionCommand().equals("black"))
System.out.println("你点击了黑色按钮");
else
System.out.println("你点击了红色按钮");
}
} 展开
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
class Demo8_8 extends JFrame implements ActionListener
{
JPanel mp=null;
JButton jb1=null;
JButton jb2=null;
public static void main(String[] args)
{
Demo8_8 demo8_8=new Demo8_8();
}
public Demo8_8()
{
mp=new JPanel();
jb1=new JButton("balck");
jb2=new JButton("red");
this.add(jb1,BorderLayout.NORTH);
mp.setBackground(Color.black);
this.add(mp);
this.add(jb2,BorderLayout.SOUTH);
//注册监听
jb1.addActionListener(this);
jb1.setActionCommand("black");
jb2.addActionListener(this);
jb1.setActionCommand("red");
this.setSize(200,150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//对事件处理的方法
public void actionPerformed(ActionEvent e)
{
//判断那个按钮被点击
if (e.getActionCommand().equals("black"))
System.out.println("你点击了黑色按钮");
else
System.out.println("你点击了红色按钮");
}
} 展开
展开全部
这样写
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Demo8_8 extends JFrame implements ActionListener {
JPanel mp = null;
JButton jb1 = null;
JButton jb2 = null;
public static void main(String[] args) {
Demo8_8 demo8_8 = new Demo8_8();
}
public Demo8_8() {
mp = new JPanel();
jb1 = new JButton("balck");
jb2 = new JButton("red");
this.add(jb1, BorderLayout.NORTH);
mp.setBackground(Color.black);
this.add(mp);
this.add(jb2, BorderLayout.SOUTH);
// 注册监听
jb1.addActionListener(this);
jb1.setActionCommand("black");
jb2.addActionListener(this);
jb1.setActionCommand("red");
this.setSize(200, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
// 对事件处理的方法
public void actionPerformed(ActionEvent e) {
// 判断那个按钮被点击
if (e.getActionCommand().equals("black"))
System.out.println("你点击了黑色按钮");
else
System.out.println("你点击了红色按钮");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询