当前位置: 技术问答>java相关
大家帮我改下这个程序看?麻烦了!!!
来源: 互联网 发布时间:2015-11-07
本文导语: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.EventObject; public class ButtonTest2 { public static void main(String[] args) { ButtonFrame frame=new ButtonFrame(); frame.setDefaultCloseOpe...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.EventObject;
public class ButtonTest2
{
public static void main(String[] args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame2 extends JFrame
{
public ButtonFrame2()
{
setTitle("ButtonTest");
this.setSize(WIDTH,HEIGHT);
ButtonPanel2 panel=new ButtonPanel2();
Container contentpanel=this.getContentPane();
contentpanel.add(panel);
}
public final int WIDTH=300;
public final int HEIGHT=200;
}
class ButtonPanel2 extends JPanel implements ActionListener
{
public ButtonPanel2()
{
//create button
JButton yellowButton=new JButton("yellow");
JButton blueButton=new JButton("blue");
JButton redButton=new JButton("red");
//add button
this.add(yellowButton);
this.add(blueButton);
this.add(redButton);
yellowButton.addActionListener(this);
blueButton.addActionListener(this);
redButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source;
EventObject event=new EventObject(source);
Object ce=event.getSource();
if (ce=yellowButton) this.setBackground(Color.yellow);
else if (e=blueButton) this.setBackground(Color.blue);
else if (e=redButton) this.setBackground(Color.red);
}
}
最后总说yellowButton,blueButton,redButton找不到?不知道为什么。
import java.awt.event.*;
import javax.swing.*;
import java.util.EventObject;
public class ButtonTest2
{
public static void main(String[] args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame2 extends JFrame
{
public ButtonFrame2()
{
setTitle("ButtonTest");
this.setSize(WIDTH,HEIGHT);
ButtonPanel2 panel=new ButtonPanel2();
Container contentpanel=this.getContentPane();
contentpanel.add(panel);
}
public final int WIDTH=300;
public final int HEIGHT=200;
}
class ButtonPanel2 extends JPanel implements ActionListener
{
public ButtonPanel2()
{
//create button
JButton yellowButton=new JButton("yellow");
JButton blueButton=new JButton("blue");
JButton redButton=new JButton("red");
//add button
this.add(yellowButton);
this.add(blueButton);
this.add(redButton);
yellowButton.addActionListener(this);
blueButton.addActionListener(this);
redButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source;
EventObject event=new EventObject(source);
Object ce=event.getSource();
if (ce=yellowButton) this.setBackground(Color.yellow);
else if (e=blueButton) this.setBackground(Color.blue);
else if (e=redButton) this.setBackground(Color.red);
}
}
最后总说yellowButton,blueButton,redButton找不到?不知道为什么。
|
修订程序如下》:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("yellow");
JButton blueButton=new JButton("blue");
JButton redButton=new JButton("red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction=new ColorAction(Color.yellow);
ColorAction blueAction=new ColorAction(Color.blue);
ColorAction redAction=new ColorAction(Color.red);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener{
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
repaint();
}
private Color backgroundColor;
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(WIDTH,HEIGHT);
ButtonPanel panel=new ButtonPanel();
Container contentPanel=getContentPane();
contentPanel.add(panel);
}
public static final int WIDTH=300;
public static final int HEIGHT=500;
}
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("yellow");
JButton blueButton=new JButton("blue");
JButton redButton=new JButton("red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction=new ColorAction(Color.yellow);
ColorAction blueAction=new ColorAction(Color.blue);
ColorAction redAction=new ColorAction(Color.red);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener{
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
repaint();
}
private Color backgroundColor;
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(WIDTH,HEIGHT);
ButtonPanel panel=new ButtonPanel();
Container contentPanel=getContentPane();
contentPanel.add(panel);
}
public static final int WIDTH=300;
public static final int HEIGHT=500;
}
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
|
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);这句怎么报错??
Container contentpanel=this.getContentPane(); 是什么意思,哪位给解释一下。
Container contentpanel=this.getContentPane(); 是什么意思,哪位给解释一下。