为什么ActionEvent无法响应????

asssf3214 2006-11-18 02:18:18
我做的一个画板程序:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import javax.swing.*;
public class DrawBoard extends JApplet implements ActionListener{
JFrame frame;
JMenuBar menu;
JMenu file,edit,view,image,color,help;
JMenuItem newfile,openfile,copy,t,helpabout,about;
JPanel panel1,panel2,panel3;
JButton cut,pencil,gun,drawline,rectangle,polygon,circle;
JColorChooser jj;

private Container top;

public void init()
{
Container top = getContentPane();
top.setLayout(new BorderLayout());

setSize(800,800);
menu=new JMenuBar();
menu.setOpaque(true);
menu.setBackground(Color.lightGray);
menu.setPreferredSize(new Dimension(500,20));

file=new JMenu("文件(F)");
edit=new JMenu("编辑(E)");
view=new JMenu("查看(V)");
image=new JMenu("图像(I)");
color=new JMenu("颜色(C)");
help=new JMenu("帮助(H)");

newfile=new JMenuItem("新建(N) Ctrl+N");
openfile=new JMenuItem("打开(O) Ctrl+O");
copy=new JMenuItem("复制(C) Ctrl+C");
t=new JMenuItem("剪切(T) Ctrl+T");
helpabout=new JMenuItem("帮助主题(H) Ctrl+H");
about=new JMenuItem("关于(A) Ctrl+A");

file.add(newfile);
file.add(openfile);
file.add(copy);
file.add(t);
help.add(helpabout);
help.add(about);

menu.add(file);
menu.add(edit);
menu.add(view);
menu.add(image);
menu.add(color);
menu.add(help);

setJMenuBar(menu);

JButton cut=new JButton("裁剪");
JButton pencil=new JButton("铅笔");
JButton gun=new JButton("喷枪");
JButton drawline=new JButton("直线");
JButton rectangle=new JButton("矩形");
JButton polygon=new JButton("多边形");
JButton circle=new JButton("圆");

cut.addActionListener(this);
pencil.addActionListener(this);
gun.addActionListener(this);
drawline.addActionListener(this);
rectangle.addActionListener(this);
polygon.addActionListener(this);
circle.addActionListener(this);

JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(7,1,1,3));
panel1.add(cut);
panel1.add(pencil);
panel1.add(gun);
panel1.add(drawline);
panel1.add(rectangle);
panel1.add(polygon);
panel1.add(circle);

top.add(panel1,BorderLayout.WEST);

JPanel panel2=new JPanel();
JColorChooser jj=new JColorChooser();
panel2.add(jj);

top.add(panel2,BorderLayout.SOUTH);

JPanel panel3=new JPanel();
panel3.setBackground(Color.WHITE);
panel3.setSize(300,500);
top.add(panel3,BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e)
{
Object g=e.getSource();
if(g==cut||g==pencil||g==gun)
{ JOptionPane.showMessageDialog(null,"Asdf","d",JOptionPane.INFORMATION_MESSAGE);
}
}
为什么我按下哪个按钮都不弹出对话框,急啊!!!
...全文
140 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
asssf3214 2006-11-18
  • 打赏
  • 举报
回复
汗,原来是这样
搞定,谢谢!
混沌骑士 2006-11-18
  • 打赏
  • 举报
回复
哦 好像不是这个问题 你好像重复定义了:
JButton cut,pencil,gun,drawline,rectangle,polygon,circle;

JButton cut=new JButton("裁剪");
JButton pencil=new JButton("铅笔");
JButton gun=new JButton("喷枪");
JButton drawline=new JButton("直线");
JButton rectangle=new JButton("矩形");
JButton polygon=new JButton("多边形");
JButton circle=new JButton("圆");
混沌骑士 2006-11-18
  • 打赏
  • 举报
回复
Object g=e.getSource();
改成:JButton bg = (JButton)e.getSource();然后用bg去和cut、pencil比较
杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 1/19 页 1.1 应用 Java Swing GUI 组件技术实现课程选课系统 GUI 窗口(第 1 部分) 1.1.1 实现选课系统的主窗口 SystemMainWindow 的 UI 界面 1、添加一个主窗口 类名称为 SystemMainWindow,包名称为 com.px1987.course.ui 将出现窗口界面的创建结果。 2、在主窗口内添加 5 个功能按钮 (1)添加 JButton 组件 杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 2/19 页 (2)重复添加 5 个 JButton 组件 杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 3/19 页 3、分别设置 5 个按钮的属性 (1)提示文字改变为如下的文字信息 杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 4/19 页 (2)改变各个按钮的对象名称以方便后面的编程实现 5 个按钮的对象名称方便为: 1) 系统注册按钮:systemRegisterButton 2) 系统登录按钮:systemLoginButton 3) 在线选课按钮:selectCourseButton 4) 退选课程按钮:removeCourseButton 5) 退出系统按钮:exitSystemButton (3)修改后的结果如下 4、为 5 个按钮添加事件响应 (1)Action 事件 杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 5/19 页 (2)分别创建出 5 个不同的事件响应方法 5、编程 5 个不同按钮的事件响应方法的功能实现代码 StudentManageInter oneStudentManage=null; CourseManageInter oneCourseManage=null; public SystemMainWindow() { initComponents(); /** * 创建 StudentManageImple 和 CourseManageImple 的对象实例 */ oneStudentManage=new StudentManageImple(); 杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 6/19 页 oneCourseManage=new CourseManageImple(); } /** 退出系统按钮的事件响应 */ private void exitSystemButtonActionPerformed(java.awt.event.ActionEvent evt) { this.dispose(); System.exit(0); } /** 退选课程按钮的事件响应 */ private void removeCourseButtonActionPerformed( java.awt.event.ActionEvent evt) { oneCourseManage.removeOneCourse(); } /** 在线选课按钮的事件响应 */ private void selectCourseButtonActionPerformed( java.awt.event.ActionEvent evt) { oneCourseManage.addOneCourse(); } /** 系统登录按钮的事件响应 */ private void systemLoginButtonActionPerformed(java.awt.event.ActionEvent evt) { oneStudentManage.systemLogin(); 杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 7/19 页 } /** 系统注册按钮的事件响应 */ private void systemRegisterButtonActionPerformed(java.awt.event.ActionEvent evt) { oneStudentManage.systemRegister(); } 6、测试主窗口内的"退出系统"按钮的正确性 点击"退出系统"按钮后,将关闭主窗口,同时退出本系统程序。 1.1.2 课程选课系统注册窗口的 UI 实现 1、打开 Form 设计器 (1)设置包名称为 com.px1987.course.ui,类名称为 StudentRegisterWin

62,614

社区成员

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

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