Java isRollover()方法在我的swing应用程序中不会产生事件 [英] Java isRollover() method does not produce an event in my swing application
问题描述
我正在阅读一本很棒的书,名为《 Swing:初学者指南》. 书中有这段代码可创建按钮和标签,以提醒按钮状态更改事件:
I am reading a great book called Swing: A Beginner's guide. There is this code in the book that creates a button and a label that alerts on button's state change events :
//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. 我应该把这个摇摆问题归咎于Lion还是我做错了什么? 谢谢.
Everything is working fine except for the rollover event. The underlying operating system is Mac OS Lion. Should I blame Lion for this swing problem or am I doing something wrong? Thank you.
更新1:我的neatbeans设置图片(希望对您有所帮助)
Update 1 : My neatbeans settings picture (I hope it helps)
推荐答案
在具有Java版本1.6.0_26
的Leopard上测试的代码如下所示.后面的</html>
标签修复了悬停时的突出故障.
Code as tested on Leopard with Java version 1.6.0_26
shown below. The trailing </html>
tag fixed a highlighting glitch on rollover.
附录:使用下面的更新示例,添加setRolloverEnabled(true)
可使模型按预期工作.有趣的是,当isRollover()
为true
时,Mac UI委托com.apple.laf.AquaButtonUI
不执行任何操作.如果对您的应用程序很重要,则当以下谓词为true
时,您可以采取所需的操作:
Addendum: Using the updated example below, adding setRolloverEnabled(true)
allows the model to work as expected. Interestingly, the Mac UI delegate, com.apple.laf.AquaButtonUI
, does nothing when isRollover()
is true
. If it's important to your application, you can take the desired action when the following predicate is 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();
}
});
}
}
这篇关于Java isRollover()方法在我的swing应用程序中不会产生事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!