📜  Java摇摆 |工具栏

📅  最后修改于: 2022-05-13 01:54:26.673000             🧑  作者: Mango

Java摇摆 |工具栏

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

该类的构造函数是:

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

常用方法:

  1. addSeparator() :将分隔符添加到工具栏的末尾
  2. setFloatable(boolean b) :如果传递了 true ,则工具栏可以拖动到其他位置,否则不能。
  3. setLayout(LayoutManager m) : 设置工具栏的布局
  4. setOrientation(int o) : 设置工具栏的方向
  5. add(Component c) : 将组件添加到工具栏
  6. getMargin() :返回工具栏的边距
  7. setMargin(Insets m) :将工具栏的边距设置为给定的插图
  8. getOrientation() :返回工具栏的方向
  9. updateUI() :来自 UIFactory 的通知,表明外观已更改。
  10. setUI(ToolBarUI u) :设置呈现此组件的外观对象。
  11. setRollover(boolean b) : 将此工具栏的翻转状态设置为 boolean b。
  12. setFloatable(boolean b) : boolean b 决定工具栏的位置是否可以改变
  13. setBorderPainted(boolean b):决定是否绘制边框。
  14. paintBorder(Graphics g) : 绘制工具栏的边框
  15. isRollover() :返回翻转状态。
  16. isFloatable() :返回可浮动属性。
  17. isBorderPainted() : 返回是否绘制边框
  18. getComponentIndex(Component c) :返回指定组件的索引。
  19. getComponentAtIndex(int i) :返回指定索引处的组件。
  20. addSeparator(Dimension size) :附加指定维度的分隔符。
  21. addSeparator() :添加默认大小的分隔符。
  22. add(Action 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);
    }
}


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


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


输出 :

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