在JOptionPane.showMessageDialog(「Some Text」);已經顯示,工具提示被禁用,我無法弄清楚如何重新啓用它們。下面是一些代碼演示問題:在JOptionPane.showMessageDialog顯示後,在Java中重新啓用ToolTips
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToolTipError extends JPanel{
JButton button;
public ToolTipError() {
button = new JButton("ToolTipHere");
button.setToolTipText("This is Java! There is no help");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
add(button);
}
void buttonActionPerformed(ActionEvent env) {
System.out.println("Button pushed."); //TEST
JOptionPane.showMessageDialog(null, "This disables ToolTips!");
// This does not re-enable the tool tip:
button.setToolTipText("This is Java! There is no help");
// Nor does this:
ToolTipManager.sharedInstance().setEnabled(true);
}
void start() {
//Create and set up the window.
JFrame frame = new JFrame("ToolTipError");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
ToolTipError newContentPane = new ToolTipError();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
ToolTipError tte = new ToolTipError();
tte.start();
}
}
注意,顯示的對話框後設置的ToolTipText不會重新啓用工具提示,也不會重新啓用它們時,ToolTipManager。
請注意,如果您將窗口移動到屏幕上,則工具提示會奇蹟般地重新開始工作!?!
上面的代碼應該爲你編譯「開箱即用」,如果你想玩的話。
在此先感謝!
即使我刪除了'showMessageDialog(...)'下面的所有內容,這對我來說都很好,所以消息彈出窗口又彈出可以看到工具提示,如果我把鼠標放在'JButton'上。希望你不要期待它在屏幕上顯示'JOptionPane'時顯示一個工具提示,因爲它是模態的,所討論的'JFrame'不會獲得焦點。 – 2012-07-08 13:46:42
無法重現 - 設置工具提示一次就可以正常工作(jdk 7/win7) – kleopatra 2012-07-08 13:49:04
@nIcEcOw +1思考不支持的期望:-)雖然顯示了原始工具提示,但「不支持」稍微出乎意料。 – kleopatra 2012-07-08 13:56:22