标签JLable上可以添加图像,当鼠标停留在标签上时,可以显示一段提示文字。
package first;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class LbTest extends JFrame implements ActionListener
{
public LbTest(String s) {
setSize(900,900);
setVisible(true);
setTitle(s);
setLayout(new FlowLayout());
ImageIcon icon1=new ImageIcon("./image/1.jpg");///图片的路径要写全
ImageIcon icon2=new ImageIcon("./image/2.jpg");
ImageIcon icon3=new ImageIcon("./image/3.jpg");
JButton jbtn=new JButton("我是按钮",icon1);
jbtn.setRolloverIcon(icon2);///当鼠标停在按钮上时变换图标
JLabel jlb=new JLabel("我是标签",icon3,SwingConstants.CENTER);
jlb.setToolTipText("QQ头像");///当鼠标停留在标签上时显示提示文本
add(jbtn);
add(jlb);
jbtn.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
validate();
// TODO Auto-generated constructor stub
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JInternalFrame in_Window;///声明内部窗体对象
in_Window=new JInternalFrame("内部窗体",true,true,true,true);
in_Window.setSize(250,200);
in_Window.setVisible(true);
add(in_Window);
JTextArea text =new JTextArea(5,15);///创建文本区对象
in_Window.add(text,BorderLayout.CENTER);
}
}
public class Test
{
public static void main(String[] args)
{
LbTest winLbTest=new LbTest("有图标的按钮和标签");
}
}