简体   繁体   English

为了在Java swing中使用setToolTipText()方法,我应该导入什么内容?

[英]What should I import in order to use the setToolTipText() method in java swing?

Please check out this code. 请检查此代码。 Why does the compiler shows a cannot find symbol error(cannot find symbol- method setToolTiptext(java.lang.String))? 为什么编译器会显示找不到符号错误(找不到符号方法setToolTiptext(java.lang.String))?

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

public class JavaToolTipExample extends JFrame
{
    private Button b;
    public JavaToolTipExample()
    {
    super("Tool Tip");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLayout(null);
    setVisible(true);
    b=new Button("Hover on me!");
    b.setToolTipText("Click!");
    add(b);
    event e=new event();
    b.addActionListener(e);
}
public class event implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        b.setText("Clicked");
    }
}
public static void main(String[]args)
 {
    JavaToolTipText gui=new JavaToolTipText();
 }
}

Change: 更改:

private Button b;

&

b=new Button("Hover on me!");

To: 至:

private JButton b;

&

b=new JButton("Hover on me!");

A JButton inherits the method from JComponent.setToolTipText(String) , whereas Button is an AWT component. JButtonJComponent.setToolTipText(String)继承该方法,而Button是AWT组件。

There is no setToolTipText method in java.awt.Button , which presumably is what you are using here. java.awt.Button没有setToolTipText方法,大概是您在此处使用的方法。 You can check the docs . 您可以检查文档 It does not inherit that method from Component either. 它也不从Component继承该方法。

I think what you meant is a javax.swing.JButton . 我认为您的意思是一个javax.swing.JButton Try to use that instead. 尝试改用它。 Here is the docs for that if you want. 如果需要的话,这里是文档

private JButton b; // This line
public JavaToolTipExample()
{
super("Tool Tip");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLayout(null);
setVisible(true);
b=new JButton("Hover on me!"); // And this line
b.setToolTipText("Click!");
add(b);
event e=new event();
b.addActionListener(e);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM