看一下这个简单的Java程序,哪儿错了呢

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class Exam extends JFrame implements ActionListener{
JButton[] b = {new JButton("红色"),new JButton("绿色"),new JButton("蓝色")};
Container con ;
public static void main(String[] args){
Exam frm = new Exam("按钮事件");
frm.setBounds(100,100,300,200);
frm.setDefaultCloseOpetion(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
Exam(String s){
setTitle(s);
con = getContentPane();
add(b[0]); add(b[1]); add(b[2]);
b[0].addActionListener(this);
b[1].addActionListener(this);
b[2].addActionListener(this);
}
public void performed(ActionEvent e){
if(e.getSource()==b[0])
con.setBackground(Color.red);
else if(e.getSource==b[1])
con.setBackground(Color.green);
else
con.setBackground(Color.blue);
}
}

效果如下

修改的错误

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Exam extends JFrame implements ActionListener {
JButton[] b = { new JButton("红色"), new JButton("绿色"), new JButton("蓝色") };
Container con;

public static void main(String[] args) {
Exam frm = new Exam("按钮事件");
frm.setBounds(100, 100, 300, 200);
//frm.setDefaultCloseOpetion(JFrame.EXIT_ON_CLOSE);//这里拼写错误
  frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}

Exam(String s) {
setTitle(s);
con = getContentPane();
add(b[0]);
add(b[1]);
add(b[2]);
b[0].addActionListener(this);
b[1].addActionListener(this);
b[2].addActionListener(this);
setLayout(new FlowLayout()); // 把布局修改成流式布局 从左到右排列组件
}


public void performed(ActionEvent e) {//方法名写错

}
//actionPerformed 才是正确的拼写
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b[0])
con.setBackground(Color.red);
else if (e.getSource() == b[1])// else if (e.getSource == b[1]) getSource()是方法要加括号
con.setBackground(Color.green);
else
con.setBackground(Color.blue);
}
}

追问

我刚才改了部分,但是还是运行错误

嗯嗯,我现在改正了,知道了,谢谢

追答

你需要全部修改啊 . 照上面的代码, 全部修改

温馨提示:内容为网友见解,仅供参考
第1个回答  2017-06-21
//已修改

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Exam extends JFrame implements ActionListener
{
JButton[] b = { new JButton ("红色"), new JButton ("绿色"), new JButton ("蓝色") };
Container con;

public static void main ( String[] args )
{
Exam frm = new Exam ("按钮事件");
frm.setBounds (100, 100, 300, 200);
frm.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frm.setVisible (true);
}

Exam ( String s )
{
setTitle (s);
con = getContentPane ();
add (b[0]);
add (b[1]);
add (b[2]);
b[0].addActionListener (this);
b[1].addActionListener (this);
b[2].addActionListener (this);
}

@Override
public void actionPerformed ( ActionEvent e )
{
if (e.getSource () == b[0])
con.setBackground (Color.red);
else if (e.getSource () == b[1])
con.setBackground (Color.green);
else
con.setBackground (Color.blue);
}
}

追问

我刚才改了部分,但是还是运行错误

相似回答
大家正在搜