import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import java.net.URL;
import java.awt.*;
import java.awt.event.*;
/**
* <p>Title: 工具欄演示</p>
* <p>Description: 提供一個工具欄,包括“打開”、“保存”、“搜索”工具按鈕</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: ToolBarDemo.java</p>
* @version 1.0
*/
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 SEARCH = "SEARCH";
/**
*<br>方法說明:構造器
*<br>輸入參數:
*<br>返回類型:
*/
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);
}
/**
*<br>方法說明:構建工具欄
*<br>輸入參數:JToolBar toolBar 工具條
*<br>返回類型:
*/
protected void addButtons(JToolBar toolBar) {
JButton button = null;
//第一個按鈕,“打開”
button = makeNavigationButton("Open16", OPEN,
"打開一個文件!",
"打開");
toolBar.add(button);
//第二個按鈕,“保存”
button = makeNavigationButton("Save16", SAVE,
"保存當前文件!",
"保存");
toolBar.add(button);
//第三個按鈕,“搜索”
button = makeNavigationButton("Search16", SEARCH,
"搜索文件中的字符!",
"搜索");
toolBar.add(button);
}
/**
*<br>方法說明:構造工具欄上的按鈕
*<br>輸入參數:
*<br>返回類型:
*/
protected JButton makeNavigationButton(String imageName,
String actionCommand,
String toolTipText,
String altText) {
//搜索圖片
String imgLocation = "images/"
+ imageName
+ ".gif";
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;
}
/**
*<br>方法說明:事件監聽
*<br>輸入參數:
*<br>返回類型:
*/
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String description = null;
if (OPEN.equals(cmd)) { //點擊第一個按鈕
description = "打開一個文件操作!";
} else if (SAVE.equals(cmd)) { //點擊第二個按鈕
description = "保存文件操作";
} else if (SEARCH.equals(cmd)) { //點擊第三個按鈕
description = "搜索字符操作";
}
displayResult("如果這裡是真正的程序,你將進入: "
+ description);
}
protected void displayResult(String actionDescription) {
textArea.append(actionDescription + newline);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
//定義窗體
JFrame frame = new JFrame("ToolBarDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//定義面板
ToolBarDemo newContentPane = new ToolBarDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
//顯示窗體
frame.pack();
frame.setVisible(true);
}
}