Swing學習19:Java Swing JToolBar

工具欄提供了一個用來顯示常用按鈕和操作的組件。它可以把任意類型的組件附加到工具條上,但是通常是增加按鈕。工具欄 JToolBar 類的常用構造方法如表 1 所示。
在這裏插入圖片描述
與 JMenuBar 不一樣,JToolBar 對象可以直接被添加到容器中。JTodBar 類的常用方法如表 2 所示。
在這裏插入圖片描述
例 1
下面通過實例來說明如何使用 JToolBar 創建工具欄,在該實例中給工具欄上的按鈕添加了圖片,當圖片不存在時使用文字代替。主要實現步驟如下所示。

(1) 創建一個 ToolBarDemo 類,繼承 JPanel 父類並實現 ActionListener 接口。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class ToolBarDemo extends JPanel implements ActionListener
{
    protected JTextArea textArea;
    protected String newline="\n";
    static final private String OPEN="OPEN";
    static final private String SAVE="SAVE";
    static final private String NEW="NEW";   
    //事件監聽器部分的代碼省略,請查閱源文件
    protected void displayResult(String actionDescription)
    {
        textArea.append(actionDescription+newline);
    }
    public static void main(String[] args)
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        //定義窗體
        JFrame frame=new JFrame("工具欄");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //定義面板
        ToolBarDemo newContentPane=new ToolBarDemo();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        //顯示窗體
        frame.pack();
        frame.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e)
    {
        // TODO 自動生成的方法存根
    }
}

(2) 在構造方法中對工具欄進行設置,主要代碼如下所示。

public ToolBarDemo()
{
    super(new BorderLayout());
    //創建工具欄
    JToolBar toolBar=new JToolBar();
    addButtons(toolBar);
    //創建一個文本域,用來輸出一些信息
    textArea=new JTextArea(15, 30);
    textArea.setEditable(false);
    JScrollPane scrollPane=new JScrollPane(textArea);
    //把組件添加到面板中
    setPreferredSize(new Dimension(450, 110));
    add(toolBar,BorderLayout.PAGE_START);
    add(scrollPane,BorderLayout.CENTER);
}

(3) 構造方法中創建了一個工具欄 toolBar,然後調用 addButtons() 方法爲工具欄設置按鈕。addButtons() 方法的代碼如下:

protected void addButtons(JToolBar toolBar)
{
    JButton button=null;
    button=makeNavigationButton("new1",NEW,"新建一個文件","新建");
    toolBar.add(button);
    button=makeNavigationButton("open1",OPEN,"打開一個文件","打開");
    toolBar.add(button);
    button=makeNavigationButton("save1",SAVE,"保存當前文件","保存");
    toolBar.add(button);
}

(4) 在 addButtons() 方法中調用 makeNavigationButton() 方法,實現對工具欄上的按鈕指定圖片、動作指令、提示信息和無圖片時的文本。具體實現代碼如下:

protected JButton makeNavigationButton(String imageName,String actionCommand,String toolTipText,String altText)
{
    //搜索圖片
    String imgLocation=imageName+".jpg";
    URL imageURL=ToolBarDemo.class.getResource(imgLocation);
    //初始化工具按鈕
    JButton button=new JButton();
    //設置按鈕的命令
    button.setActionCommand(actionCommand);
    //設置提示信息
    button.setToolTipText(toolTipText);
    button.addActionListener(this);
    if(imageURL!=null)
    {
        //找到圖像
        button.setIcon(new ImageIcon(imageURL));
    }
    else
    {
        //沒有圖像
        button.setText(altText);
        System.err.println("Resource not found: "+imgLocation);
    }
    return button;
}

(5) 運行程序,在窗口的頂部會看到包含 3 個按鈕的工具欄。當鼠標指針放在工具按鈕上時出現提示信息,如圖 1 所示。
在這裏插入圖片描述

謝謝觀看

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章