Java计算器中的多行标签
我目前正在为我的CS类编写一个程序,它应该是一个简单的计算器,具有加/减/乘/除功能。在显示文本时,我希望它看起来尽可能类似于Windows calculator,一个文本在右下角对齐的方形显示窗口。单击运算符时,它应显示在第二行,后面是缩进,然后是第二个数字。 插入数字的代码和一切都很好。显示代码不正确。我读到应该使用HTML在标签中创建换行符,这给了我以下代码:Java计算器中的多行标签,java,swing,jlabel,Java,Swing,Jlabel,我目前正在为我的CS类编写一个程序,它应该是一个简单的计算器,具有加/减/乘/除功能。在显示文本时,我希望它看起来尽可能类似于Windows calculator,一个文本在右下角对齐的方形显示窗口。单击运算符时,它应显示在第二行,后面是缩进,然后是第二个数字。 插入数字的代码和一切都很好。显示代码不正确。我读到应该使用HTML在标签中创建换行符,这给了我以下代码: private void display() { if (!(firstEntry == ""))
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>");
}
private void display()
{
如果(!(firstEntry==“”)
jDisplay.setText(“+firstEntry+”);
如果(!(操作==“”)
jDisplay.setText(“+jDisplay.getText()+”
“+oper+”);
如果(!(第二项==“”)
jDisplay.setText(“+jDisplay.getText()+”+secondEntry+”);
}
第一个数字显示得很好,但只要我单击一个操作符,一切都会消失。我做错了什么?我的建议是为这种特殊需要创建一个新组件。下面是一个示例代码
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");
}
}
}
导入java.awt.gridbag约束;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.ActionEvent;
导入javax.swing.AbstractAction;
导入javax.swing.BorderFactory;
导入javax.swing.BoxLayout;
导入javax.swing.JButton;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.KeyStroke;
导入javax.swing.SwingConstants;
公共类计算器{
杰帕内尔·卡尔帕内尔;
显示;
字符串[]运算符={“+”、“-”、“*”、“/”、“=”};
双电流值=0;
字符串currentOperator=“=”;
公共计算器(){
initCalc();
}
私有void initCalc(){
显示=新显示();
int xPos=0;
int-yPos=0;
calcPanel=新的JPanel();
setLayout(新的GridBagLayout());
添加(显示,新的GridBagConstraints(xPos、YPO、3、1、1.0、1.0、GridBagConstraints.CENTER、GridBagConstraints.BOTH、新的插入(5、5、5、5)、0、0));
对于(inti=0;iMaybe),问题是当执行第二个if时,jDisplay.getText()is已经包含html标记,您可以再次将其放入html标记中。将JTextArea用于纯文本,将JTextPane用于装饰内容,必须使用新LineAfik的逻辑:不能将JLabel包含多行。您必须使用@mKorbel.@user979349有效!Instea所述的JTextArea
或JTextPane
在使用getText()的过程中,我只是再次连接了变量。@Kylopardiun您可以有一个JLabel
,包含任意多行。