Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im trying to create a gui calculator in Java. I cant figure out how to let my label accept multiple inputs to calculate? public class

Im trying to create a gui calculator in Java. I cant figure out how to let my label accept multiple inputs to calculate?

public class assignment3 extends Application

{ int ac=0,ad=0; public void start(Stage s) { s.setTitle("Calculator"); VBox x = new VBox(); Label l1 = new Label (""); Label l2 = new Label(""); Button zero = new Button("0"); Button one = new Button("1"); Button two = new Button("2"); Button three = new Button("3"); Button four = new Button("4"); Button five = new Button("5"); Button six = new Button("6"); Button seven = new Button("7"); Button eight = new Button("8"); Button nine = new Button("9"); Button add = new Button("+"); Button sub = new Button("-"); Button div = new Button("/"); Button mul = new Button("*"); Button equal = new Button("="); x.getChildren().add(l1); x.getChildren().add(l2); x.getChildren().add(zero); x.getChildren().add(one); x.getChildren().add(two); x.getChildren().add(three); x.getChildren().add(four); x.getChildren().add(five); x.getChildren().add(six); x.getChildren().add(seven); x.getChildren().add(eight); x.getChildren().add(nine); x.getChildren().add(add); x.getChildren().add(sub); x.getChildren().add(div); x.getChildren().add(mul); x.getChildren().add(equal); EventHandler event = new EventHandler() { public void handle(ActionEvent e) { l1.setText("0"); } }; EventHandler event1 = new EventHandler() { public void handle(ActionEvent e) { l1.setText("1"); } }; EventHandler event2 = new EventHandler() { public void handle(ActionEvent e) { l1.setText("2"); } }; EventHandler event3 = new EventHandler() { public void handle(ActionEvent e) { l1.setText("3"); } }; EventHandler event4 = new EventHandler() { public void handle(ActionEvent e) { l1.setText("4"); } }; EventHandler event5 = new EventHandler() { public void handle(ActionEvent e) { l1.setText("5"); } }; EventHandler event6 = new EventHandler() { public void handle(ActionEvent e) { l1.setText("6"); } }; EventHandler event7 = new EventHandler() { public void handle(ActionEvent e) { l1.setText("7"); } }; EventHandler event8 = new EventHandler() { public void handle(ActionEvent e) { l1.setText("8"); } }; EventHandler event9 = new EventHandler() { public void handle(ActionEvent e) { l1.setText("9"); } }; EventHandler eventadd = new EventHandler() { public void handle(ActionEvent e) { String a = l1.getText(); ac = Integer.parseInt(a); String b = l1.getText(); ad = Integer.parseInt(b); int r=ad+ac; String res=Integer.toString(r); l1.setText(res); } }; EventHandler eventsub = new EventHandler() { public void handle(ActionEvent e) { String a = l1.getText(); ac = Integer.parseInt(a); String b = l1.getText(); ad = Integer.parseInt(b); int r=ad-ac; String res=Integer.toString(r); l1.setText(res); } }; EventHandler eventmul = new EventHandler() { public void handle(ActionEvent e) { String a = l1.getText(); ac = Integer.parseInt(a); String b = l1.getText(); ad = Integer.parseInt(b); int r=ad*ac; String res=Integer.toString(r); l1.setText(res); } }; EventHandler eventdiv = new EventHandler() { public void handle(ActionEvent e) { String a = l1.getText(); ac = Integer.parseInt(a); String b = l1.getText(); ad = Integer.parseInt(b); double r=ad/ac; String res=Double.toString(r); l1.setText(res); } }; zero.setOnAction(event); one.setOnAction(event1); two.setOnAction(event2); three.setOnAction(event3); four.setOnAction(event4); five.setOnAction(event5); six.setOnAction(event6); seven.setOnAction(event7); eight.setOnAction(event8); nine.setOnAction(event9); add.setOnAction(eventadd); sub.setOnAction(eventsub); mul.setOnAction(eventmul); div.setOnAction(eventdiv); Scene sc = new Scene(x, 400,500); s.setScene(sc); s.show();

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions