我正在尝试打开一个包含2个文本框的窗口以及一个带运算符的下拉菜单。然后,在每个文本框中输入一个数字,然后选择一个运算符,程序将执行所需的数学运算。必须有2个类别,并通过贴出一个标签说如果涉及除法,则不能除以0(此消息必须为红色且具有一定的字体大小),然后将其除以第二个数字,以回应0。数学并将其输出为标签(这也必须是特定的颜色和大小)。在上周,我至少已取消和重做此代码3次。我真的迷路了。
// Imports
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.application.Application;
import javafx.scene.control.TextField;
public class Test2Driver extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception
{
String result = null;
double num1 = 0;
double num2 = 0;
TextField num1TextField = new TextField();
try
{
num1 = Double.parseDouble(num1TextField.getText());
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
HBox num1TextBox = new HBox(150, num1TextField);
num1TextBox.setAlignment(Pos.CENTER);
TextField num2TextField = new TextField();
try
{
num2 = Double.parseDouble(num2TextField.getText());
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
HBox num2TextBox = new HBox(150, num2TextField);
num2TextBox.setAlignment(Pos.CENTER);
Label answer = new Label(result);
HBox answerBox = new HBox(150, answer);
answerBox.setAlignment(Pos.CENTER);
answer.setTextFill(Color.BLACK);
answerBox.setStyle("-fx-font-size: 36px;");
String[] operators = {"", "+", "-", "*", "/", "%"};
JComboBox<String> control = new JComboBox<>(operators);
control.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
JComboBox control = (JComboBox)event.getSource();
Object selected = control.getSelectedItem();
if(selected.toString().equals(""))
{
Calculator.blank();
}
else if(selected.toString().equals("+"))
{
Calculator.add();
}
else if(selected.toString().equals("-"))
{
Calculator.subtract();
}
else if(selected.toString().equals("*"))
{
Calculator.multiply();
}
else if(selected.toString().equals("/"))
{
Calculator.divide();
}
else if(selected.toString().equals("%"))
{
Calculator.modulus();
}
}
});
VBox display = new VBox(num1TextBox, num2TextBox, answer);
display.setAlignment(Pos.CENTER);
display.setPadding(new Insets(10));
Scene scene = new Scene(display);
primaryStage.setWidth(400);
primaryStage.setHeight(300);
primaryStage.setScene(scene);
primaryStage.setTitle("Calculator");
primaryStage.show();
}
public class Calculator extends Test2Driver
{
public String blank(String result)
{
result = "Error, pick an operator.";
return result;
}
public String add(double num1, double num2, String result)
{
double sum;
sum = num1 + num2;
result = String.valueOf(sum);
return result;
}
public String subtract(double num1, double num2, String result)
{
double difference;
difference = num1 - num2;
result = String.valueOf(difference);
return result;
}
public String multiply(double num1, double num2, String result)
{
double product;
product = num1 * num2;
result = String.valueOf(product);
return result;
}
public String divide(double num1, double num2, String result)
{
if(num2 == 0)
{
result = "Can not divide by 0!";
return result;
}
else
{
double quotient;
quotient = num1 / num2;
result = String.valueOf(quotient);
return result;
}
}
public String modulus(double num1, double num2, String result)
{
if(num2 == 0)
{
result = "Can not divide by 0!";
return result;
}
else
{
double modulus;
modulus = num1 % num2;
result = String.valueOf(modulus);
return result;
}
}
}
}
答案 0 :(得分:1)
我检查了您的代码,发现很多错误: 为了使用以下行:
answer.setTextFill(Color.BLACK);
您必须为javafx而不是awt导入正确的颜色:
//import java.awt.Color; use javafx.scene.paint.Color instead
//Imports
//import java.awt.Color; use javafx.scene.paint.Color instead
import javafx.scene.paint.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.application.Application;
import javafx.scene.control.TextField;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
String result = null;
double num1 = 0;
double num2 = 0;
TextField num1TextField = new TextField();
try
{
num1 = Double.parseDouble(num1TextField.getText());
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
HBox num1TextBox = new HBox(150, num1TextField);
num1TextBox.setAlignment(Pos.CENTER);
TextField num2TextField = new TextField();
try
{
num2 = Double.parseDouble(num2TextField.getText());
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
HBox num2TextBox = new HBox(150, num2TextField);
num2TextBox.setAlignment(Pos.CENTER);
Label answer = new Label(result);
HBox answerBox = new HBox(150, answer);
answerBox.setAlignment(Pos.CENTER);
answer.setTextFill(Color.BLACK);
answerBox.setStyle("-fx-font-size: 36px;");
String[] operators = {"", "+", "-", "*", "/", "%"};
JComboBox<String> control = new JComboBox<>(operators);
final double finalNum1 = num1;
final double finalNum2 = num2;
control.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
//Create instance of Calculator class
Calculator calc = new Calculator();
JComboBox control = (JComboBox)event.getSource();
Object selected = control.getSelectedItem();
if(selected.toString().equals(""))
{
calc.blank();
}
else if(selected.toString().equals("+"))
{
calc.add(finalNum1, finalNum2, "");
}
else if(selected.toString().equals("-"))
{
calc.subtract(finalNum1, finalNum2, "");
}
else if(selected.toString().equals("*"))
{
calc.multiply(finalNum1, finalNum2, "");
}
else if(selected.toString().equals("/"))
{
calc.divide(finalNum1, finalNum2, "");
}
else if(selected.toString().equals("%"))
{
calc.modulus(finalNum1, finalNum2, "");
}
}
});
VBox display = new VBox(num1TextBox, num2TextBox, answer);
display.setAlignment(Pos.CENTER);
display.setPadding(new Insets(10));
Scene scene = new Scene(display);
primaryStage.setWidth(400);
primaryStage.setHeight(300);
primaryStage.setScene(scene);
primaryStage.setTitle("Calculator");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public class Calculator extends Main
{
public String blank()
{
String result;
result = "Error, pick an operator.";
return result;
}
public String add(double num1, double num2, String result)
{
double sum;
sum = num1 + num2;
result = String.valueOf(sum);
return result;
}
public String subtract(double num1, double num2, String result)
{
double difference;
difference = num1 - num2;
result = String.valueOf(difference);
return result;
}
public String multiply(double num1, double num2, String result)
{
double product;
product = num1 * num2;
result = String.valueOf(product);
return result;
}
public String divide(double num1, double num2, String result)
{
if(num2 == 0)
{
result = "Can not divide by 0!";
return result;
}
else
{
double quotient;
quotient = num1 / num2;
result = String.valueOf(quotient);
return result;
}
}
public String modulus(double num1, double num2, String result)
{
if(num2 == 0)
{
result = "Can not divide by 0!";
return result;
}
else
{
double modulus;
modulus = num1 % num2;
result = String.valueOf(modulus);
return result;
}
}
}
}