我应该导入什么才能在java swing中使用setToolTipText()方法?

时间:2021-06-01 07:06:52

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();
 }
}

2 个解决方案

#1


2  

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.

JButton从JComponent.setToolTipText(String)继承该方法,而Button是AWT组件。

#2


0  

There is no setToolTipText method in java.awt.Button, which presumably is what you are using here. You can check the docs. It does not inherit that method from Component either.

java.awt.Button中没有setToolTipText方法,大概就是你在这里使用的方法。您可以查看文档。它也不会从Component继承该方法。

I think what you meant is a javax.swing.JButton. Try to use that instead. Here is the docs for that if you want.

我想你的意思是javax.swing.JButton。尝试使用它。如果您愿意,可以使用以下文档。

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);

#1


2  

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.

JButton从JComponent.setToolTipText(String)继承该方法,而Button是AWT组件。

#2


0  

There is no setToolTipText method in java.awt.Button, which presumably is what you are using here. You can check the docs. It does not inherit that method from Component either.

java.awt.Button中没有setToolTipText方法,大概就是你在这里使用的方法。您可以查看文档。它也不会从Component继承该方法。

I think what you meant is a javax.swing.JButton. Try to use that instead. Here is the docs for that if you want.

我想你的意思是javax.swing.JButton。尝试使用它。如果您愿意,可以使用以下文档。

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);