Witam. Otóż mam mały problem.
Mam funkcje ktora zamienia liczbę dziesiętna na binarna i zwraca ja w postaci String. Oto ona:
public String getBinary(String value)
{
int dec=Integer.parseInt(value);
String binary="";
while(dec > 0)
{
int rest=dec%2;
dec/=2;
binary=rest+binary;
}
return binary;
}
I teraz value przekazuje do niej poprzez wywolanie metody getText() z klasy JButton
public class BinaryAction implements ActionListener
{
@Override public void actionPerformed(ActionEvent event)
{
CalculatorPanel.this.setEnabledButton(false);
//Zmieniamy etykiete przycisku
//Usuwamy stary sluchacz i dodajemy nowy
JButton button= CalculatorPanel.this.buttons.get(11);
button.setLabel("Dec");
button.removeActionListener(CalculatorPanel.this.binary);
button.addActionListener(CalculatorPanel.this.decimal);
//Wykonujemy zamiane
CalculatorPanel.this.display.setText("");
String bin=CalculatorPanel.this.getBinary(CalculatorPanel.this.display.getText());//Tutaj
CalculatorPanel.this.display.setText(bin);
}
}
Niestety cos jest nie tak. Program sie kompiluje. w pewnym sensie dziala lecz jak przyjdzie do wywolania tej funkcji to sie zalamuje i dostaje bombe:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at CalculatorPanel.getBinary(Calculator.java:121)
at CalculatorPanel$BinaryAction.actionPerformed(Calculator.java:188)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Jeżeli do tej funkcji recznie wpisze np 16 to wszystko dziala jak powinno
Tutaj cały kod
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Calculator
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override public void run()
{
CalculatorFrame frame=new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
this.setTitle("Binarny");
CalculatorPanel pan=new CalculatorPanel();
this.add(pan);
this.pack();
}
}
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
//Konstrukcja i nadanie poczatkowych wartosci zmiennym
this.start=true;
this.buttons=new ArrayList<>();
this.setLayout(new BorderLayout());
this.display=new JButton("0");
this.display.setEnabled(false);
this.add(this.display,BorderLayout.NORTH);
this.panel=new JPanel();
this.panel.setLayout(new GridLayout(4,3));
//Tworzenie przycisków
insert = new InsertAction();
decimal = new DecimalAction();
binary = new BinaryAction();
this.makeButton("7",insert);
this.makeButton("8",insert);
this.makeButton("9",insert);
this.makeButton("4",insert);
this.makeButton("5",insert);
this.makeButton("6",insert);
this.makeButton("1",insert);
this.makeButton("2",insert);
this.makeButton("3",insert);
this.makeButton("0",insert);
this.makeButton(".",insert);
this.makeButton("Bin",binary);
this.add(this.panel,BorderLayout.CENTER);
}
/**
* Tworzenie przyciskow
* @param label Opis przycisku
* @param listener Sluchacz do przycisku
*/
public void makeButton(String label,ActionListener listener)
{
JButton button=new JButton(label);
button.addActionListener(listener);
this.panel.add(button);
this.buttons.add(button);
}
/**
* Ustawianie stanu przycisku
* @param statement wartosc
* true-wlaczony
* false-wylaczony
*/
public void setEnabledButton(boolean statement)
{
for(int i=0;i<11;i++)
this.buttons.get(i).setEnabled(statement);
this.buttons.get(6).setEnabled(true);
this.buttons.get(9).setEnabled(true);
}
/**
* Zamienia liczbe dziesietna na binarna
* @param value wartosc do zamiany
* @return Liczba Binarna w postaci lancucha(String)
*/
public String getBinary(String value)
{
int dec=Integer.parseInt(value);
String binary="";
while(dec > 0)
{
int rest=dec%2;
dec/=2;
binary=rest+binary;
}
return binary;
}
/**
* Wewnetrzna klasa nasluchujaca.
* Dodaje tresc do przycisku
*/
public class InsertAction implements ActionListener
{
@Override public void actionPerformed(ActionEvent event)
{
String input=event.getActionCommand();
if(start)
{
CalculatorPanel.this.display.setText("");
CalculatorPanel.this.start=false;
}
CalculatorPanel.this.display.setText(CalculatorPanel.this.display.getText()+input);
}
}
//Odpowiada za postac kalkulatora w trybie dziesietnym
public class DecimalAction implements ActionListener
{
@Override public void actionPerformed(ActionEvent event)
{
CalculatorPanel.this.setEnabledButton(true);
//Zmieniamy etykiete przycisku
//Usuwamy stary sluchacz i dodajemy nowy
JButton button= CalculatorPanel.this.buttons.get(11);
button.setLabel("Bin");
button.removeActionListener(CalculatorPanel.this.decimal);
button.addActionListener(CalculatorPanel.this.binary);
}
}
//Odpowiada za postac kalkulatora w trybie dziesietnym
public class BinaryAction implements ActionListener
{
@Override public void actionPerformed(ActionEvent event)
{
CalculatorPanel.this.setEnabledButton(false);
//Zmieniamy etykiete przycisku
//Usuwamy stary sluchacz i dodajemy nowy
JButton button= CalculatorPanel.this.buttons.get(11);
button.setLabel("Dec");
button.removeActionListener(CalculatorPanel.this.binary);
button.addActionListener(CalculatorPanel.this.decimal);
//Wykonujemy zamiane
CalculatorPanel.this.display.setText("");
String bin=CalculatorPanel.this.getBinary(CalculatorPanel.this.display.getText());
CalculatorPanel.this.display.setText(bin);
}
}
//Pola
private JPanel panel;
private JButton display;
private ArrayList<JButton> buttons;
private boolean start;
private InsertAction insert;
private DecimalAction decimal;
private BinaryAction binary;
//CalculatorPanel.this.
}