JToolBar是Java Swing包的一部分。JToolBar是工具栏的一个实现。JToolBar是一组常用组件,如按钮或下拉菜单。 用户可以将JToolBar拖到不同的位置
null
该类的构造函数是:
- JToolBar():创建一个水平方向的新工具栏
- JToolBar(into):创建具有指定方向的新工具栏
- JToolBar(字符串n):使用指定的名称创建新工具栏
- JToolBar(String n,int o):创建具有指定名称和方向的新工具栏。
常用方法:
- addSeparator():将分隔符添加到工具栏的末尾
- setFloatable(布尔b):如果传递true,则可以将工具栏拖到其他位置,或者不拖到其他位置。
- setLayout(LayoutManager m):设置工具栏的布局
- 设置方向(int o):设置工具栏的方向
- 添加(组件c):将组件添加到工具栏
- getMargin():返回工具栏的边距
- setMargin(插图m):将工具栏的边距设置为给定的插图
- getOrientation():返回工具栏的方向
- updateUI():来自UIFactory的外观已更改的通知。
- setUI(ToolBarUI u):设置呈现此组件的外观对象。
- setRollover(布尔b):将此工具栏的滚动状态设置为布尔b。
- setFloatable(布尔b):布尔b决定工具栏的位置是否可以更改
- SetbOrderPaint(布尔b):决定是否绘制边框。
- paintBorder(图形g):绘制工具栏的边框
- isRollover():返回滚动状态。
- isFloatable():返回floatable属性。
- IsBorderPaint():返回是否绘制边框
- getComponentIndex(组件c):返回指定组件的索引。
- getComponentAtIndex(int i):返回指定索引处的组件。
- addSeparator(维度大小):附加指定维度的分隔符。
- addSeparator():附加默认大小的分隔符。
- 添加(操作a):在指定操作之后添加新的JButton。
- paramString():返回此JToolBar的字符串表示形式。
- getUIClassID():返回呈现此组件的外观类的名称。
- getAccessibleContext():获取与此JToolBar关联的AccessibleContext。
以下程序将演示工具栏的使用。 1.编程创建一个简单的工具栏,并向其中添加按钮和组合框。
JAVA
// Java Program to create a simple toolbar and add buttons and combobox to it. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Tool extends JFrame { // toolbar static JToolBar tb; // buttons static JButton b1, b2; // create a frame static JFrame f; // create a combo box static JComboBox x; public static void main() { // create a frame f = new JFrame( "Toolbar demo" ); // set layout for frame f.setLayout( new BorderLayout()); // create a toolbar tb = new JToolBar(); // create a panel JPanel p = new JPanel(); // create a combobox x = new JComboBox( new String[] { "item 1" , "item 2" , "item 3" }); // create new buttons b1 = new JButton( "button 1" ); b2 = new JButton( "button 2" ); // add buttons p.add(b1); p.add(b2); // add menu to menu bar p.add(x); tb.add(p); // add toolbar to frame f.add(tb, BorderLayout.NORTH); // set the size of the frame f.setSize( 500 , 500 ); f.setVisible( true ); } } |
输出:
2.创建工具栏并向其组件添加操作侦听器的程序。
JAVA
// Java Program to create a toolbar and add action listener to its components . import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Tool extends JFrame implements ActionListener, ItemListener { // toolbar static JToolBar tb; // buttons static JButton b1, b2; // create a frame static JFrame f; // create a combo box static JComboBox x; // create a label static JLabel l, l1; public static void main() { // create a object of class Tool to = new Tool(); // create a label l = new JLabel( "nothing selected" ); l1 = new JLabel( "nothing selected" ); // create a frame f = new JFrame( "Toolbar demo" ); // set layout for frame f.setLayout( new BorderLayout()); // create a toolbar tb = new JToolBar(); // create a panel JPanel p = new JPanel(); // create a combobox x = new JComboBox( new String[] { "item 1" , "item 2" , "item 3" }); // add actionListener x.addItemListener(to); // create new buttons b1 = new JButton( "button 1" ); b2 = new JButton( "button 2" ); // add ActionListener to it b1.addActionListener(to); b2.addActionListener(to); // add buttons p.add(b1); p.add(b2); // add menu to menu bar p.add(x); tb.add(p); // create a panel JPanel p1 = new JPanel(); p1.add(l); p1.add(l1); // add toolbar to frame f.add(tb, BorderLayout.NORTH); f.add(p1, BorderLayout.CENTER); // set the size of the frame f.setSize( 500 , 500 ); f.setVisible( true ); } // if button is pressed public void actionPerformed(ActionEvent e) { l.setText(e.getActionCommand() + " selected." ); } // if combo box is selected public void itemStateChanged(ItemEvent e) { l1.setText(x.getSelectedItem() + " selected." ); } } |
输出:
3.创建垂直工具栏并向其组件添加操作侦听器的程序。
JAVA
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Tool extends JFrame implements ActionListener, ItemListener { // toolbar static JToolBar tb; // buttons static JButton b1, b2; // create a frame static JFrame f; // create a combo box static JComboBox x; // create a label static JLabel l, l1; public static void main() { // create a object of class Tool to = new Tool(); // create a label l = new JLabel( "nothing selected" ); l1 = new JLabel( "nothing selected" ); // create a frame f = new JFrame( "Toolbar demo" ); // set layout for frame f.setLayout( new BorderLayout()); // create a toolbar tb = new JToolBar( "toolbar" ); // set orientation tb.setOrientation(SwingConstants.VERTICAL); // create a panel JPanel p = new JPanel(); // set layout p.setLayout( new BoxLayout(p, BoxLayout.Y_AXIS)); // create a combobox x = new JComboBox( new String[] { "item 1" , "item 2" , "item 3" }); // add actionListener x.addItemListener(to); // create new buttons b1 = new JButton( "button 1" ); b2 = new JButton( "button 2" ); // add ActionListener to it b1.addActionListener(to); b2.addActionListener(to); // add buttons p.add(b1); p.add(b2); // add menu to menu bar p.add(x); tb.add(p); // create a panel JPanel p1 = new JPanel(); p1.add(l); p1.add(l1); // add toolbar to frame f.add(tb, BorderLayout.WEST); f.add(p1, BorderLayout.CENTER); // set the size of the frame f.setSize( 500 , 500 ); f.setVisible( true ); } // if button is pressed public void actionPerformed(ActionEvent e) { l.setText(e.getActionCommand() + " selected." ); } // if combo box is selected public void itemStateChanged(ItemEvent e) { l1.setText(x.getSelectedItem() + " selected." ); } } |
输出:
注意:以下程序可能无法在联机编译器中运行,请使用脱机IDE
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END