我的标签未显示在GUI前缀表达式程序中

时间:2013-04-08 15:48:25

标签: java swing user-interface label expression

我正在使用GUI作为前缀表达式程序。程序本身工作正常,但GUI有点问题。当我运行它时,结果标签不会显示。我已经尝试了一切,但我不知道为什么它不起作用。任何帮助将不胜感激。谢谢!

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


public class PrefixExpression extends JFrame
{
    private JPanel panel;
    private JTextField expression;
    private JLabel title;
    private JLabel instruction;
    private JLabel results;
    private final int WIDTH = 310;
    private final int HEIGHT = 175;
    private JButton calculateBtn;

    public PrefixExpression()
    {
        setTitle("Prefix Expression Calculator");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);
    }

    private void buildPanel()
    {
        title = new JLabel("Calculating Prefix Expressions");
        instruction = new JLabel("Please enter your expression");
        expression = new JTextField(10);
        calculateBtn = new JButton("Calculate");
        calculateBtn.addActionListener(new ButtonListener());

        panel = new JPanel();
        panel.add(title);
        panel.add(instruction);
        panel.add(expression);
        panel.add(calculateBtn);

    }


    public static void main(String[] args)
    {
        PrefixExpression main = new PrefixExpression();
    }



    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            Stack <Integer> numberStack = new Stack <Integer>();
            Stack <Character> operatorStack = new Stack <Character>();


            String input = expression.getText();

            StringTokenizer strToken = new StringTokenizer(input, " ", false);


            while (strToken.hasMoreTokens())
            {
                String i = strToken.nextToken();
                int number;
                char operator;

                try
                {
                    number = Integer.parseInt(i);
                    numberStack.push(number);
                }
                catch (NumberFormatException nfe)
                {
                    operator = i.charAt(0);
                    operatorStack.push(operator);
                }
            }

            int answer = calculateMethod(numberStack, operatorStack);
            results = new JLabel("Here is your answer: " + answer); 
            panel.add(results);  //This is where the results label is added to panel
     }

        public int calculateMethod(Stack <Integer> numberStack, Stack <Character> operatorStack)
        {
            int result = 0;
            while(! numberStack.empty())
            {
                char operator = (char) operatorStack.pop();
                if (operator == '+')
                    result = addition(numberStack);
                else if (operator == '*')
                    result = multiplication(numberStack);
            }

            return result;
        }

        public int addition(Stack <Integer> numberStack)
        {
            int numb1 = (int) numberStack.pop();
            int numb2 = (int) numberStack.pop();
            int answer = numb1 + numb2;
            return answer;
        }

        public int multiplication(Stack <Integer> numberStack)
        {
            int numb1 = (int) numberStack.pop();
            int numb2 = (int) numberStack.pop();
            int answer = numb1 * numb2;
            return answer;
        }
    }
}

3 个答案:

答案 0 :(得分:2)

  

panel.add(结果); //这是将结果标签添加到面板

的位置

将组件添加到可见GUI时,您需要执行以下操作:

panel.revalidate();
panel.repaint();

这将调用布局管理器并适当调整组件的大小。

答案 1 :(得分:0)

我会将JPanel的布局更改为流布局以外的其他布局,在计算结果之前创建结果JLabel并将其添加到面板中。然后,您需要使用results.setText(answer);,以便在完成后显示结果,除非您希望在面板中显示所有结果。

答案 2 :(得分:-1)

        panel.add(results);  //This is where the results label is added to panel
        revalidate();