首页 > 软件开发 > JAVA >

Swing 工具栏的使用

来源:互联网 2023-03-16 19:27:08 396

工具栏是程序中常见的一种组件,比如Word和Excel工作区上方的工具栏。JavaSwing也有工具栏组件:JToolBar,具体怎么使用呢?接下来小编就整理分享给大家。0ON办公区 - 实用经验教程分享!

Swing 工具栏的使用0ON办公区 - 实用经验教程分享!

工具/原料

  • eclipse2019

一、构造方法

  • 1

    常用的构造方法如下:0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    构造方法1:0ON办公区 - 实用经验教程分享!

    JToolBar()创建新的工具栏,默认的方向为 HORIZONTAL(水平)0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    构造方法2:0ON办公区 - 实用经验教程分享!

    JToolBar(int orientation)创建具有指定 orientation 的新工具栏0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    构造方法3:0ON办公区 - 实用经验教程分享!

    JToolBar(String name)创建一个具有指定 name 的新工具栏0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    构造方法4:0ON办公区 - 实用经验教程分享!

    JToolBar(String name,int orientation)创建一个具有指定 name 和 orientation 的新工具栏0ON办公区 - 实用经验教程分享!

  • 1相关内容非法爬取自百度经验
  • 二、常用方法

  • 1

    常用方法如下:

    0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    Swing 工具栏的使用0ON办公区 - 实用经验教程分享!

  • 三、实例

  • 1

    创建JFrame容器,设置容器的名称,容器的内容窗格:Demo27_JToolBar0ON办公区 - 实用经验教程分享!

    Swing 工具栏的使用0ON办公区 - 实用经验教程分享!

  • 2

    内容窗格Demo27_JToolBar目前还是空的,接下来我们把它填满0ON办公区 - 实用经验教程分享!

    Demo27_JToolBar是我们自定义的一个类,它继承JPanel,并且实现ActionListener接口0ON办公区 - 实用经验教程分享!

    Swing 工具栏的使用0ON办公区 - 实用经验教程分享!

    Swing 工具栏的使用0ON办公区 - 实用经验教程分享!

  • 3

    完成之后的效果如下:0ON办公区 - 实用经验教程分享!

    点击工具栏上的按钮,会在下方显示对应的内容0ON办公区 - 实用经验教程分享!

    Swing 工具栏的使用0ON办公区 - 实用经验教程分享!

    Swing 工具栏的使用0ON办公区 - 实用经验教程分享!

  • 4

    Demo27_JToolBar类代码如下:0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    public class Demo27_JToolBar extends JPanel implements ActionListener{0ON办公区 - 实用经验教程分享!

    protected JTextArea textArea;0ON办公区 - 实用经验教程分享!

    protected String newline="\n";0ON办公区 - 实用经验教程分享!

    static final private String OPEN="OPEN";0ON办公区 - 实用经验教程分享!

    static final private String SAVE="SAVE";0ON办公区 - 实用经验教程分享!

    static final private String NEW="NEW";0ON办公区 - 实用经验教程分享!

    public static void main(String[] args){0ON办公区 - 实用经验教程分享!

    JFrame.setDefaultLookAndFeelDecorated(true);0ON办公区 - 实用经验教程分享!

    //定义窗体0ON办公区 - 实用经验教程分享!

    JFrame frame=new JFrame("工具栏");0ON办公区 - 实用经验教程分享!

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);0ON办公区 - 实用经验教程分享!

    //定义面板0ON办公区 - 实用经验教程分享!

    Demo27_JToolBar demo27_JToolBar=new Demo27_JToolBar();0ON办公区 - 实用经验教程分享!

    demo27_JToolBar.setOpaque(true);0ON办公区 - 实用经验教程分享!

    frame.setContentPane(demo27_JToolBar);0ON办公区 - 实用经验教程分享!

    SwingUtils.setCenter(frame);//设置窗体大小600*800并居中0ON办公区 - 实用经验教程分享!

    //显示窗体0ON办公区 - 实用经验教程分享!

    frame.setVisible(true);0ON办公区 - 实用经验教程分享!

    }0ON办公区 - 实用经验教程分享!

    //事件监听器部分的代码省略,请查阅源文件0ON办公区 - 实用经验教程分享!

    private void displayResult(String actionDescription){0ON办公区 - 实用经验教程分享!

    textArea.append(actionDescription newline);0ON办公区 - 实用经验教程分享!

    }0ON办公区 - 实用经验教程分享!

    public Demo27_JToolBar() {0ON办公区 - 实用经验教程分享!

    super(new BorderLayout());0ON办公区 - 实用经验教程分享!

    //创建工具栏0ON办公区 - 实用经验教程分享!

    JToolBar toolBar=new JToolBar();0ON办公区 - 实用经验教程分享!

    addButtons(toolBar);0ON办公区 - 实用经验教程分享!

    //创建一个文本域,用来输出一些信息0ON办公区 - 实用经验教程分享!

    textArea=new JTextArea(15, 30);0ON办公区 - 实用经验教程分享!

    textArea.setEditable(false);0ON办公区 - 实用经验教程分享!

    textArea.setFont(new Font("宋体", 1, 20));0ON办公区 - 实用经验教程分享!

    JScrollPane scrollPane=new JScrollPane(textArea);0ON办公区 - 实用经验教程分享!

    //把组件添加到面板中0ON办公区 - 实用经验教程分享!

    setPreferredSize(new Dimension(450, 110));0ON办公区 - 实用经验教程分享!

    add(toolBar,BorderLayout.PAGE_START);0ON办公区 - 实用经验教程分享!

    add(scrollPane,BorderLayout.CENTER);0ON办公区 - 实用经验教程分享!

    }0ON办公区 - 实用经验教程分享!

    private void addButtons(JToolBar toolBar) {0ON办公区 - 实用经验教程分享!

    JButton button=null;0ON办公区 - 实用经验教程分享!

    button=makeNavigationButton("NEW",NEW,"新建一个文件","新建");0ON办公区 - 实用经验教程分享!

    toolBar.add(button);0ON办公区 - 实用经验教程分享!

    button=makeNavigationButton("OPEN",OPEN,"打开一个文件","打开");0ON办公区 - 实用经验教程分享!

    toolBar.add(button);0ON办公区 - 实用经验教程分享!

    button=makeNavigationButton("SAVE",SAVE,"保存当前文件","保存");0ON办公区 - 实用经验教程分享!

    toolBar.add(button);0ON办公区 - 实用经验教程分享!

    }0ON办公区 - 实用经验教程分享!

    private JButton makeNavigationButton(String imageName,String actionCommand,String toolTipText,String altText){0ON办公区 - 实用经验教程分享!

    //搜索图片0ON办公区 - 实用经验教程分享!

    String imgLocation=imageName ".png";0ON办公区 - 实用经验教程分享!

    ImageIcon icon = new ImageIcon(imgLocation);0ON办公区 - 实用经验教程分享!

    //初始化工具按钮0ON办公区 - 实用经验教程分享!

    JButton button=new JButton();0ON办公区 - 实用经验教程分享!

    //设置按钮的命令0ON办公区 - 实用经验教程分享!

    button.setActionCommand(actionCommand);0ON办公区 - 实用经验教程分享!

    //设置提示信息0ON办公区 - 实用经验教程分享!

    button.setToolTipText(toolTipText);0ON办公区 - 实用经验教程分享!

    button.addActionListener(this);0ON办公区 - 实用经验教程分享!

    button.setIcon(icon);0ON办公区 - 实用经验教程分享!

    return button;0ON办公区 - 实用经验教程分享!

    }0ON办公区 - 实用经验教程分享!

    @Override0ON办公区 - 实用经验教程分享!

    public void actionPerformed(ActionEvent e) {0ON办公区 - 实用经验教程分享!

    if(e.getActionCommand().contentEquals("NEW")) {0ON办公区 - 实用经验教程分享!

    displayResult("新建一个文件");0ON办公区 - 实用经验教程分享!

    }else if(e.getActionCommand().contentEquals("OPEN")) {0ON办公区 - 实用经验教程分享!

    displayResult("打开一个文件");0ON办公区 - 实用经验教程分享!

    }else if(e.getActionCommand().contentEquals("SAVE")) {0ON办公区 - 实用经验教程分享!

    displayResult("保存文件");0ON办公区 - 实用经验教程分享!

    }0ON办公区 - 实用经验教程分享!

    }0ON办公区 - 实用经验教程分享!

    }0ON办公区 - 实用经验教程分享!

  • 5

    SwingUtils类代码如下:0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    0ON办公区 - 实用经验教程分享!

    public class SwingUtils {0ON办公区 - 实用经验教程分享!

    public static void setCenter(JFrame jf) {0ON办公区 - 实用经验教程分享!

    int screenWidth=Toolkit.getDefaultToolkit().getScreenSize().width;0ON办公区 - 实用经验教程分享!

    int screenHeight=Toolkit.getDefaultToolkit().getScreenSize().height;0ON办公区 - 实用经验教程分享!

    int jframeWidth = 800;0ON办公区 - 实用经验教程分享!

    int jframeHeight = 600;0ON办公区 - 实用经验教程分享!

    jf.setBounds((screenWidth/2)-(jframeWidth/2), (screenHeight/2)-(jframeHeight/2),0ON办公区 - 实用经验教程分享!

    jframeWidth, jframeHeight);0ON办公区 - 实用经验教程分享!

    }0ON办公区 - 实用经验教程分享!

    }0ON办公区 - 实用经验教程分享!

  • 以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!0ON办公区 - 实用经验教程分享!


    标签: JAVA

    办公区 Copyright © 2016-2023 www.bgqu.net. Some Rights Reserved. 备案号:湘ICP备2020019561号统计代码