这是一个java模拟栈的编写
package net.etwo.cec.stack; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; /** * * @author etwo * */ public class ImitateStack { private JFrame jfMain;// 主窗体 private JPanel jpMain;// 面板 private JLabel jlShowStack, jlOneNum, jlOutStackState, jlState; private JTextField jtfShowStack, jtfOneNum, jtfOutStackState; private JButton jbtnOutStack, jbtnInStack, jbtnClearStack; public ImitateStack() { jfMain = new JFrame("栈模拟器"); jpMain = new JPanel(); jlState = new JLabel("栈模拟器!"); jlShowStack = new JLabel("栈显示情况:"); jlOneNum = new JLabel("请输入一个整数:"); jlOutStackState = new JLabel("出栈情况:"); jtfShowStack = new JTextField(50); jtfOneNum = new JTextField(30); jtfOutStackState = new JTextField(30); jbtnOutStack = new JButton("出 栈"); jbtnInStack = new JButton("进 栈"); jbtnClearStack = new JButton("清空栈"); jpMain.setLayout(null); jlState.setBounds(220, 20, 200, 20); jlShowStack.setBounds(29, 60, 100, 20); jtfShowStack.setBounds(133, 60, 250, 20); jbtnOutStack.setBounds(400, 60, 75, 20); jlOneNum.setBounds(3, 100, 150, 20); jtfOneNum.setBounds(133, 100, 250, 20); jbtnInStack.setBounds(400, 100, 75, 20); jlOutStackState.setBounds(42, 140, 150, 20); jtfOutStackState.setBounds(133, 140, 250, 20); jbtnClearStack.setBounds(400, 140, 75, 20); jpMain.add(jlState); jpMain.add(jlShowStack); jpMain.add(jtfShowStack); jpMain.add(jbtnOutStack); jpMain.add(jlOneNum); jpMain.add(jtfOneNum); jpMain.add(jbtnInStack); jpMain.add(jlOutStackState); jpMain.add(jtfOutStackState); jpMain.add(jbtnClearStack); jfMain.add(jpMain); jfMain.setSize(566, 370); jfMain.setLocationRelativeTo(null); jfMain.setVisible(true); jfMain.setDefaultCloseOperation(jfMain.EXIT_ON_CLOSE); // 设置栈本身不可编辑 jtfShowStack.setEnabled(false); // 进栈 jbtnInStack.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub actionMain(jtfOneNum, jbtnInStack); } }); // 栈清空 jbtnClearStack.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String strStack = jtfShowStack.getText(); if ("".equals(strStack)) { JOptionPane.showMessageDialog(jfMain, "栈已为空!"); } else { jtfShowStack.setText(""); } } }); // 出栈 jbtnOutStack.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String str = jtfShowStack.getText().trim(); if ("".equals(str)) { JOptionPane.showMessageDialog(jfMain, "栈已经为空"); } else { String[] strBuf = str.split(" "); for (int i = strBuf.length - 1; i >= 0; i--) { jtfOutStackState.setText(strBuf[0]); jtfShowStack.setText(str.substring(strBuf.length - i)); break; } } } }); } public void actionMain(JTextField jtfNum, JButton jbtnStack) { String numStack = jtfNum.getText().trim(); if ("".equals(numStack)) { JOptionPane.showMessageDialog(jfMain, "不能为空"); } else { try { Integer intStack = Integer.parseInt(numStack); jtfShowStack.setText(numStack + " " + jtfShowStack.getText()); jtfNum.setText(""); // jbtnStack.setEnabled(false); } catch (Exception e2) { // TODO: handle exception JOptionPane.showMessageDialog(jfMain, "请输入整数"); } } } public static void main(String[] args) { new ImitateStack(); } }