|
|
Регистрация Восстановить пароль |
Повторная активизация e-mail |
Регистрация | Задать вопрос |
Заплачу за решение |
Новые сообщения |
Сообщения за день |
Расширенный поиск |
Правила |
Всё прочитано |
|
Опции темы | Поиск в этой теме |
19.04.2016, 14:26 | #1 |
Регистрация: 14.10.2014
Сообщений: 5
|
Java FX . Тема "Калькулятор". Не получается включить цифры
Вот мои программы:
package application; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Calculator extends Application { @Override public void start(Stage primaryStage) throws Exception { Parent root = FXMLLoader.load(getClass().getResou rce("calc.fxml")); primaryStage.setTitle("Калькулятор" ); primaryStage.setScene(new Scene(root)); primaryStage.show(); } public static void main(String[] args) { launch(args); } } |
19.04.2016, 14:26 | #2 |
Регистрация: 14.10.2014
Сообщений: 5
|
calc.java
package application; import javafx.fxml.Initializable; import javafx.fxml.FXML; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.scene.control.TextField; import javafx.scene.input.KeyEvent; public class calc implements Initializable { String text = ""; double number1 = 0; double number2 = 0; char operation; @FXML private TextField textField; @SuppressWarnings("incomplete-switch") @FXML private void keyPressed(KeyEvent ke){ switch (ke.getCode()){ case BACK_SPACE: text = ""; textField.setText(text); break; case ENTER: try{ switch(operation){ case '+': number2 = Float.parseFloat(text); text = "" +(number1+number2); break; case '-': number2 = Float.parseFloat(text); text = "" +(number1-number2); break; case '*': number2 = Float.parseFloat(text); text = "" +(number1*number2); break; case '/': number2 = Float.parseFloat(text); text = "" +(number1/number2); break; default: text = "Syntax ERROR"; break; } } catch(java.lang.NumberFormatExcepti on error){ text = "Syntax ERROR"; } textField.setText(text); break; } } @FXML private void keyTyped(KeyEvent ke){ switch (ke.getCharacter()){ case "0": text += "0"; textField.setText(text); break; case "1": text += "1"; textField.setText(text); break; case "2": text += "2"; textField.setText(text); break; case "3": text += "3"; textField.setText(text); break; case "4": text += "4"; textField.setText(text); break; case "5": text += "5"; textField.setText(text); break; case "6": text += "6"; textField.setText(text); break; case "7": text += "7"; textField.setText(text); break; case "8": text += "8"; textField.setText(text); break; case "9": text += "9"; textField.setText(text); break; case ".": text += "."; textField.setText(text); break; case"*": if(text.length()>0)number1=Float.pa rseFloat(text); operation = '*'; text = ""; textField.setText("*"); break; case"/": if(text.length()>0)number1=Float.pa rseFloat(text); operation = '/'; text = ""; textField.setText("/"); break; case"+": if(text.length()>0)number1=Float.pa rseFloat(text); operation = '+'; text = ""; textField.setText("+"); break; case"-": if(text.length()>0)number1=Float.pa rseFloat(text); operation = '-'; text = ""; textField.setText("-"); break; } } @FXML private void button0(ActionEvent event){ text +="0"; textField.setText(text); } @FXML private void button1(ActionEvent event){ text +="1"; textField.setText(text); } @FXML private void button2(ActionEvent event){ text +="2"; textField.setText(text); } @FXML private void button3(ActionEvent event){ text +="3"; textField.setText(text); } @FXML private void button4(ActionEvent event){ text +="4"; textField.setText(text); } @FXML private void button5(ActionEvent event){ text +="5"; textField.setText(text); } @FXML private void button6(ActionEvent event){ text +="6"; textField.setText(text); } @FXML private void button7(ActionEvent event){ text +="7"; textField.setText(text); } @FXML private void button8(ActionEvent event){ text +="8"; textField.setText(text); } @FXML private void button9(ActionEvent event){ text +="9"; textField.setText(text); } @FXML private void button10(ActionEvent event){ text =","; textField.setText(text); } @FXML private void button11(ActionEvent event){ text ="+/-"; textField.setText(text); } @FXML private void button12(ActionEvent event){ if(text.length()>0) number1=Float.parseFloat(text); operation ='+'; text =""; textField.setText("+"); } @FXML private void button13(ActionEvent event){ if(text.length()>0) number1=Float.parseFloat(text); operation ='-'; text =""; textField.setText("-"); } @FXML private void button14(ActionEvent event){ if(text.length()>0) number1=Float.parseFloat(text); operation ='*'; text =""; textField.setText("*"); } @FXML private void button15(ActionEvent event){ if(text.length()>0) number1=Float.parseFloat(text); operation ='/'; text =""; textField.setText("/"); } @FXML private void button16(ActionEvent event){ text =""; textField.setText(text); } @FXML private void button17(ActionEvent event){ try{ switch(operation){ case '+': number2 = Float.parseFloat(text); text = "" +(number1+number2); break; case '-': number2 = Float.parseFloat(text); text = "" +(number1-number2); break; case '*': number2 = Float.parseFloat(text); text = "" +(number1*number2); break; case '/': number2 = Float.parseFloat(text); text = "" +(number1/number2); break; default: text = "Syntax ERROR"; break; } } catch(java.lang.NumberFormatExcepti on error){ text = "Syntax ERROR"; } textField.setText(text); } @Override public void initialize(URL url,ResourceBundle rb){ //TODO } } |
19.04.2016, 14:27 | #3 |
Регистрация: 14.10.2014
Сообщений: 5
|
calc.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#keyPressed" onKeyTyped="#keyTyped" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <center> <GridPane hgap="10.0" minHeight="275.0" minWidth="300.0" prefHeight="278.0" prefWidth="449.0" vgap="10.0" BorderPane.alignment="CENTER"> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="40.0" prefWidth="40.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="40.0" prefWidth="40.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="40.0" prefWidth="40.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="40.0" prefWidth="40.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="40.0" prefWidth="40.0" /> </columnConstraints> <rowConstraints> <RowConstraints minHeight="35.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="35.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="35.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="35.0" vgrow="SOMETIMES" /> </rowConstraints> <children> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="7" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="8" GridPane.columnIndex="1" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="9" GridPane.columnIndex="2" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="/" GridPane.columnIndex="3" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="COS" GridPane.columnIndex="4" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="4" GridPane.rowIndex="1" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="5" GridPane.columnIndex="1" GridPane.rowIndex="1" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="6" GridPane.columnIndex="2" GridPane.rowIndex="1" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="*" GridPane.columnIndex="3" GridPane.rowIndex="1" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="C" GridPane.columnIndex="4" GridPane.rowIndex="1" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#button1" text="1" GridPane.rowIndex="2" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="2" GridPane.columnIndex="1" GridPane.rowIndex="2" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="3" GridPane.columnIndex="2" GridPane.rowIndex="2" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="-" GridPane.columnIndex="3" GridPane.rowIndex="2" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#button0" text="0" GridPane.rowIndex="3" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="," GridPane.columnIndex="1" GridPane.rowIndex="3" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="+/-" GridPane.columnIndex="2" GridPane.rowIndex="3" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="+" GridPane.columnIndex="3" GridPane.rowIndex="3" /> <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="=" GridPane.columnIndex="4" GridPane.rowIndex="3" /> </children> </GridPane> </center> <top> <TextField fx:id="textField" prefHeight="70.0" prefWidth="600.0" BorderPane.alignment="CENTER" /> </top> </BorderPane> |
Похожие темы | ||||
Тема | Автор | Раздел | Ответов | Последнее сообщение |
Не получается импортировать библиотеку "net" Java | RichiSP | Общие вопросы по Java, Java SE, Kotlin | 7 | 15.03.2016 13:48 |
Калькулятор(Java). Проблема вычислительных кнопок с "продолжением" действия. | KirkMETAL | Помощь студентам | 1 | 05.06.2010 17:55 |
Напечатать текст, удалив из него все цифры и знаки "+" или "–". Язык С++. | KaylasMKTY | Помощь студентам | 8 | 07.03.2010 21:35 |