我正在創建一個計算器,並希望有一個分隔數字的按鈕,以便獲得十進制數字。我是新手,一直在思考和尋找一個星期而沒有進步。我已經閱讀過關於NumberFormat和DecimalFormat的內容,但是我無法讓它們工作,坦率地說我不知道它們是否正確。如何獲得小數點分隔符?
import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.*;
/** skapar en enkel räknare med det vanligaste räknesätten
*Java Doc kommentarer för utförliga kommentarer för en längre applikation*/
public class CounterApplication extends JFrame implements ActionListener
{
private JTextField t1;
private JButton b1;
private JButton b2;
private JButton bIndex[] = new JButton[24];
private double tal = 0;
private double talTemp = 0;
private String sign;
private JPanel p1;
private JPanel p2;
private JPanel p3;
/** initierar Räknaren som en applet */
public CounterApplication()
{
setLayout(new BorderLayout());
setBackground(Color.lightGray);
p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(t1 = new JTextField(22));
t1.setFont(new Font("Sans Serif", Font.BOLD, 12));
t1.setEnabled(false);
t1.setText(""+tal);
p2 = new JPanel();
p2.setLayout(new GridLayout(4,6,4,4));
for(int i = 0; i<24; i++)
{
bIndex[i] = new JButton();
bIndex[i].addActionListener(this);
bIndex[i].setFont(new Font("Sans Serif", Font.BOLD, 16));
bIndex[i].setForeground(Color.blue);
p2.add(bIndex[i]);
}
bIndex[0].setLabel("7");
bIndex[1].setLabel("8");
bIndex[2].setLabel("9");
bIndex[3].setForeground(Color.red);
bIndex[3].setLabel("/");
bIndex[4].setLabel("sqrt");
bIndex[5].setLabel("sin");
bIndex[6].setLabel("4");
bIndex[7].setLabel("5");
bIndex[8].setLabel("6");
bIndex[9].setForeground(Color.red);
bIndex[9].setLabel("*");
bIndex[10].setLabel("%");
bIndex[11].setLabel("cos");
bIndex[12].setLabel("1");
bIndex[13].setLabel("2");
bIndex[14].setLabel("3");
bIndex[15].setForeground(Color.red);
bIndex[15].setLabel("-");
bIndex[16].setLabel("1/x");
bIndex[17].setLabel("log");
bIndex[18].setLabel("0");
bIndex[19].setLabel("+/-");
bIndex[20].setLabel(",");
bIndex[21].setForeground(Color.red);
bIndex[21].setLabel("+");
bIndex[22].setLabel("=");
bIndex[23].setLabel("ln");
p3 = new JPanel();
p3.setLayout(new GridLayout(1,2,4,4));
b1 = new JButton("CE");
b1.setFont(new Font("Sans Serif", Font.BOLD, 16));
b1.setForeground(Color.red);
p3.add(b1,"South");
b2 = new JButton("C");
b2.setFont(new Font("Sans Serif", Font.BOLD, 16));
b2.setForeground(Color.red);
p3.add(b2,"South");
b1.addActionListener(this);
b2.addActionListener(this);
add(p1,"North");
add(p2,"Center");
add(p3,"South");
setVisible(true);
pack();
}
/** styr händelsehanteringen i räknaren */
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == bIndex[20]) //Knappen ","
{
}
if(e.getSource() == bIndex[21]) //Knappen "+"
{
count();
sign = "+";
t1.setText("" + talTemp);
}
if(e.getSource() == bIndex[22]) //Knappen "="
{
count();
tal = talTemp;
sign = " ";
t1.setText("" + talTemp);
}
if(e.getSource() == bIndex[23]) //Knappen "ln"
{
/*count();
sign = "ln";*/
talTemp = tal;
tal = Math.log(talTemp);
t1.setText("" + tal);
}
if(e.getSource() == b1) //Knappen CE
{
tal = 0;
t1.setText("" + tal);
}
if(e.getSource() == b2) //Knappen C
{
tal = 0;
talTemp= 0;
t1.setText(" " + tal);
}
}
/** beräknar resultatet av den matematiska operationen */
public void count()
{
if(talTemp>0){
if(sign == "+")
tal = talTemp + tal;
if(sign == "-")
tal = talTemp - tal;
if(sign == "*")
tal = talTemp * tal;
if(sign == "/")
tal = talTemp/tal;
if(sign == "%")
tal = talTemp * 0.01;
if(sign == "sqrt")
tal = Math.sqrt(talTemp);
if(sign == "1/x")
tal = 1 /talTemp;
if(sign == "=")
tal = talTemp + tal;
if(sign == "sin")
tal = Math.sin(talTemp);
if(sign == "cos")
tal = Math.cos(talTemp);
if(sign == "log")
tal = Math.log(talTemp);
}
talTemp = tal;
tal = 0;
}
public static void main(String args[])
{
CounterApplication c = new CounterApplication();
}
}
發表你的嘗試和你想要的輸出的代碼示例。 – kevcodez
_I無法讓他們工作_ 究竟是什麼問題?你可以說得更詳細點嗎? –
喜歡,我不知道如何使用以及如何解決它們。 @SaurabhSrivastava –