- Swing 编程示例
- 示例 - 主页
- 示例 - 环境设置
- 示例 - 边框
- 示例 - 按钮
- 示例 - 复选框
- 示例 - 组合框
- 示例 - 颜色选择器
- 示例 - 对话框
- 示例 - 编辑器窗格
- 示例 - 文件选择器
- 示例 - 格式化文本字段
- 示例 - 框架
- 示例 - 列表
- 示例 - 布局
- 示例 - 菜单
- 示例 - 密码字段
- 示例 - 进度条
- 示例 - 滚动窗格
- 示例 - 滑块
- 示例 - 微调器
- 示例 - 表格
- 示例 - 工具栏
- 示例 - 树
- 有用的 Swing 资源
- Swing - 快速指南
- Swing - 有用资源
- Swing - 讨论
Swing 示例 - 创建工具栏
以下示例展示了如何在 Java Swing 应用程序中创建工具栏。
我们正在使用以下 API。
JToolBar − 创建工具栏。
JToolBar().add(component) − 向工具栏添加组件。
示例
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class SwingTester {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingTester(){
prepareGUI();
}
public static void main(String[] args){
SwingTester swingControlDemo = new SwingTester();
swingControlDemo.showTableDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(500,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showTableDemo(){
headerLabel.setText("Control in action: JToolbar");
JToolBar toolBar = new JToolBar("Toolbar");
JButton buttonA = new JButton("Alpha");
JButton buttonB = new JButton("Beta");
JButton buttonC = new JButton("Gamma");
toolBar.add(buttonA);
toolBar.add(buttonB);
toolBar.add(buttonC);
controlPanel.add(toolBar);
mainFrame.setVisible(true);
}
}
输出
swingexamples_toolbars.htm
广告