Java isRollover()方法在我的swing应用程序中不会产生事件

时间:2012-04-05 22:18:15

标签: java swing jbutton changelistener

我正在读一本名为Swing:A Beginner's guide的好书。 书中有这样的代码,它创建了一个按钮和一个标签,用于警告按钮的状态变化事件:

//Demonstrate a change listener and the button model

package swingexample2_6;

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

public class ChangeDemo {

    JButton jbtn;
    JLabel jlab;

    public ChangeDemo() {
        //Create a new JFrame container
        JFrame jfrm = new JFrame("Button Change Events");

        //Specify FlowLayout for the layout manager
        jfrm.getContentPane().setLayout(new FlowLayout());

        //Give the frame an initial size
        jfrm.setSize(250, 160);

        //Terminate the program when the user closes the application
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create an empty label
        jlab = new JLabel();

        //Make a button
        jbtn = new JButton("Press for Change Event Test");

        //--Add change listener
        jbtn.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent ce) {
                ButtonModel mod = jbtn.getModel();
                String what = "";

                if (mod.isEnabled()) {
                    what += "Enabled<br>";
                }
                if (mod.isRollover()) {
                    what += "Rollover<br>";
                }
                if (mod.isArmed()) {
                    what += "Armed<br>";
                }
                if (mod.isPressed()) {
                    what += "Pressed<br>";
                }

                //Notice that this label's text is HTML
                jlab.setText("<html>Current stats:<br>" + what);
            }
        });


        //Add the components to the content pane
        jfrm.getContentPane().add(jbtn);
        jfrm.getContentPane().add(jlab);

        //Display the frame
        jfrm.setVisible(true);
    }

    public static void main(String[] args) {
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ChangeDemo();
            }
        });
    }
}

除了翻转事件之外,一切正常。 底层操作系统是Mac OS Lion。 我应该责怪狮子这个摆动问题还是我做错了什么? 谢谢。

更新1:我的neatbeans设置图片(我希望它有帮助) settings http://i44.tinypic.com/vdzg92.png

2 个答案:

答案 0 :(得分:5)

在Leopard上测试的代码,Java版本1.6.0_26如下所示。尾随</html>标记修复了翻转时的突出显示故障。

附录:使用下面更新的示例,添加setRolloverEnabled(true)可使模型按预期工作。有趣的是,当com.apple.laf.AquaButtonUIisRollover()时,Mac UI委托true不执行任何操作。如果它对您的应用程序很重要,那么当以下谓词为true时,您可以采取所需的操作:

System.getProperty("os.name").startsWith("Mac OS X")

作为参考,此example演示了setRolloverIcon()

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

public class ChangeDemo {

    private JButton jbtn;
    private JLabel jlab;

    public ChangeDemo() {
        JFrame jfrm = new JFrame("Button Change Events");
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrm.setLayout(new GridLayout(0, 1));
        jlab = new JLabel("", JLabel.CENTER);
        jbtn = new JButton("Press for Change Event Test");
        jbtn.setRolloverEnabled(true);

        jbtn.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent ce) {
                ButtonModel mod = jbtn.getModel();
                String what = "";

                if (mod.isEnabled()) {
                    what += "Enabled<br>";
                }
                if (mod.isRollover()) {
                    what += "Rollover<br>";
                }
                if (mod.isArmed()) {
                    what += "Armed<br>";
                }
                if (mod.isPressed()) {
                    what += "Pressed<br>";
                }

                //Notice that this label's text is HTML
                jlab.setText("<html>Current stats:<br>" + what + "</html>");
            }
        });

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(50, 10, 0, 10));
        panel.add(jbtn);
        jfrm.add(panel);
        jfrm.add(jlab);

        jfrm.pack();
        jfrm.setLocationRelativeTo(null);
        jfrm.setVisible(true);
    }

    public static void main(String[] args) {
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ChangeDemo changeDemo = new ChangeDemo();
            }
        });
    }
}

答案 1 :(得分:0)

好的,好的。我知道这已经得到了很好的回答,OP的问题出现在Mac上,但这个答案需要sharin&#39;

如果您使用的是Windows 7,如果您将主题设置为&#34; Classic&#34;,则翻转效果将无法正常工作。将其设置为&#34; Basic&#34;和翻转工作。