求代码解答!

windhp 2005-08-09 12:12:07
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Third
{
public static void main(String[] args)
{
MyFrame frame=new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
setSize(800,800);
setTitle("MyAction");
Container con=getContentPane();
MyPanel panel=new MyPanel();
con.add(panel);
}
}
class MyPanel extends JPanel
{
public MyPanel()
{
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton(new ImageIcon("arg.gif"));
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);
}
}
class ColorAction implements ActionListener
{
public ColorAction(Color c,);
{
cor=c;
}
public void actionPerformed(ActionEvent event)
{
MyPanel panel=new MyPanel();
panel.setBackground(cor);
}
private Color cor;
}

这个程序的问题是破坏了ColorAction的封装吧?要实现同样的目标应该怎样修改此程序呢?谢谢!
...全文
74 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangdeji 2005-08-09
  • 打赏
  • 举报
回复
好深奥
大河V5 2005-08-09
  • 打赏
  • 举报
回复
public ColorAction(Color c,);
{
cor=c;
}

---------------------------
这边不对吧
believefym 2005-08-09
  • 打赏
  • 举报
回复
给你参考一下我写的吧
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ChangeColor extends JFrame{
private Panel2 panel;
public ChangeColor(){
panel = new Panel2();

Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(panel,BorderLayout.CENTER);

this.setSize(new Dimension(400,300));
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String args[]){
new ChangeColor().setVisible(true);
}

}

class Panel2 extends JPanel implements ActionListener{
private JButton yellow,blue,red;

public Panel2(){
yellow = new JButton("yellow");
blue = new JButton("blue");
red = new JButton("red");

yellow.addActionListener(this);
blue.addActionListener(this);
red.addActionListener(this);

this.setLayout(new FlowLayout());
this.add(yellow);
this.add(blue);
this.add(red);
}

public void actionPerformed(ActionEvent e){
if(e.getSource() == yellow){
this.setBackground(Color.YELLOW);
}
else if(e.getSource() == blue){
this.setBackground(Color.BLUE);
}
else if(e.getSource() == red){
this.setBackground(Color.RED);
}
}

}
believefym 2005-08-09
  • 打赏
  • 举报
回复
//楼主的程序看了真不爽

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Third
{
public static void main(String[] args)
{
MyFrame frame=new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
setSize(800,800);
setTitle("MyAction");
Container con=getContentPane();
MyPanel panel=new MyPanel();
con.add(panel);
}
}
class MyPanel extends JPanel
{
public MyPanel()
{
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton(new ImageIcon("arg.gif"));
JButton redButton=new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction=new ColorAction(this,Color.YELLOW);
ColorAction blueAction=new ColorAction(this,Color.BLUE);
ColorAction redAction=new ColorAction(this,Color.RED);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
}
class ColorAction implements ActionListener
{
public ColorAction(MyPanel p,Color c)
{
cor=c;
parent = p;
}
public void actionPerformed(ActionEvent event)
{
//MyPanel panel=new MyPanel();
//panel.setBackground(cor);
parent.setBackground(cor);
}
private Color cor;
private MyPanel parent;
}
windhp 2005-08-09
  • 打赏
  • 举报
回复
public ColorAction(Color c)
{
cor=c;
}
我是初学者,我已经改了,结果能运行,但按了按钮后却没反应,应该是破坏了封装了啊。怎么搞的,哪位高手帮帮忙啊?
baggio5277 2005-08-09
  • 打赏
  • 举报
回复
你的代码我运行了 可能是
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);
的问题!!

我看看要是有时间,做一个和你同功能的,给你贴上来
baggio5277 2005-08-09
  • 打赏
  • 举报
回复
纠正 下
应该是 (Color c)
baggio5277 2005-08-09
  • 打赏
  • 举报
回复
public ColorAction(Color c,); // 是你这里的书写问题吗?
{
cor=c;
}

-------------------
(Color c,) ??  应该是  (Color ,c)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧