import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameDemo extends JFrame implements ActionListener{ /**
* @param args
*/
public FrameDemo(){
JButton button1= new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
Container container = getContentPane();
container.setLayout(new FlowLayout());
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
getContentPane().add(button1);
getContentPane().add(button2);
getContentPane().add(button3);
getContentPane().add(button4);
getContentPane().add(button5);
getContentPane().add(button6);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
container.add(p1);
container.add(p2);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== button1)
System.out.println("Button1 is clicked");
if(e.getSource()== button2)
System.out.println("Button1 is clicked");
if(e.getSource()== button3)
System.out.println("Button1 is clicked");
if(e.getSource()== button4)
System.out.println("Button1 is clicked");
if(e.getSource()== button5)
System.out.println("Button1 is clicked");
if(e.getSource()== button6)
System.out.println("Button1 is clicked");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FrameDemo frame = new FrameDemo();
frame.setSize(300,400);
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
为什么报错,说
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== button1)
System.out.println("Button1 is clicked");
if(e.getSource()== button2)
System.out.println("Button1 is clicked");
if(e.getSource()== button3)
System.out.println("Button1 is clicked");
if(e.getSource()== button4)
System.out.println("Button1 is clicked");
if(e.getSource()== button5)
System.out.println("Button1 is clicked");
if(e.getSource()== button6)
System.out.println("Button1 is clicked");
}
中的buttoni(i=1,2.....)不能解释
import java.awt.event.*;
import javax.swing.*;
public class FrameDemo extends JFrame implements ActionListener{ /**
* @param args
*/
public FrameDemo(){
JButton button1= new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
Container container = getContentPane();
container.setLayout(new FlowLayout());
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
getContentPane().add(button1);
getContentPane().add(button2);
getContentPane().add(button3);
getContentPane().add(button4);
getContentPane().add(button5);
getContentPane().add(button6);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
container.add(p1);
container.add(p2);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== button1)
System.out.println("Button1 is clicked");
if(e.getSource()== button2)
System.out.println("Button1 is clicked");
if(e.getSource()== button3)
System.out.println("Button1 is clicked");
if(e.getSource()== button4)
System.out.println("Button1 is clicked");
if(e.getSource()== button5)
System.out.println("Button1 is clicked");
if(e.getSource()== button6)
System.out.println("Button1 is clicked");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FrameDemo frame = new FrameDemo();
frame.setSize(300,400);
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
为什么报错,说
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== button1)
System.out.println("Button1 is clicked");
if(e.getSource()== button2)
System.out.println("Button1 is clicked");
if(e.getSource()== button3)
System.out.println("Button1 is clicked");
if(e.getSource()== button4)
System.out.println("Button1 is clicked");
if(e.getSource()== button5)
System.out.println("Button1 is clicked");
if(e.getSource()== button6)
System.out.println("Button1 is clicked");
}
中的buttoni(i=1,2.....)不能解释
把这些作为类的私有变量就可以了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameDemo extends JFrame implements ActionListener{ /**
* @param args
*/
static JButton button1= new JButton("button1");
static JButton button2 = new JButton("button2");
static JButton button3 = new JButton("button3");
static JButton button4 = new JButton("button4");
static JButton button5 = new JButton("button5");
static JButton button6 = new JButton("button6");
public FrameDemo(){
Container container = getContentPane();
container.setLayout(new FlowLayout());
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
getContentPane().add(button1);
getContentPane().add(button2);
getContentPane().add(button3);
getContentPane().add(button4);
getContentPane().add(button5);
getContentPane().add(button6);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
container.add(p1);
container.add(p2);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== button1)
System.out.println("Button1 is clicked");
if(e.getSource()== button2)
System.out.println("Button2 is clicked");
if(e.getSource()== button3)
System.out.println("Button3 is clicked");
if(e.getSource()== button4)
System.out.println("Button4 is clicked");
if(e.getSource()== button5)
System.out.println("Button5 is clicked");
if(e.getSource()== button6)
System.out.println("Button6 is clicked");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FrameDemo frame = new FrameDemo();
frame.setSize(300,400);
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}