创建堆栈GUI程序

时间:2017-03-10 18:30:31

标签: java user-interface stack

所以我正在创建一个GUI Java程序,用户可以在其中输入一个数字到文本字段中,然后将其推送到堆栈中,该堆栈显示在框架的某个位置。

目前我有GUI所有设置,它只是推动,弹出和窥视我正在努力的堆栈。

我知道堆栈是如何工作的(整个最后进入,先出概念)然而我无法理解如何将这个概念应用于文本字段和按钮!

我对Java GUI的了解非常差,所以我非常感谢一些指导,因为我读过的所有指南都没有帮助我将这些应用到我的问题中!这是当前的代码,我必须解释一下tabbedpane将来会用于进一步扩展程序,但它只是我目前正在处理的Stack选项卡。

这就是我到目前为止,我已经从代码顶部删除了导入,只是为了整理这篇文章中的内容,但是如果需要可以再添加它们:

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

public abstract class DefaultFrame implements ActionListener {

public static Stack<Integer> stack;

public static void main(String[] args) {
    JFrame frame = new JFrame("Data Container");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1080, 720);

    JTabbedPane pane = new JTabbedPane();
    StackPanel stackPanel = new StackPanel();
    pane.addTab("Stack", stackPanel);
    stackPanel.setLayout(new BorderLayout(0, 0));

    JTextPane stacktext = new JTextPane();
    stacktext.setText(
            "A Stack in Java is a container for variables, similar to an Array. It could be visualised as a stack of plates or books, where you would need to remove plates in order to access plates that are further down the stack. A Stack uses these keywords; Push, Pop and Peek.\r\n\r\nPush: Pushes another variable to the top of the stack.\r\nPop: Removes the top variable from the stack.\r\nPeek: Returns the variable at the top of the stack");
    stackPanel.add(stacktext, BorderLayout.SOUTH);

    JPanel function = new JPanel();
    stackPanel.add(function, BorderLayout.WEST);
    function.setSize(540, 980);

    JTextField txtinput = new JTextField();
    function.add(txtinput);
    txtinput.setColumns(10);
    String number = txtinput.getText();


    JButton btnPush = new JButton("Push");
    function.add(btnPush);
    btnPush.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            push();
        }

        private void push() {

            int input = Integer.parseInt(number);
            stack.push(input);

        }
    });

    JButton btnPop = new JButton("Pop");
    function.add(btnPop);

    JButton btnPeek = new JButton("Peek");
    function.add(btnPeek);

    JPanel display = new JPanel();
    stackPanel.add(display, BorderLayout.CENTER);

    pane.addTab("Queue", new QueuePanel());
    pane.addTab("Set", new SetPanel());
    pane.addTab("Tree", new TreePanel());
    pane.addTab("List", new ListPanel());
    pane.addTab("Stack", new StackPanel());

    frame.getContentPane().add(pane);
    frame.setVisible(true);

}

}

1 个答案:

答案 0 :(得分:0)

首先,您必须初始化stack

public static Stack<Integer> stack = new Stack<>();//initialize stack

然后你必须添加ActionListener并将想要的行为放入其中。所以对于按钮,它看起来像

JTextArea jTextArea = new JTextArea(20, 1);//To display pushed values
jTextArea.setEditable(false);

JButton btnPush = new JButton("Push");
function.add(btnPush);
btnPush.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String input = txtinput.getText();//first get the input
        if (!input.isEmpty()){//check that inout is not empty
            stack.push(Integer.parseInt(input));//push on stack
            System.out.println("input = " + input);
            jTextArea.setText("");//clear TextArea
            stack.forEach(element -> jTextArea.append(element + System.lineSeparator()));//for every element in stack add (element + \n)
        }else{
            System.out.println("Input is empty");
        }
    }
});

要重复使用JTextArea - 更新,您应该像以下一样创建ActionListener

JTextArea jTextArea = new JTextArea(20, 1);//To display pushed values
jTextArea.setEditable(false);

JButton btnPush = new JButton("Push");
JButton btnPop = new JButton("Pop");
JButton btnPeek = new JButton("Peek");
JButton btnUpdate = new JButton("Reload stack");

ActionListener updateListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        jTextArea.setText("");//clear TextArea
        stack.forEach(x -> jTextArea.append(x + System.lineSeparator()));//for every element in stack add (element + \n)
    }
};
btnUpdate.addActionListener(updateListener);
btnPush.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String input = txtinput.getText();
        if (!input.isEmpty()) {
            stack.push(Integer.parseInt(input));
            System.out.println("input = " + input);                    
            updateListener.actionPerformed(new ActionEvent(btnPush, 0, ""));//call the updateListener to refresh jTextArea
        } else {
            System.out.println("Input is empty");
        }

    }
});
function.add(btnPush);
function.add(btnPop);
function.add(btnPeek);

function.add(jTextArea);
function.add(btnUpdate);

在弹出操作中使用updateListener.actionPerformed(new ActionEvent(btnPush, 0, ""));

要澄清:

stack.forEach(element -> jTextArea.append(element + System.lineSeparator()));

也可以写成:

for(Enumeration<Integer> elements = stack.elements();elements.hasMoreElements();){            
    jTextArea.append(elements.nextElement() + System.lineSeparator());
}
//or    
for(int i = 0; i < stack.size(); i++) {
    jTextArea.append(stack.get(i) + System.lineSeparator());
}
//or    
for (Integer aStack : stack) {
    jTextArea.append(aStack + System.lineSeparator());
}