工具栏

package swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class ToolBarTest {
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   @Override
   public void run() {
    ToolBarFrame frame = new ToolBarFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
   }
  });
 }
}

class ToolBarFrame extends JFrame {
 public ToolBarFrame() {
  this.setTitle("ToolBar Test");
  this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  
  panel = new JPanel();
  this.add(panel, BorderLayout.CENTER);
  
  Action blueAction = new ColorAction("Blue", new ImageIcon("c:/picture/1.gif"), Color.BLUE);
  Action yellowAction = new ColorAction("Yellow", new ImageIcon("c:/picture/2.gif"), Color.YELLOW);
  Action redAction = new ColorAction("Red", new ImageIcon("c:/picture/3.gif"), Color.RED);
  Action exitAction = new AbstractAction("Exit", new ImageIcon("c:/picture/4.gif")) {
   @Override
   public void actionPerformed(ActionEvent event) {
    System.exit(0);
   }
  };
  exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit");
  
  //工具栏
  JToolBar toolBar = new JToolBar();
  toolBar.add(blueAction);
  toolBar.add(yellowAction);
  toolBar.add(redAction);
  toolBar.addSeparator();
  toolBar.add(exitAction);
  this.add(toolBar, BorderLayout.NORTH);
  
  //菜单栏
  JMenu menu = new JMenu("Color");
  menu.add(blueAction);
  menu.add(yellowAction);
  menu.add(redAction);
  menu.addSeparator();
  menu.add(exitAction);
  
  JMenuBar menuBar = new JMenuBar();
  menuBar.add(menu);
  this.setJMenuBar(menuBar);
 }
 
 public static final int DEFAULT_WIDTH = 300;
 public static final int DEFAULT_HEIGHT = 200;
 
 private JPanel panel;
 
 class ColorAction extends AbstractAction {
  public ColorAction(String name, Icon icon, Color c) {
   this.putValue(Action.NAME, name);
   this.putValue(Action.SMALL_ICON, icon);
   this.putValue(Action.SHORT_DESCRIPTION, name + " background");
   this.putValue("Color", c);
  }

  @Override
  public void actionPerformed(ActionEvent event) {
   Color c = (Color) this.getValue("Color");
   panel.setBackground(c);
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值