gpt4 book ai didi

java - BlueJ 将计算器小程序连接到两个类

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:51 25 4
gpt4 key购买 nike

我遇到了一个很可能很容易解决的问题,但我已经搜索了很长时间但找不到答案。我希望使用 blueJ 小程序类来制作一个基本计算器,它只需获取不同类的字符串值并将它们吐入一个界面中。

我的小程序看起来像这样;

    import acm.program.*;
import acm.gui.*;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.event.*;
import java.awt.Color;


public class Calculator extends Program
{
private JTextField infix, postfix, result;
private JLabel infixLabel, postfixLabel, resultLabel;
private JButton goButton, clearButton;

public Calculator()
{
start();
setSize(400, 200);
setBackground(Color.GREEN);
}

public void init()
{
TableLayout table = new TableLayout(4, 2);
setLayout(table);

infix = new JTextField();
postfix = new JTextField();
result = new JTextField();

postfix.setEditable(false);
result.setEditable(false);

Dimension d = infix.getPreferredSize();
d.setSize(200, d.getHeight());
infix.setPreferredSize(d);

infixLabel = new JLabel("<html><b>infix</b></html>");
infixLabel.setForeground(Color.WHITE);
postfixLabel = new JLabel("<html><b>postfix</b></html>");
postfixLabel.setForeground(Color.WHITE);
resultLabel = new JLabel("result");
resultLabel.setForeground(Color.WHITE);

goButton = new JButton("Go!");
goButton.setActionCommand("Go!");
clearButton = new JButton("Clear");
clearButton.setActionCommand("Clear");

add(infixLabel);
add(infix);
add(postfixLabel);
add(postfix);
add(resultLabel);
add(result);
add(goButton);
add(clearButton);

addActionListeners();
}

public void actionPerformed(ActionEvent ae)
{
String infixString = ""; // Change this line
String expression = ""; // Change this line
String postfixString;

String what = ae.getActionCommand();
if (what.equals("Clear"))
{
infix.setText("");
postfix.setText("");
result.setText("");
}
else if (what.equals("Go!"))
{
postfixString = ""; //Change this line
postfix.setText("" + postfixString);
result.setText("" + expression); // Change this line
}
}
}

这是一项作业,凡是写着“//更改此行”的地方都是我必须更改的部分。我第一天不在类里面讨论这个问题,我在网上搜索了任何答案。基本上我有两个名为 infixToPostfix 的类,它返回一个“字符串中缀”,基本上将其按后缀顺序排列,而evaluatePostfix 返回一个计算后缀表达式的“字符串后缀”。

我该如何将它们连接到我的小程序?

最佳答案

不用初始化所有这些包,只需使用该行

import java.io.*:

这基本上可以访问所有包另外,由于您继承了一个类,该类也必须在程序中,所以这个答案有点不完整,但一旦我理解了整个事情,我就会编辑

关于java - BlueJ 将计算器小程序连接到两个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33382171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com