Java - 'podsłuch' na przyciski w kalkulatorze - jak skrocic?

0

Zaczynam robic swoj pierwszy program z JavaFX i teraz pytanie - czy daloby rade skrocic ten kod? Probowalem zrobic to petla, ale nie za bardzo mi to wychodzilo..

 public class SampleController implements Initializable {

	@FXML
	private Button zero, one, two, three, four, five, six, seven, eight, nine, add, sub, div, mult, clear, result;

	@FXML
	public TextField textSpace;
	

	


	@Override
	public void initialize(URL arg0, ResourceBundle arg1){{
		
	}
	
	zero.setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("0");
		}});
	one.setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("1");
		}});
	two.setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("2");
		}});
	three.setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("3");
		}});
	four.setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("4");
		}});
	five.setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("5");
		}});
	six.setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("6");
		}});
	seven.setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("7");
		}});
	eight.setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("8");
		}});
	nine.setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("9");
		}});
	
	
	
	}}
0

A co dokładnie Ci nie wychodziło?

0

wychodzilo wszystko, tzn podsluch przyciskow dziala, chodzi o to czy da sie to w inny sposob (krotszy) zapisac czy tak zostawic?

0

ah, o petle pytales, zaraz wkleje kod

0
@Override
	public void initialize(URL arg0, ResourceBundle arg1){{
	
	}
		
	Button numbers[] = new Button[] {zero, one, two, three, four, five, six, seven, eight, nine};
	Button options[] = new Button[] {add, sub, div, mult, clear, result};
	
	for (int i=0; i<=9; i++){
	numbers[i].setOnAction(new EventHandler<ActionEvent>(){
		public void handle(ActionEvent event){
			textSpace.setText("0"+(i++));
		}});

w ten sposob probowalem, ale jest blad "Local variable i defined in an enclosing scope must be final or effectively final"

0

Iterator faktycznie nie może być final, ale można to spróbować obejść np. tak:

		final List<Integer> a=new ArrayList(Arrays.asList(new Integer[]{1,2,3,4}));
		
		for(int i=0;i<4;i++){
			final Integer in = a.get(i);
			new X(){public int get(){return a.indexOf(in);}};
		}
0

Do modyfikacji pola tekstowego używasz chyba złej metody, Ty chcesz dopisać cyfrę, a nie zamienić dotychczasowy tekst nową cyfrą. Powinieneś użyć metody appendText. Ze zmiennych zero,one,..,nine też można zrezygnować.

        EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>()
        {
            public void handle(ActionEvent event)
            {
                Button src = (Button)(event.getSource());
                textSpace.appendText(src.getText());
            }
        };
        Button[] numbers = new Button[10];
        for(int i=0;i<=numbers.length;i++)
        {
            numbers[i] = new Button(""+i);
            numbers[i].setOnAction(handler);
        }
0

Dzieki, chociaz zostawilem tak jak bylo wczesniej.. pojawil sie kolejny problem, poza metoda initialize napisalem to:

@Override
	private void handleNumbers(ActionEvent event){
		String number = ((Button)event.getSource()).getText();
		String oldNumber = textSpace.getText();
		String newNumber = oldNumber + number;
		textSpace.setText(newNumber);
	}

"The method handleNumbers(ActionEvent) of type SampleController must override or implement a supertype method"

a jak usuwam @Override to wyskakuje, ze "method is never used locally"

0

Co to za klasa SampleController?
Skróć kod

    private void handleNumbers(ActionEvent event){
        String number = ((Button)event.getSource()).getText();
        textSpace.appendText(number);
    }
0

Domyslna nazwa kiedy nowy projekt JavaFX robie, a w niej dodaje wlasnie podsluchy na przyciski, wczesniejszy kod tej klasy:

 public class SampleController implements Initializable {
	@FXML
	private TextField textSpace;
	
	@FXML
	private Button zero, one, two, three, four, five, six, seven, eight, nine, add, sub, div, mult, clear, result;

@Override
public void initialize(URL arg0, ResourceBundle arg1){
	zero.setOnAction(new EventHandler<ActionEvent>(){
        public void handle(ActionEvent event){
            textSpace.setText("0");
        }});
[ ...reszta przyciskow... ]
}
@Override
	@Override
	private void handleNumbers(ActionEvent event){
		String number = ((Button)event.getSource()).getText();
        textSpace.appendText(number);
}

ten blad to podobno moze byc wina kompilatora, ale nie wiem ile w tym prawdy..

1 użytkowników online, w tym zalogowanych: 0, gości: 1