如何實現單擊一個菜單命令打開一個窗口

江南红衣 2007-07-13 06:40:42
书上写的关于监听方面的内容不是很清楚,各位熟悉这方面的朋友能否指点一下,如何实现监听.
...全文
283 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
江南红衣 2007-07-17
  • 打赏
  • 举报
回复
Thanks,
realcbb 2007-07-17
  • 打赏
  • 举报
回复
addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
.............
}
为什么监听都要加上这么一段代码,而且如果是mouse或键盘所对应的代码又不一样了,怎样才能很好的区分开如果是mouse或键盘所对应的监听代码是什么?

========================================

addActionListener(ActionListener) - 类 java.awt.Button 中的方法
添加指定的操作侦听器,以接收来自此按钮的操作事件。
addActionListener(ActionListener) - 类 java.awt.List 中的方法
添加指定的操作侦听器以从此列表接收操作事件。
addActionListener(ActionListener) - 类 java.awt.MenuItem 中的方法
添加指定的操作侦听器,以从此菜单项接收操作事件。
addActionListener(ActionListener) - 类 java.awt.TextField 中的方法
添加指定的操作侦听器,以从此文本字段接收操作事件。
addActionListener(ActionListener) - 类 javax.swing.AbstractButton 中的方法
将一个 ActionListener 添加到按钮中。
addActionListener(ActionListener) - 接口 javax.swing.ButtonModel 中的方法
向按钮添加一个 ActionListener。
addActionListener(ActionListener) - 接口 javax.swing.ComboBoxEditor 中的方法
添加一个 ActionListener。
addActionListener(ActionListener) - 类 javax.swing.DefaultButtonModel 中的方法
将一个 ActionListener 添加到按钮中。
addActionListener(ActionListener) - 类 javax.swing.JComboBox 中的方法
添加 ActionListener。
addActionListener(ActionListener) - 类 javax.swing.JFileChooser 中的方法
向文件选择器添加一个 ActionListener。
addActionListener(ActionListener) - 类 javax.swing.JTextField 中的方法
添加指定的操作侦听器以从此文本字段接收操作事件。
addActionListener(ActionListener) - 类 javax.swing.plaf.basic.BasicComboBoxEditor 中的方法

addActionListener(ActionListener) - 类 javax.swing.Timer 中的方法
将一个操作侦听器添加到 Timer。

每种事件都有对应的监听器,不清楚时可以查API文档。
江南红衣 2007-07-16
  • 打赏
  • 举报
回复
addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
.............
}
为什么监听都要加上这么一段代码,而且如果是mouse或键盘所对应的代码又不一样了,怎样才能很好的区分开如果是mouse或键盘所对应的监听代码是什么?
lanseliuying 2007-07-14
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame{
JPanel pane;
/** Creates a new instance of A */
public Test() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JbInit();
}
public void JbInit(){
pane=(JPanel)getContentPane();
JMenuBar menuBar=new JMenuBar();//菜单栏
JMenu m1=new JMenu("帮助");
JMenuItem item=new JMenuItem("关于");
item.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("关于"))
new About();
}
});
m1.add(item);
menuBar.add(m1);
pane.add(menuBar,BorderLayout.NORTH);
}
public static void main(String[]args){
Test a=new Test();
a.setSize(200,200);
a.setTitle("MyTree");
a.setVisible(true);
a.setResizable(false);
}
}
class About extends JFrame{

public About(){
setBounds(200,180,200,200);
//可以根据需要添加组件

setVisible(true);
}
}
tianwugang 2007-07-14
  • 打赏
  • 举报
回复
我也是新手,这样应该可以
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class A extends JFrame{
JPanel pane;
/** Creates a new instance of A */
public A() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JbInit();
}
public void JbInit(){
pane=(JPanel)getContentPane();
JMenuBar menuBar=new JMenuBar();//菜单栏
JMenu m1=new JMenu("帮助");
JMenuItem item=new JMenuItem("关于");
item.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("关于"))
new About();
}
});
m1.add(item);
menuBar.add(m1);
pane.add(menuBar,BorderLayout.NORTH);
}
public static void main(String[]args){
A a=new A();
a.setSize(200,200);
a.setTitle("MyTree");
a.setVisible(true);
a.setResizable(false);
}
}
class About{
JDialog dialog;
JOptionPane option;
public About(){
dialog=new JDialog(new CreateFrame(),"about",false);
option=new JOptionPane("ghhhhhhhhhjgjhghjghjghj\n"
+ "nbvhtygkughjgjhg\n"
+"opiu987hjky8087?",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION);
dialog.getContentPane().add(option);
dialog.setSize(200,180);
dialog.pack();
dialog.show();
}
}
joejoe1991 2007-07-13
  • 打赏
  • 举报
回复
定义一个实现监听器接口的类 实现里面的方法
然后再用对象的addXXXListener方法 注册监听器

应该是这样的吧。。 我新手

62,623

社区成员

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

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