Name: rk38400 Date: 04/09/98
System is JDK 1.1.5, Swing 1.0, Solaris 2.5. The following program doesn't
behave as expected.
Clicking on the button generates the message 'button'. Clicking in the
panel generates the message 'mouse click'. Selecting the menu item 'Item'
*sometimes* generates the messages 'menu item' *and* 'mouse click'. It
seems to happen very often when the menu item is first selected. After
a number of selections it then reverts to the behaviour I'd expect,
only printing 'menu item'. Leaving the program quiescent for a while
causes the wrong behaviour to reappear. If the window is resized so
that a heavyweight menu is used the behaviour is as expected.
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
public class menubug extends JFrame {
public menubug() {
JPanel panel = new JPanel(new BorderLayout()) ;
getContentPane().add(panel) ;
JMenuBar mb = new JMenuBar() ;
JMenu file = new JMenu("File") ;
mb.add(file) ;
JMenuItem item = new JMenuItem("Item") ;
file.add(item) ;
item.addActionListener(new ItemListener()) ;
JMenuItem exit = new JMenuItem("Exit") ;
file.add(exit) ;
exit.addActionListener(new ExitListener()) ;
JPanel panel2 = new JPanel() ;
panel2.addMouseListener(new MyMouseAdapter()) ;
JButton button = new JButton("Button") ;
button.addActionListener(new MyButtonListener()) ;
panel2.add(button) ;
panel.add(mb, "North") ;
panel.add(panel2, "Center") ;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{System.exit(0);}}) ;
pack() ;
setBounds(200, 200, 640, 400) ;
setVisible(true) ;
}
public class ItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("menu item") ;
}
}
public class MyButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("button") ;
}
}
public class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0) ;
}
}
public class MyMouseAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
System.out.println("mouse click") ;
}
}
public static void main(String s[]) {
menubug m = new menubug() ;
}
}
(Review ID: 25984)
======================================================================