首页 > 代码库 > java的栈图形演示

java的栈图形演示

  1 import java.awt.*;
  2 import javax.swing.*;
  3 import java.awt.event.*;
  4 /*
  5 指示发生了组件定义的动作的语义事件。当特定于组件的动作(比如被按下)发生时,由组件(比如 Button)生成此高级别事件。
  6 事件被传递给每一个 ActionListener 对象,这些对象是使用组件的 addActionListener 方法注册的,用以接收这类事件。 
  7 所以在给TextField类添加 ActionListener 类型的监听器时就会失败!
  8 
  9 ****下面还有XXXListener和XXXAdapter的用法,相信你会喜欢上XXXAdapter的用法
 10 */
 11 public class stackDemo extends MouseAdapter{
 12    JFrame fr=new JFrame("StackDemo");//对话框
 13    JPanel pan= new JPanel();//菜单面板
 14    JPanel panStack = new JPanel();
 15    JButton pushBtn, popBtn, peekBtn;
 16    JTextField tf= new JTextField("整数", 4);
 17    JButton stackBtn[]= new JButton[10];
 18    
 19    JPanel panStackPointerLabel= new JPanel();
 20    JLabel stackPointerLabel = new JLabel("<-top");
 21 
 22    JPanel panRet= new JPanel();   
 23    JTextField tfRet= new JTextField("操作结果!");
 24 
 25    int top;
 26    
 27    public stackDemo(){
 28       fr.setSize(420,500);
 29       fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 30       fr.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
 31 
 32       pan.setPreferredSize(new Dimension(400, 50));
 33       pan.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
 34       panStack.setPreferredSize(new Dimension(80, 350));//设置栈面板大小
 35       panStack.setBackground(Color.yellow);
 36       pan.setBackground(Color.blue);
 37       pan.add(new JLabel("操作菜单:"));
 38       pan.add(pushBtn=new JButton("进栈"));
 39       pushBtn.addMouseListener(new pushAction());
 40       pan.add(popBtn=new JButton("出栈"));
 41       popBtn.addMouseListener(new popAction());
 42       pan.add(peekBtn=new JButton("栈顶元素"));
 43       
 44       tf.addMouseListener(this);
 45       pan.add(tf);
 46           
 47       for(int i=0; i<10; ++i){
 48          stackBtn[i]=new JButton("   ");
 49          panStack.add(stackBtn[i]);
 50       }
 51       fr.add(pan);
 52       fr.add(panStack);
 53       panStackPointerLabel.setLayout(null);
 54       panStackPointerLabel.setPreferredSize(new Dimension(80, 350));//设置指针面板的大小
 55       panStackPointerLabel.setBackground(Color.LIGHT_GRAY);
 56       stackPointerLabel.setFont(new Font("华文行楷", Font.BOLD, 20));
 57       panStackPointerLabel.add(stackPointerLabel);
 58 
 59       fr.add(panStackPointerLabel);
 60       panRet.setLayout(new FlowLayout(FlowLayout.LEFT));
 61       panRet.setBackground(Color.red);
 62       panRet.setPreferredSize(new Dimension(400, 50));
 63       
 64       tfRet.setEditable(false);//不能不编辑
 65       panRet.add(tfRet);//操作结果面板
 66       fr.add(panRet);
 67       fr.setVisible(true);
 68       stackPointerLabel.setBounds(0, stackBtn[9].getLocation().y, 50, 50);//设置栈顶指针位置
 69       top=9;
 70    }
 71 
 72    public void mouseClicked(MouseEvent e){
 73        tf.selectAll();//鼠标单击时选中全部文本
 74    }
 75    
 76    //push 按钮监听器
 77    class pushAction implements MouseListener{
 78        public void mouseClicked(MouseEvent e){
 79           String text;
 80           
 81           if((text=tf.getText())!="   "){
 82               for(int i=0; i<text.length(); ++i)
 83                  if(!Character.isDigit(text.charAt(i)))
 84                     return ;
 85           }
 86           if(top<0){
 87              tfRet.setText("栈顶溢出!");
 88              return ;
 89           }
 90           Point pt=stackBtn[top].getLocation();
 91           stackBtn[top].setText(text);
 92           tfRet.setText("进栈值" + text);
 93           stackPointerLabel.setBounds(0, pt.y, 50, 50);
 94           --top;
 95        }
 96       public void mouseDragged(MouseEvent e){}
 97       public void mouseEntered(MouseEvent e){}
 98       public void mouseExited(MouseEvent e){}
 99       public void mouseMoved(MouseEvent e){}
100       public void mouseReleased(MouseEvent e){}
101       public void mousePressed(MouseEvent e){}
102    }
103 
104    //pop按钮监听器
105    class popAction extends MouseAdapter{
106        public void mouseClicked(MouseEvent e){
107           String text;
108           if(top>=9){
109             tfRet.setText("栈底溢出!");
110             return ;
111           }
112           ++top;
113           Point pt=stackBtn[top].getLocation();
114           text=stackBtn[top].getText();
115           tfRet.setText("出栈值" + text);
116           stackBtn[top].setText("   ");
117           stackPointerLabel.setBounds(0, pt.y, 50, 50);
118        }
119    }
120    
121    public static void main(String args[]){
122       stackDemo mySstackDemo = new stackDemo();
123    }
124 }