• java 核心编程 toolbar


    简介

    toolbar

    code

    /*
     * @Author: your name
     * @Date: 2020-11-08 09:38:15
     * @LastEditTime: 2020-11-08 10:10:19
     * @LastEditors: Please set LastEditors
     * @Description: In User Settings Edit
     * @FilePath: /java/calcu/ToolbarFrame.java
     */
    package calcu;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * A frame with a toolbar and menu for color changes.
     */
    public class ToolBarFrame extends JFrame
    {
       private static final int DEFAULT_WIDTH = 300;
       private static final int DEFAULT_HEIGHT = 200;
       private JPanel panel;
       public ToolBarFrame()
       {
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    
          // add a panel for color change
    
          panel = new JPanel();
          add(panel, BorderLayout.CENTER);
    
          // set up actions
    
          var blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE);
          var yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"),
                Color.YELLOW);
          var redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED);
    
          var exitAction = new AbstractAction("Exit", new ImageIcon("exit.gif"))
             {
                public void actionPerformed(ActionEvent event)
                {
                   System.exit(0);
                }
             };
          exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit");
    
          // populate toolbar
    
          var bar = new JToolBar();
          bar.add(blueAction);
          bar.add(yellowAction);
          bar.add(redAction);
          bar.addSeparator();
          bar.add(exitAction);
          add(bar, BorderLayout.NORTH);
    
          // populate menu
    
          var menu = new JMenu("Color");
          menu.add(yellowAction);
          menu.add(blueAction);
          menu.add(redAction);
          menu.add(exitAction);
          var menuBar = new JMenuBar();
          menuBar.add(menu);
          setJMenuBar(menuBar);
       }
       public static void main(String []args) {
            EventQueue.invokeLater(() -> {
                ToolBarFrame t = new ToolBarFrame();
                t.setTitle("ImageTest");
                t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                t.setVisible(true);
            });
       }
       
    
       /**
        * The color action sets the background of the frame to a given color.
        */
       class ColorAction extends AbstractAction
       {
          public ColorAction(String name, Icon icon, Color c)
          {
             putValue(Action.NAME, name);
             putValue(Action.SMALL_ICON, icon);
             putValue(Action.SHORT_DESCRIPTION, name + " background");
             putValue("Color", c);
          }
    
          public void actionPerformed(ActionEvent event)
          {
             Color c = (Color) getValue("Color");
             panel.setBackground(c);
          }
       }
    }
    

    TIPs

    java uNRESOLVED COMPILATIon problem
    导致的问题可能是,文件名和类名没有对应。

    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    [CSS揭秘]不规则投影
    [CSS揭秘]规则投影
    [CSS揭秘]伪随机背景
    [CSS揭秘]复杂的背景图案
    [CSS揭秘]条纹背景
    [CSS揭秘]连续的图像边框
    Git_Eclipse:[1]Git安装插件
    Git_常用命令
    上海 day38--多表查询、python操作MySQL
    上海 day37-- MySQL 单表查询,连表操作和子查询
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13943666.html
Copyright © 2020-2023  润新知