package basic;import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.plaf.metal.MetalIconFactory;
import javax.swing.table.DefaultTableModel;public class TestToolBar extends JFrame {
    TestToolBar() {
        super("Test ToolBar");        DefaultTableModel dm = new DefaultTableModel();
        dm.setDataVector(new Object[][] { { "a", "b", "c" }, { "A", "B", "C" } }, new Object[] { "1st\nalpha",
                "2nd\nbeta", "3rd\ngamma" });        JTable table = new JTable(dm);
        JScrollPane scroll = new JScrollPane(table);
        JToolBar toolBar = new JToolBar();
        JButton button1 = new JButton();
        button1.setPreferredSize(new Dimension(48,48));
        button1.setIcon(MetalIconFactory.getTreeComputerIcon());
        button1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "这里是按钮1的点击时间处理");
            }
        });
        
        JButton button2 = new JButton();
        button2.setPreferredSize(new Dimension(48,48));
        button2.setIcon(MetalIconFactory.getFileChooserHomeFolderIcon());
        button2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "这里是按钮2的点击时间处理");
            }
        });
        
        JButton button3 = new JButton();
        button3.setPreferredSize(new Dimension(48,48));
        ImageIcon iconBtn3 = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/Images/05 (10).gif"))); 
        button3.setIcon(iconBtn3);
        button3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "这里是按钮3的点击时间处理");
            }
        });
        
        JButton button4 = new JButton();
        button4.setPreferredSize(new Dimension(48,48));
        ImageIcon iconBtn4 = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/Images/05 (1).gif"))); 
        button4.setIcon(iconBtn4);
        button4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "这里是按钮4的点击时间处理");
            }
        });
        
        toolBar.add(button1);
        toolBar.add(button2);
        toolBar.add(button3);
        toolBar.add(button4);
        
        add(toolBar, BorderLayout.PAGE_START);
        this.getContentPane().add(scroll);
        this.setSize(400, 500);
        this.setVisible(true);
    }
        public static void main(String[] args) {
        TestToolBar frame = new TestToolBar();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
我想做到这样的效果, 鼠标划过toolbar上的jbutton ,jbutton是图片做成的小图标,可以提示一点文字信息 比如是"用户登入";
点击下去的话,会进入另一个界面;这个事件 我不知道该是什么;我原来是这样的:
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="用户登录")
{
}不知道actionPerformed方法 该怎么写,才能产生这样的效果

解决方案 »

  1.   

    可以通过getToolTipText获取按钮的提示文本
    public String getToolTipText()返回通过 setToolTipText 所设置的工具提示字符串。 返回:
    工具提示文本工具栏按钮对象.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    //如果楼主能确定点击该按钮后的事件处理是什么,可以直接在下面写相关的代码就可以
                }
            });
      

  2.   

    鼠标划过toolbar上的jbutton ,jbutton是图片做成的小图标,可以提示一点文字信息 比如是"用户登入"; 
    这个问题解决了; 
    谢谢
    的确是这个方法:setToolTipText()按下这个按钮, 我还要试试先
      

  3.   

    这是一个没有 名字的Jbutton button 
    JButton button2 = new JButton();
    我无法获得它的名字:
    这样我就没法确定 按下的是哪个 button了根据getToolTipText()这个不同 有方法吗?if(e.getActionCommand()=="用户登录")
            {
                        }
      

  4.   

    主类按如下定义,不必每次新建一个 ActionListener
    public class TestToolBar extends JFrame implements ActionListener {按钮可以如下设定命令字符串,然后在 actionPerformed 里面就可以识别了
    JButton button1 = new JButton();
    button1.setActionCommand("用户登录");  public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("用户登录")) {
          // 这是 button1 的点击事件
        }
      }
      

  5.   

    主类按如下定义,不必每次新建一个 ActionListener
    public class TestToolBar extends JFrame implements ActionListener { 
    ...
    ...
        JButton button1 = new JButton();
        button1.setActionCommand("用户登录");
        button1.addActionListener(this); //不需要新建一个ActionListener
      

  6.   

    如果只要实现 鼠标在按钮上时,弹出一个文字提示,setToolTipText()  这个方法就可以了,
    如果要实现鼠标在按钮上时,图标变小,或者Border变色,我觉得给按钮 添加 鼠标事件!MouseAdapter void mouseClicked(MouseEvent e) 
              鼠标在组件上单击时调用。 
     void mouseEntered(MouseEvent e) 
              鼠标进入组件时调用。 
     void mouseExited(MouseEvent e) 
              鼠标离开组件时调用。 
     void mousePressed(MouseEvent e) 
              鼠标按键在组件上按下时调用。 
     void mouseReleased(MouseEvent e) 
              鼠标按键在组件上释放时调用。 鼠标进入组件时,触发这个换小图标或Border变色的操作
    鼠标离开组件时,则还原~单击事件 或者 双击事件 都可以在这里实现了~