当前位置: 技术问答>java相关
真正理解JAVA的高手请进来挑战挑战!
来源: 互联网 发布时间:2015-06-11
本文导语: 我们知道菜单子项类为MenuItem,现在我们要做的是写一个类myMenuItem,要求继承MenuItem,并增加一个功能,就是当鼠标在myMenuItem类实例化的对象上时,状态栏显示该对象的text.假设状态栏对象为statusbar,菜单子项对象为TestM...
我们知道菜单子项类为MenuItem,现在我们要做的是写一个类myMenuItem,要求继承MenuItem,并增加一个功能,就是当鼠标在myMenuItem类实例化的对象上时,状态栏显示该对象的text.假设状态栏对象为statusbar,菜单子项对象为TestMenuItem.
Label statusbar = new Label();
myMenuItem testMenuItem = new myMenuItem();
怎么实现?
这是我突然间想到的,但自己没写出来,只有个思路,不知道对否,请高手共同探讨。
思路:在myMenuItem类中增加鼠标事件,使鼠标在myMenuItem类实例化的对象上时就能产生一条消息,使状态栏的text等于myMenuItem类实例化的对象的text.
不知道大家是否看明白,探讨探讨!
Label statusbar = new Label();
myMenuItem testMenuItem = new myMenuItem();
怎么实现?
这是我突然间想到的,但自己没写出来,只有个思路,不知道对否,请高手共同探讨。
思路:在myMenuItem类中增加鼠标事件,使鼠标在myMenuItem类实例化的对象上时就能产生一条消息,使状态栏的text等于myMenuItem类实例化的对象的text.
不知道大家是否看明白,探讨探讨!
|
Have a look at this, I think the problem is solved there:
http://www.javaworld.com/javaworld/javaqa/1999-10/01-qa-menuhelp.html
http://www.javaworld.com/javaworld/javaqa/1999-10/01-qa-menuhelp.html
|
思路可行。
|
Component类里的addMouseListener方法,看看对你有没有帮助。
/**
* Adds the specified mouse listener to receive mouse events from
* this component.
* If listener l is null,
* no exception is thrown and no action is performed.
*
* @param l the mouse listener
* @see java.awt.event.MouseEvent
* @see java.awt.event.MouseListener
* @see #removeMouseListener
* @see #getMouseListeners
* @since JDK1.1
*/
public synchronized void addMouseListener(MouseListener l) {
if (l == null) {
return;
}
mouseListener = AWTEventMulticaster.add(mouseListener,l);
newEventsOnly = true;
// if this is a lightweight component, enable mouse events
// in the native container.
if (peer instanceof LightweightPeer) {
parent.proxyEnableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
}
/**
* Adds the specified mouse listener to receive mouse events from
* this component.
* If listener l is null,
* no exception is thrown and no action is performed.
*
* @param l the mouse listener
* @see java.awt.event.MouseEvent
* @see java.awt.event.MouseListener
* @see #removeMouseListener
* @see #getMouseListeners
* @since JDK1.1
*/
public synchronized void addMouseListener(MouseListener l) {
if (l == null) {
return;
}
mouseListener = AWTEventMulticaster.add(mouseListener,l);
newEventsOnly = true;
// if this is a lightweight component, enable mouse events
// in the native container.
if (peer instanceof LightweightPeer) {
parent.proxyEnableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
}
|
重载构造函数!
private Label status = null;
myMenuItem(Label status);
{
this.status = tstus;
}
当然每次向status写信息时要判断status 是否为null!
其他同上!
private Label status = null;
myMenuItem(Label status);
{
this.status = tstus;
}
当然每次向status写信息时要判断status 是否为null!
其他同上!
|
菜单有一个 Menu…… 什么事件的,可以在进入菜单项的时候写代码,晚上回去写例子
|
可以用swing 中的 JMenuItem .
刚刚写的,用Jbuilder 3.0 自动生成的。
自己改了一点。
符合你的要求。可以运行。 jbuiler3.0 + jdk1.2
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame {
JMenuBar menuBar1 = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
JMenu menuHelp = new JMenu();
JMenuItem menuHelpAbout = new JMenuItem();
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
statusBar.setText(" ");
menuFile.setText("File");
menuFileExit.setText("Exit");
menuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileExit_actionPerformed(e);
}
});
menuHelp.setText("Help");
menuHelpAbout.setText("About");
menuHelpAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
helpAbout_actionPerformed(e);
}
});
menuHelpAbout.addMouseListener (new MouseListener() {
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){
helpAbout_mouseEnteredActionPerformed(e);
}
public void mouseExited(MouseEvent e){
helpAbout_mouseExitedActionPerformed(e);
}
});
menuFile.add(menuFileExit);
menuHelp.add(menuHelpAbout);
menuBar1.add(menuFile);
menuBar1.add(menuHelp);
this.setJMenuBar(menuBar1);
this.getContentPane().add(statusBar, BorderLayout.SOUTH);
}
//File | Exit action performed
public void fileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
//Help | About action performed
public void helpAbout_actionPerformed(ActionEvent e) {
}
//Help | About mouse performed
public void helpAbout_mouseEnteredActionPerformed(MouseEvent e) {
statusBar.setText ("Monse Enter - "+menuHelpAbout.getText ());
}
public void helpAbout_mouseExitedActionPerformed(MouseEvent e) {
statusBar.setText ("Monse Exit - "+menuHelpAbout.getText ());
}
//Overridden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
fileExit_actionPerformed(null);
}
}
}
第二个文件:
package csdn;
import javax.swing.UIManager;
public class Application1 {
boolean packFrame = false;
//Construct the application
public Application1() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
frame.pack();
else
frame.validate();
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
}
new Application1();
System.out.println("End");
}
}
可以运行,试试看啦。
刚刚写的,用Jbuilder 3.0 自动生成的。
自己改了一点。
符合你的要求。可以运行。 jbuiler3.0 + jdk1.2
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame {
JMenuBar menuBar1 = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
JMenu menuHelp = new JMenu();
JMenuItem menuHelpAbout = new JMenuItem();
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
statusBar.setText(" ");
menuFile.setText("File");
menuFileExit.setText("Exit");
menuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileExit_actionPerformed(e);
}
});
menuHelp.setText("Help");
menuHelpAbout.setText("About");
menuHelpAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
helpAbout_actionPerformed(e);
}
});
menuHelpAbout.addMouseListener (new MouseListener() {
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){
helpAbout_mouseEnteredActionPerformed(e);
}
public void mouseExited(MouseEvent e){
helpAbout_mouseExitedActionPerformed(e);
}
});
menuFile.add(menuFileExit);
menuHelp.add(menuHelpAbout);
menuBar1.add(menuFile);
menuBar1.add(menuHelp);
this.setJMenuBar(menuBar1);
this.getContentPane().add(statusBar, BorderLayout.SOUTH);
}
//File | Exit action performed
public void fileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
//Help | About action performed
public void helpAbout_actionPerformed(ActionEvent e) {
}
//Help | About mouse performed
public void helpAbout_mouseEnteredActionPerformed(MouseEvent e) {
statusBar.setText ("Monse Enter - "+menuHelpAbout.getText ());
}
public void helpAbout_mouseExitedActionPerformed(MouseEvent e) {
statusBar.setText ("Monse Exit - "+menuHelpAbout.getText ());
}
//Overridden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
fileExit_actionPerformed(null);
}
}
}
第二个文件:
package csdn;
import javax.swing.UIManager;
public class Application1 {
boolean packFrame = false;
//Construct the application
public Application1() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
frame.pack();
else
frame.validate();
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
}
new Application1();
System.out.println("End");
}
}
可以运行,试试看啦。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。