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 }
?