我正在为我的CS类编写一个程序,它应该是一个带有加/减/乘/除功能的简单计算器。在显示文本时,我希望它看起来尽可能类似于Windows计算器,一个方形显示窗口,文本右下方对齐。单击运算符时,它应显示在第二行,然后是缩进,然后是第二个数字。 插入数字和一切的代码工作正常。显示代码不是。我读到我应该使用HTML在标签中创建换行符,这给了我下面的代码:
private void display()
{
if (!(firstEntry == ""))
jDisplay.setText("<html>" + firstEntry + "</html>");
if (!(oper == ""))
jDisplay.setText("<html>" + jDisplay.getText() + "<br/>" + oper + "</html>");
if (!(secondEntry == ""))
jDisplay.setText("<html>" + jDisplay.getText() + " " + secondEntry + "</html>");
}
第一个数字显示正常,但是一旦我点击一个操作符,一切都会消失。我做错了什么?
答案 0 :(得分:0)
我的建议是为这个特殊需求创建一个新组件。这里有一个示例代码。
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
public class Calculator {
JPanel calcPanel;
Display display;
String[] operators={"+", "-", "*", "/", "="};
double currentValue=0;
String currentOperator="=";
public Calculator() {
initCalc();
}
private void initCalc() {
display=new Display();
int xPos=0;
int yPos=0;
calcPanel=new JPanel();
calcPanel.setLayout(new GridBagLayout());
calcPanel.add(display, new GridBagConstraints(xPos, yPos, 3, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
for(int i=0;i<=9;i++)
{
KeyStroke keyStroke=KeyStroke.getKeyStroke(i, 0);
AbstractAction action=new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
String value=display.getValue()+((Button) e.getSource()).getText();
display.setValue(value);
}
};
Button b=new Button(keyStroke, action, Integer.toString(i));
if(i%3==0)
{
xPos=0;
yPos++;
}
else
{
xPos++;
}
calcPanel.add(b, new GridBagConstraints(xPos, yPos, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
}
for(String s:operators)
{
KeyStroke keyStroke=KeyStroke.getKeyStroke(s);
AbstractAction action=new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if(currentOperator.equals("="))
{
display.clearDisplay();
}
String value=display.getValue();
if(e.getActionCommand().equals("="))
display.appendDisplay(value );
else
display.appendDisplay(value + " " +e.getActionCommand());
if(e.getActionCommand()=="=")
{
double dValue=Double.parseDouble(value);;
if(currentOperator.equals("+"))
{
currentValue+=dValue;
}
else if(currentOperator.equals("-"))
{
currentValue-=dValue;
}
else if(currentOperator.equals("*"))
{
currentValue*=dValue;
}
else if(currentOperator.equals("/"))
{
currentValue/=dValue;
}
display.setValue(Double.toString(currentValue));
}
else
{
currentValue=Double.parseDouble(value);
display.setValue(" ");
}
currentOperator=e.getActionCommand();
}
};
Button b=new Button(keyStroke, action, s);
calcPanel.add(b, new GridBagConstraints(xPos, yPos, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
xPos++;
if(xPos%3==0)
{
xPos=0;
yPos++;
}
}
}
public void showCalc()
{
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(calcPanel);
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
Calculator calc=new Calculator();
calc.showCalc();
}
class Display extends JComponent
{
JLabel displayLabel=new JLabel(" ");
JLabel valueLabel=new JLabel(" ");
String displayString;
public Display() {
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(displayLabel);
add(valueLabel);
setBorder(BorderFactory.createEtchedBorder());
displayLabel.setHorizontalAlignment(SwingConstants.RIGHT);
displayLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
valueLabel.setHorizontalAlignment(SwingConstants.RIGHT);
valueLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
}
public String getValue()
{
return valueLabel.getText();
}
public void setValue(String value)
{
valueLabel.setText(value);
}
public void appendDisplay(String value)
{
displayString=(displayString=="" || displayString==null)? value: (displayString) + "<br/>" + value ;
displayLabel.setText("<html>" + displayString+"</html>");
}
public void clearDisplay()
{
displayString=" ";
displayLabel.setText(displayString);
}
}
class Button extends JButton
{
public Button(KeyStroke keyStroke, AbstractAction action, String actionCommand) {
super(action);
setActionCommand(actionCommand);
setText(actionCommand);
getInputMap().put(keyStroke, "action");
}
}
}