java的栈图形演示


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*
指示发生了组件定义的动作的语义事件。当特定于组件的动作(比如被按下)发生时,由组件(比如 Button)生成此高级别事件。
事件被传递给每一个 ActionListener 对象,这些对象是使用组件的 addActionListener 方法注册的,用以接收这类事件。 
所以在给TextField类添加 ActionListener 类型的监听器时就会失败!

****下面还有XXXListener和XXXAdapter的用法,相信你会喜欢上XXXAdapter的用法
*/
public class stackDemo extends MouseAdapter{
   JFrame fr=new JFrame("StackDemo");//对话框
   JPanel pan= new JPanel();//菜单面板
   JPanel panStack = new JPanel();
   JButton pushBtn, popBtn, peekBtn;
   JTextField tf= new JTextField("整数", 4);
   JButton stackBtn[]= new JButton[10];
   
   JPanel panStackPointerLabel= new JPanel();
   JLabel stackPointerLabel = new JLabel("<-top");

   JPanel panRet= new JPanel();   
   JTextField tfRet= new JTextField("操作结果!");

   int top;
   
   public stackDemo(){
      fr.setSize(420,500);
      fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      fr.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

      pan.setPreferredSize(new Dimension(400, 50));
      pan.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
      panStack.setPreferredSize(new Dimension(80, 350));//设置栈面板大小
      panStack.setBackground(Color.yellow);
      pan.setBackground(Color.blue);
      pan.add(new JLabel("操作菜单:"));
      pan.add(pushBtn=new JButton("进栈"));
      pushBtn.addMouseListener(new pushAction());
      pan.add(popBtn=new JButton("出栈"));
      popBtn.addMouseListener(new popAction());
      pan.add(peekBtn=new JButton("栈顶元素"));
      
      tf.addMouseListener(this);
      pan.add(tf);
          
      for(int i=0; i<10; ++i){
         stackBtn[i]=new JButton("   ");
         panStack.add(stackBtn[i]);
      }
      fr.add(pan);
      fr.add(panStack);
      panStackPointerLabel.setLayout(null);
      panStackPointerLabel.setPreferredSize(new Dimension(80, 350));//设置指针面板的大小
      panStackPointerLabel.setBackground(Color.LIGHT_GRAY);
      stackPointerLabel.setFont(new Font("华文行楷", Font.BOLD, 20));
      panStackPointerLabel.add(stackPointerLabel);

      fr.add(panStackPointerLabel);
      panRet.setLayout(new FlowLayout(FlowLayout.LEFT));
      panRet.setBackground(Color.red);
      panRet.setPreferredSize(new Dimension(400, 50));
      
      tfRet.setEditable(false);//不能不编辑
      panRet.add(tfRet);//操作结果面板
      fr.add(panRet);
      fr.setVisible(true);
      stackPointerLabel.setBounds(0, stackBtn[9].getLocation().y, 50, 50);//设置栈顶指针位置
      top=9;
   }

   public void mouseClicked(MouseEvent e){
       tf.selectAll();//鼠标单击时选中全部文本
   }
   
   //push 按钮监听器
   class pushAction implements MouseListener{
       public void mouseClicked(MouseEvent e){
          String text;
          
          if((text=tf.getText())!="   "){
              for(int i=0; i<text.length(); ++i)
                 if(!Character.isDigit(text.charAt(i)))
                    return ;
          }
          if(top<0){
             tfRet.setText("栈顶溢出!");
             return ;
          }
          Point pt=stackBtn[top].getLocation();
          stackBtn[top].setText(text);
          tfRet.setText("进栈值" + text);
          stackPointerLabel.setBounds(0, pt.y, 50, 50);
          --top;
       }
      public void mouseDragged(MouseEvent e){}
      public void mouseEntered(MouseEvent e){}
      public void mouseExited(MouseEvent e){}
      public void mouseMoved(MouseEvent e){}
      public void mouseReleased(MouseEvent e){}
      public void mousePressed(MouseEvent e){}
   }

   //pop按钮监听器
   class popAction extends MouseAdapter{
       public void mouseClicked(MouseEvent e){
          String text;
          if(top>=9){
            tfRet.setText("栈底溢出!");
            return ;
          }
          ++top;
          Point pt=stackBtn[top].getLocation();
          text=stackBtn[top].getText();
          tfRet.setText("出栈值" + text);
          stackBtn[top].setText("   ");
          stackPointerLabel.setBounds(0, pt.y, 50, 50);
       }
   }
   
   public static void main(String args[]){
      stackDemo mySstackDemo = new stackDemo();
   }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值