一个简单问题!!!

applezh 2003-09-12 09:40:29
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

/**
A frame with a button panel
*/
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(WIDTH, HEIGHT);

// add panel to frame

ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}

public static final int WIDTH = 300;
public static final int HEIGHT = 200;
}

/**
A panel with three buttons.
*/
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
// create buttons

JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");

// add buttons to panel

add(yellowButton);
add(blueButton);
add(redButton);

// create button actions

ColorAction yellowAction = new ColorAction(Color.yellow);
ColorAction blueAction = new ColorAction(Color.blue);
ColorAction redAction = new ColorAction(Color.red);

// associate actions with buttons

yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}

/**
An action listener that sets the panel's background color.
*/
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}

public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
repaint();
}

private Color backgroundColor;
}
}

上面的这段程序中的ColorAction类是内部类
如果我想把ColorAction类改成外部类 这段程序该怎么改写???
...全文
38 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
applezh 2003-09-13
  • 打赏
  • 举报
回复
HELP!!!
JavaBoyCaoJi 2003-09-12
  • 打赏
  • 举报
回复
up
applezh 2003-09-12
  • 打赏
  • 举报
回复
那下面这段程序那???他的内部类引用了外部类的方法!!!
这该怎么办???

import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;

public class Sketch
{
public static void main(String[] args)
{
SketchFrame frame = new SketchFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

/**
A frame with a panel for sketching a figure
*/
class SketchFrame extends JFrame
{
public SketchFrame()
{
setTitle("Sketch");
setSize(WIDTH, HEIGHT);

// add panel to frame

SketchPanel panel = new SketchPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}

public static final int WIDTH = 300;
public static final int HEIGHT = 200;
}

/**
A panel for sketching with the keyboard.
*/
class SketchPanel extends JPanel
{
public SketchPanel()
{
last = new Point2D.Double(100, 100);
lines = new ArrayList();
KeyHandler listener = new KeyHandler();
addKeyListener(listener);
}

public boolean isFocusTraversable()
{
return true; // allow panel to get input focus
}

/**
Add a new line segment to the sketch.
@param dx the movement in x direction
@param dy the movement in y direction
*/
public void add(int dx, int dy)
{
// compute new end point
Point2D end = new Point2D.Double(last.getX() + dx,
last.getY() + dy);

// add line segment
Line2D line = new Line2D.Double(last, end);
lines.add(line);
repaint();

// remember new end point
last = end;
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;

// draw all lines
for (int i = 0; i < lines.size(); i++)
g2.draw((Line2D)lines.get(i));
}

private Point2D last;
private ArrayList lines;

private static final int SMALL_INCREMENT = 1;
private static final int LARGE_INCREMENT = 5;

private class KeyHandler implements KeyListener
{
public void keyPressed(KeyEvent event)
{
int keyCode = event.getKeyCode();

// set distance
int d;
if (event.isShiftDown())
d = LARGE_INCREMENT;
else
d = SMALL_INCREMENT;

// add line segment
if (keyCode == KeyEvent.VK_LEFT) add(-d, 0);
else if (keyCode == KeyEvent.VK_RIGHT) add(d, 0);
else if (keyCode == KeyEvent.VK_UP) add(0, -d);
else if (keyCode == KeyEvent.VK_DOWN) add(0, d);
}

public void keyReleased(KeyEvent event) {}

public void keyTyped(KeyEvent event)
{
char keyChar = event.getKeyChar();

// set distance
int d;
if (Character.isUpperCase(keyChar))
{
d = LARGE_INCREMENT;
keyChar = Character.toLowerCase(keyChar);
}
else
d = SMALL_INCREMENT;

// add line segment
if (keyChar == 'h') add(-d, 0);
else if (keyChar == 'l') add(d, 0);
else if (keyChar == 'k') add(0, -d);
else if (keyChar == 'j') add(0, d);
}
}
}
maqinglong_2000 2003-09-12
  • 打赏
  • 举报
回复
放到外面就可以了
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

62,614

社区成员

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

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