Вопрос Подскажите с кодом Java, только начал учиться

Регистрация
10 Ноя 2013
Сообщения
93
Репутация
0
Спасибо
0
Монет
0
public static void main(String[] args) {
// TODO Auto-generated method stub
okno nf=new okno();
}
}
class okno extends JFrame
{
private JTextArea text;
private double p1=0,p2=0;
private int oper=0;
private void btnclick(JButton btn)
{
String str=btn.getText();
//JOptionPane.showMessageDialog(null, str);
if (str =="EXIT")
{
System.exit(0);
}
else if (str=="c")
{
p1=0;p2=0;text.setText("");
}
else if(str=="+")
{
oper=1;
p1 = Double.parseDouble(text.getText());
text.setText("");
}
else if(str=="-")
{
oper=2;
p1 = Double.parseDouble(text.getText());
text.setText("");
}
else if(str=="*")
{
oper=3;
p1 = Double.parseDouble(text.getText());
text.setText("");
}
else if(str=="/")
{
oper=4;
p1 = Double.parseDouble(text.getText());
text.setText("");
}
else if(str=="=")
{
p2 = Double.parseDouble(text.getText());
if(oper==1)
{
text.setText(""+(p1+p2));
}
else if(oper==2)
{
text.setText(""+(p1-p2));
}
else if(oper==3)
{
text.setText(""+(p1*p2));
}
else if(oper==4)
{
text.setText(""+(p1/p2));
}
else if(oper==5)//Факториал
{
int sum=0;
for(int i = 1;i<=p2;i++)
sum=sum*i;
text.setText(""+(sum));
}
}
else
{
text.setText("" + text.getText() + str);

}

//JOptionPane.showMessageDialog(null, "Бутерброд");
}
public okno()
{
Container cont = getContentPane();
JPanel pan = new JPanel();
pan.setLayout(null);
Font btnFont = new Font("serif",0,20);
Font labFont = new Font("arial",1,30);
Font textFont = new Font("arial",2,30);
JButton[] btn = new JButton[17];
for (int i=0;i<17;i++)
{
btn = new JButton();

btn.setSize(100,25);
btn.setFont(btnFont);
btn.setLocation(30,50 + 30*i);
pan.add(btn);

btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
btnclick((JButton)e.getSource());
}});
}
for(int i=0;i<10;i++)btn.setText(""+i);
btn[10].setText("+");
btn[11].setText("-");
btn[12].setText("/");
btn[13].setText("*");
btn[14].setText("=");
btn[15].setText("c");
btn[16].setText("EXIT");
btn[17].setText("!");
JLabel lab = new JLabel("Resultat: ");
lab.setFont(labFont);
lab.setBounds(130,0,300,50);
pan.add(lab);
text = new JTextArea();
text.setFont(textFont);
text.setBounds(300,10,300,35);
text.setForeground(new Color(0,0,100));
text.setBackground(Color.WHITE);
pan.add(text);
cont.add(pan);
setBounds(0,0,800,600);
setTitle ("Calculator");
setVisible(true);
}
}
 
ArrayIndexOutOfBoundsException - учись читать ошибки, она тебе прям орет в чем проблема. Array + Index + Out переведи эти 3 слова с англа... JButton[] btn = new JButton[17]; .... btn[17].setText("!"); 17го индекса не существует. Есть 0 1 2...16
 
Назад
Сверху