Java Swing | JToolBar

JToolBar是Java Swing包的一部分。JToolBar是工具栏的一个实现。JToolBar是一组常用组件,如按钮或下拉菜单。 用户可以将JToolBar拖到不同的位置

null

该类的构造函数是:

  1. JToolBar():创建一个水平方向的新工具栏
  2. JToolBar(into):创建具有指定方向的新工具栏
  3. JToolBar(字符串n):使用指定的名称创建新工具栏
  4. JToolBar(String n,int o):创建具有指定名称和方向的新工具栏。

常用方法:

  1. addSeparator():将分隔符添加到工具栏的末尾
  2. setFloatable(布尔b):如果传递true,则可以将工具栏拖到其他位置,或者不拖到其他位置。
  3. setLayout(LayoutManager m):设置工具栏的布局
  4. 设置方向(int o):设置工具栏的方向
  5. 添加(组件c):将组件添加到工具栏
  6. getMargin():返回工具栏的边距
  7. setMargin(插图m):将工具栏的边距设置为给定的插图
  8. getOrientation():返回工具栏的方向
  9. updateUI():来自UIFactory的外观已更改的通知。
  10. setUI(ToolBarUI u):设置呈现此组件的外观对象。
  11. setRollover(布尔b):将此工具栏的滚动状态设置为布尔b。
  12. setFloatable(布尔b):布尔b决定工具栏的位置是否可以更改
  13. SetbOrderPaint(布尔b):决定是否绘制边框。
  14. paintBorder(图形g):绘制工具栏的边框
  15. isRollover():返回滚动状态。
  16. isFloatable():返回floatable属性。
  17. IsBorderPaint():返回是否绘制边框
  18. getComponentIndex(组件c):返回指定组件的索引。
  19. getComponentAtIndex(int i):返回指定索引处的组件。
  20. addSeparator(维度大小):附加指定维度的分隔符。
  21. addSeparator():附加默认大小的分隔符。
  22. 添加(操作a):在指定操作之后添加新的JButton。
  23. paramString():返回此JToolBar的字符串表示形式。
  24. getUIClassID():返回呈现此组件的外观类的名称。
  25. 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 );
}
}


输出:

图片[1]-Java Swing | JToolBar-yiteyi-C++库

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." );
}
}


输出:

图片[2]-Java Swing | JToolBar-yiteyi-C++库

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." );
}
}


输出:

图片[3]-Java Swing | JToolBar-yiteyi-C++库

注意:以下程序可能无法在联机编译器中运行,请使用脱机IDE

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享