5

我正在阅读一本名为 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设置图片(我希望它有帮助) 设置

4

2 回答 2

5

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

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

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

作为参考,此示例演示了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();
            }
        });
    }
}
于 2012-04-06T00:19:59.060 回答
0

好吧好吧。我知道这已经得到了很好的回答,并且 OP 的问题是在 Mac 上,但这个答案需要分享。

如果您使用的是 Windows 7,如果您将主题设置为“经典”,则翻转效果将不起作用。将其设置为“基本”,翻转将起作用。

于 2015-04-15T23:40:17.180 回答