黑马程序员------java基础----GUI增加菜单

黑马程序员------java基础----GUI增加菜单

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

public class MainFrame extends JFrame implements ActionListener{
 private Container cont;
 private JButton btnLeft;
 private JButton btnCenter;
 private JButton btnRight;
 private JButton btnBorder;
 private JButton btnGrid;
 private JLabel labHit;
 private JMenuBar menuBar;
 private JMenu mOperation;
 private JMenu mHelp;
 private JMenuItem mItemFlowLeft;
 private JMenuItem mItemFlowCenter;
 private JMenuItem mItemFlowRight;
 private JMenuItem mItemBorder;
 private JMenuItem mItemGrid;
 private JPanel panelMenu;
 private JPanel panelCompent;
 
 public MainFrame(){
  this.setTitle("第一个界面");
  this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  this.setSize(400, 300);
  this.setLocation(300, 300);
 
  cont = this.getContentPane();
//  this.setLayout(new GridLayout(3,2));
  this.setLayout(null);
  panelMenu = new JPanel();
  panelMenu.setLayout(new FlowLayout(FlowLayout.LEFT));
  panelMenu.setBounds(0, 0, 400, 30);
  panelCompent = new JPanel();
  panelCompent.setLayout(new GridLayout(3,2));
  panelCompent.setBounds(0, 30, 400, 240);
  cont.add(panelMenu);
  cont.add(panelCompent);
  addMenu();
  panelMenu.add(menuBar);
  btnLeft = new JButton("Left");
  btnCenter = new JButton("Center");
  btnRight = new JButton("Right");
  btnBorder = new JButton("边界");
  btnGrid = new JButton("网格");
  labHit = new JLabel("网格布局管理器");
  panelCompent.add(btnLeft);
  panelCompent.add(btnCenter);
  panelCompent.add(btnRight);
  panelCompent.add(btnBorder);
  panelCompent.add(btnGrid);
  panelCompent.add(labHit);
  btnLeft.addActionListener(this);
  btnCenter.addActionListener(this);
  btnRight.addActionListener(this);
  btnBorder.addActionListener(this);
  btnGrid.addActionListener(this);
  this.setVisible(true);
 }
 public void addMenu(){
    menuBar = new JMenuBar();
    mOperation = new JMenu("布局操作");
    mHelp = new JMenu("帮助");
    mItemFlowLeft = new JMenuItem("流式左靠齐");
    mItemFlowCenter = new JMenuItem("流式居中");
    mItemFlowRight = new JMenuItem("流式右靠齐");
    mItemBorder = new JMenuItem("边界");
    mItemGrid = new JMenuItem("网格");
    mItemFlowLeft.addActionListener(this);
    mItemFlowCenter.addActionListener(this);
    mItemFlowRight.addActionListener(this);
    mItemBorder.addActionListener(this);
    mItemGrid.addActionListener(this);
    mOperation.add(mItemFlowLeft);
    mOperation.add(mItemFlowCenter);
    mOperation.add(mItemFlowRight);
    mOperation.add(mItemBorder);
    mOperation.add(mItemGrid);
    menuBar.add(mOperation);
    menuBar.add(mHelp);
 }
 public void actionPerformed(ActionEvent aEvt){
  if((aEvt.getSource() == btnLeft)
      || (aEvt.getSource() == mItemFlowLeft)){
   panelCompent.setLayout(new FlowLayout(FlowLayout.LEFT));
   labHit.setText("流式布局管理器左靠齐");
  }
  else{
   if((aEvt.getSource() == btnRight)
     || (aEvt.getSource() == mItemFlowRight)){
    panelCompent.setLayout(new FlowLayout(FlowLayout.RIGHT));
    labHit.setText("流式布局管理器右靠齐");
   }
   else{
    if((aEvt.getSource() == btnCenter)
      || (aEvt.getSource() == mItemFlowCenter)){
     panelCompent.setLayout(new FlowLayout(FlowLayout.CENTER));
     labHit.setText("流式布局管理器居中");
    }
    else{
     if((aEvt.getSource() == btnGrid)
      || (aEvt.getSource() == mItemGrid)){
      panelCompent.setLayout(new GridLayout(3,2));
      labHit.setText("网格布局管理器");
     }
     else{
      if((aEvt.getSource() == btnBorder)
      || (aEvt.getSource() == mItemBorder)){
       panelCompent.setLayout(new BorderLayout());
      
       labHit.setText("边界布局管理器左靠齐");
       panelCompent.add(btnLeft, "West");
       panelCompent.add(btnCenter, "Center");
       panelCompent.add(btnRight, "East");
       panelCompent.add(btnBorder, "North");
       panelCompent.add(btnGrid, "South");
//       cont.add(labHit);
      }
     }
    }
   }
  }
 }
 public static void main(String[] args){
  new MainFrame();
 }

}
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值