黑马程序员---java基础----GUI添加工具栏

黑马程序员---java基础----GUI添加工具栏

 

1、JMenuItem菜单项
     JMenuItem menuItem = new JMenuItem("名字");
     ...
2、JMenu菜单:存放菜单项
     JMenu menu = new JMenu("名字");
     ...
     menu.add(菜单项);
3、JMenuBar菜单栏:存放菜单
     JMenuBar menuBar = new JMenuBar();
     menuBar.add(菜单);
4、将菜单栏添加到容器中
二、工具栏
1、JButton:图标按钮
   JButton btn = new JButton(new ImageIcon("图标名字"));
   ...
2、JToolBar:工具栏

   JToolBar toolBar = new JToolBar();
   toolBar.add(图标按钮);
3、将工具栏添加到容器中。

 


 import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainFrame extends JFrame implements ActionListener{
    private Container cont;
    private JPanel panelMenu;
    private JPanel panelToolBar;
    private JPanel panelCompent;

    private JButton btnLeft;
    private JButton btnCenter;
    private JButton btnRight;
    private JButton btnBorder;
    private JButton btnGrid;
    private JLabel labHit;

    private JMenuBar menuBar;
    private JMenu menuOperation;
    private JMenu menuHelp;
    private JMenuItem mItemFlowLeft;
    private JMenuItem mItemFlowCenter;
    private JMenuItem mItemFlowRight;
    private JMenuItem mItemBorder;
    private JMenuItem mItemGrid;

    private JToolBar toolBar;
    private JButton tBtnLeft;
    private JButton tBtnCenter;
    private JButton tBtnRight;
    private JButton tBtnBorder;
    private JButton tBtnGrid;  

    public MainFrame(){
        this.setTitle("第一个界面");
        this.setBounds(300, 300, 600, 450);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

        cont = this.getContentPane();
//        cont.setLayout(new GridLayout(3, 9));
        cont.setLayout(null);
        initPanel();
        initMenu();
        initToolBar();
        initCompent();
        addMenu();
        addCompent();
//        addBorderCompent();
        this.setVisible(true);
    }
    public void initPanel(){
        panelMenu = new JPanel();
        panelMenu.setLayout(new FlowLayout(FlowLayout.LEFT));
        panelMenu.setBounds(0, 0, 600, 30);
        panelToolBar = new JPanel();
        panelToolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
        panelToolBar.setBounds(0, 30, 600, 50);
        panelCompent = new JPanel();
        panelCompent.setLayout(new GridLayout(3, 2));
        panelCompent.setBounds(0, 80, 600, 350);
        cont.add(panelMenu);
        cont.add(panelToolBar);
        cont.add(panelCompent);
    }
    public void initMenu(){
        menuBar = new JMenuBar();
        menuOperation = new JMenu("布局操作");
        menuHelp = new JMenu("帮助");
        mItemFlowLeft = new JMenuItem("流式左布局");
        mItemFlowLeft.addActionListener(this);
        mItemFlowCenter = new JMenuItem("流式居中");
        mItemFlowCenter.addActionListener(this);
        mItemFlowRight = new JMenuItem("流式右布局");
        mItemFlowRight.addActionListener(this);
        mItemBorder = new JMenuItem("边界布局");
        mItemBorder.addActionListener(this);
        mItemGrid = new JMenuItem("网格布局");
        mItemGrid.addActionListener(this);
    }
    public void initToolBar(){
        toolBar = new JToolBar();
        tBtnLeft = new JButton(new ImageIcon("COPY.jpg"));
         tBtnLeft.addActionListener(this);
        tBtnLeft.setToolTipText("流式左布局");
        tBtnCenter = new JButton(new ImageIcon("HELP.jpg"));
        tBtnCenter.addActionListener(this);
        tBtnCenter.setToolTipText("流式居中");
        tBtnRight = new JButton(new ImageIcon("find.jpg"));
         tBtnRight.addActionListener(this);
        tBtnRight.setToolTipText("流式右布局");
        tBtnBorder = new JButton(new ImageIcon("CUT.jpg"));
        tBtnBorder.addActionListener(this);
        tBtnBorder.setToolTipText("边界布局");
        tBtnGrid = new JButton(new ImageIcon("OPEN.jpg"));
        tBtnGrid.addActionListener(this);
        tBtnGrid.setToolTipText("网格布局");
        toolBar.add(tBtnLeft);
        toolBar.add(tBtnCenter);
        toolBar.add(tBtnRight);
        toolBar.add(tBtnBorder);
        toolBar.add(tBtnGrid);
        panelToolBar.add(toolBar);
    }
    public void initCompent(){
        btnLeft = new JButton("LEFT");
        btnLeft.addActionListener(this);
        btnRight = new JButton("RIGHT");
        btnRight.addActionListener(this);
        btnCenter = new JButton("CENTER");
        btnCenter.addActionListener(this);
        btnBorder = new JButton("BORDER");
        btnBorder.addActionListener(this);
        btnGrid = new JButton("GRID");
        btnGrid.addActionListener(this);
        labHit = new JLabel("提示");
    }
    public void addMenu(){
        menuOperation.add(mItemFlowLeft);
        menuOperation.add(mItemFlowCenter);
        menuOperation.add(mItemFlowRight);
        menuOperation.add(mItemBorder);
        menuOperation.add(mItemGrid);
        menuBar.add(menuOperation);
        menuBar.add(menuHelp);
        panelMenu.add(menuBar);
    }
    public void addCompent(){
        panelCompent.add(btnLeft);
        panelCompent.add(btnCenter);
        panelCompent.add(btnRight);
        panelCompent.add(btnBorder);
        panelCompent.add(btnGrid);
        panelCompent.add(labHit);
    }

    public void addBorderCompent(){
        panelCompent.add(btnLeft, "West");
        panelCompent.add(btnCenter, "Center");
        panelCompent.add(btnRight, "East");
        panelCompent.add(btnBorder, "North");
        panelCompent.add(btnGrid, "South");
    }

    public void actionPerformed(ActionEvent aEvt){
        if((btnLeft == aEvt.getSource())
          ||(mItemFlowLeft == aEvt.getSource())
             ||(tBtnLeft == aEvt.getSource())){
            panelCompent.setLayout(new FlowLayout(FlowLayout.LEFT));
            labHit.setText("流式布局管理器左靠齐");
        }
        else {
            if((btnRight == aEvt.getSource())
              ||(mItemFlowRight == aEvt.getSource())
                   ||(tBtnRight == aEvt.getSource())){
                panelCompent.setLayout(new FlowLayout(FlowLayout.RIGHT));
                labHit.setText("流式布局管理器右靠齐");
             }
     else {
                if((btnCenter == aEvt.getSource())
                  ||(mItemFlowCenter == aEvt.getSource())
                    ||(tBtnCenter == aEvt.getSource())){
                    panelCompent.setLayout(new FlowLayout(FlowLayout.CENTER));
                    labHit.setText("流式布局管理器中靠齐");
                 }
   else {
                     if((btnBorder == aEvt.getSource())
                       ||(mItemBorder == aEvt.getSource())
                          ||(tBtnBorder == aEvt.getSource())){
                         panelCompent.setLayout(new BorderLayout());
                         addBorderCompent();
                         labHit.setText("边界布局管理器");
                      }
                      else{
                          if((btnGrid == aEvt.getSource())
                            ||(mItemGrid == aEvt.getSource())
                            ||(tBtnGrid == aEvt.getSource())
                                 ){
                              panelCompent.setLayout(new GridLayout(3, 2));
//                              addBorderCompent();
                              labHit.setText("网格布局管理器");
                          }
                      }
           }
       }
   }
   }
    public static void main(String[] args){
        new MainFrame();
    }
}
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值