import javax.swing.*;
import java.awt.*; public class Demo extends JFrame {
public Demo() {
setTitle("工具栏");
setBounds(100, 100, 300, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container c = getContentPane(); JButton newButton=new JButton("新建");
JButton saveButton=new JButton("保存"); JToolBar toolBar=new JToolBar();//工具栏
toolBar.setFloatable(false);//不可移动
c.add(toolBar, BorderLayout.NORTH);//边界布局,其他布局可能显示不全
toolBar.add(newButton);
toolBar.addSeparator(new Dimension(20,0));//分隔符的尺寸(dimension)
toolBar.add(saveButton);
} public static void main(String[] args) {
Demo frame = new Demo();
frame.setVisible(true);
}
}