我编写的程序应该允许用户选择形状,颜色和笔触颜色并在绘图区域创建它。一切正常,但唯一的问题是,当我制作一个圆圈时,按重置,创建一个矩形,然后如果我重置并尝试再次制作圆圈,它不会让我在文本字段中输入任何内容。如果我制作一个圆圈,重置,制作一个矩形,重置,制作一个椭圆,重置,我就不能使用圆形和矩形的文本字段。
所以看起来我可以继续制作一个形状,直到我制作另一个形状,然后之前的形状是无法使用的。
我怀疑它是我的重置按钮,但是我清除了其中的所有内容,所以我不知道我还能做些什么。任何帮助表示赞赏。
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
//@author Luka
public class more4 extends Application {
@Override
public void start(Stage primaryStage) {
//GRIDPANE
GridPane root = new GridPane();//this will contain all of the flowpanes
root.setVgap(2.0);//set a vgap to space out the flowpanes inside
//FLOWPANES TO ENCAPSULATE EACH GROUP OF LABELS AND THEIR RBUTTONS IN.
FlowPane fp = new FlowPane();
FlowPane fp2 = new FlowPane();
FlowPane fp2h = new FlowPane();
FlowPane fp3 = new FlowPane();
FlowPane fp4 = new FlowPane();
FlowPane fp5 = new FlowPane();
FlowPane cir = new FlowPane();//this flowpane contains the text fields for circle
FlowPane rec = new FlowPane();//this flowpane contains the text fields for rectangle
FlowPane ell = new FlowPane();//this flowpane contains the text fields for ellipse
FlowPane pol = new FlowPane();//this flowpane contains the text fields for polygon
//LABELS
Label shape = new Label("Shape: ");
Label fill = new Label("Fill Color: ");
Label line = new Label("Line Color: ");
Label dimensions = new Label("CLICK RESET TO BEGIN!");
Label drawLabel = new Label("Drawing Area: ");
//RADIO BUTTONS
//GROUP ONE:
final ToggleGroup group1 = new ToggleGroup();
RadioButton circle = new RadioButton("Circle");
circle.setToggleGroup(group1);//add to group1
circle.setSelected(true);//in this group the circle is selected by default
RadioButton rectangle = new RadioButton("Rectangle");
rectangle.setToggleGroup(group1);//add to group1
RadioButton elipse = new RadioButton("Ellipse");
elipse.setToggleGroup(group1);//add to group1
RadioButton polygon = new RadioButton("Polygon");
polygon.setToggleGroup(group1);//add to group1
//GROUP TWO:
final ToggleGroup group2 = new ToggleGroup();
RadioButton red = new RadioButton("Red");
red.setToggleGroup(group2);//add to group2
red.setSelected(true);//in this group, red is the default selected option
RadioButton green = new RadioButton("Green");
green.setToggleGroup(group2);//add to group2
RadioButton blue = new RadioButton("Blue");
blue.setToggleGroup(group2);//add to group2
//GROUP THREE
final ToggleGroup group3 = new ToggleGroup();
RadioButton red2 = new RadioButton("Red");
red2.setToggleGroup(group3);//add to group3
red2.setSelected(true);//in this group the red option is the default option
RadioButton green2 = new RadioButton("Green");
green2.setToggleGroup(group3);//add to group3
RadioButton blue2 = new RadioButton("Blue");
blue2.setToggleGroup(group3);//add to group3
//TEXTFIELD
TextField tf = new TextField();//default textfield
//TEXTFIELDS FOR CIRCLE
TextField centerX = new TextField();
centerX.setPrefWidth(30.0);
TextField centerY = new TextField();
centerY.setPrefWidth(30.0);
TextField radius = new TextField();
radius.setPrefWidth(30.0);
//TEXTFIELDS FOR RECTANGLE
TextField setX = new TextField();
setX.setPrefWidth(30.0);
TextField setY = new TextField();
setY.setPrefWidth(30.0);
TextField setWidth = new TextField();
setWidth.setPrefWidth(30.0);
TextField setHeight = new TextField();
setHeight.setPrefWidth(30.0);
//TEXTFIELDS FOR ELLIPSE
TextField setCenterX = new TextField();
setCenterX.setPrefWidth(30.0);
TextField setCenterY = new TextField();
setCenterY.setPrefWidth(30.0);
TextField setRadiusX = new TextField();
setRadiusX.setPrefWidth(30.0);
TextField setRadiusY = new TextField();
setRadiusY.setPrefWidth(30.0);
//TEXTFIELD FOR POLYGON
TextField p1 = new TextField();
p1.setPrefWidth(30.0);
TextField p2 = new TextField();
p2.setPrefWidth(30.0);
TextField p3 = new TextField();
p3.setPrefWidth(30.0);
TextField p4 = new TextField();
p4.setPrefWidth(30.0);
TextField p5 = new TextField();
p5.setPrefWidth(30.0);
TextField p6 = new TextField();
p6.setPrefWidth(30.0);
//PANE - DRAWING AREA
Pane canvas = new Pane();
canvas.setStyle("-fx-background-color: white;");
canvas.setStyle("-fx-border-color: black;\n"
+ "-fx-border-insets: 5;\n"
+ "-fx-border-width: 3;\n"
+ "-fx-border-style: dotted;\n");
canvas.setPrefSize(450, 460);
////////////////////////
cir.getChildren().addAll(centerX, centerY, radius);
GridPane.setConstraints(cir, 2, 18);
rec.getChildren().addAll(setX, setY, setWidth, setHeight);
GridPane.setConstraints(rec, 2, 18);
ell.getChildren().addAll(setCenterX, setCenterY, setRadiusX, setRadiusY);
GridPane.setConstraints(rec, 2, 18);
pol.getChildren().addAll(p1, p2, p3, p4, p5, p6);
GridPane.setConstraints(pol, 2, 18);
//BUTTONS
Button reset = new Button();
reset.setText("Reset");
reset.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
circle.setSelected(true);
red.setSelected(true);
red2.setSelected(true);
canvas.getChildren().clear();
cir.getChildren().clear();
centerX.clear();
centerY.clear();
radius.clear();
rec.getChildren().clear();
ell.getChildren().clear();
pol.getChildren().clear();
dimensions.setText("Enter Center X, Center Y, and Radius: ");
centerX.setPromptText("X");
centerY.setPromptText("Y");
radius.setPromptText("R");
cir.getChildren().addAll(dimensions, centerX, centerY, radius);
GridPane.setConstraints(cir, 1, 18);
root.getChildren().addAll(cir);
}
});
//EXIT BUTTON
Button exit = new Button();
exit.setText("Exit");
exit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(0);
}
});
Button draw = new Button();
draw.setText("Draw");
draw.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (circle.isSelected()) {
double cx = Double.parseDouble(centerX.getText());
double cy = Double.parseDouble(centerY.getText());
double rc = Double.parseDouble(radius.getText());
Circle c = new Circle();
c.setCenterX(cx);
c.setCenterY(cy);
c.setRadius(rc);
canvas.getChildren().add(c);
if (red.isSelected()) {
c.setFill(Color.RED);
if (red2.isSelected()) {
c.setStroke(Color.RED);
} else if (green2.isSelected()) {
c.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
c.setStroke(Color.BLUE);
}
} else if (green.isSelected()) {
c.setFill(Color.GREEN);
if (red2.isSelected()) {
c.setStroke(Color.RED);
} else if (green2.isSelected()) {
c.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
c.setStroke(Color.BLUE);
}
} else if (blue.isSelected()) {
c.setFill(Color.BLUE);
if (red2.isSelected()) {
c.setStroke(Color.RED);
} else if (green2.isSelected()) {
c.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
c.setStroke(Color.BLUE);
}
}
} else if (rectangle.isSelected()) {
double rx = Double.parseDouble(setX.getText());
double ry = Double.parseDouble(setY.getText());
double rw = Double.parseDouble(setWidth.getText());
double rh = Double.parseDouble(setHeight.getText());
Rectangle r = new Rectangle();
r.setX(rx);
r.setY(ry);
r.setWidth(rw);
r.setHeight(rh);
canvas.getChildren().add(r);
if (red.isSelected()) {
r.setFill(Color.RED);
if (red2.isSelected()) {
r.setStroke(Color.RED);
} else if (green2.isSelected()) {
r.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
r.setStroke(Color.BLUE);
}
} else if (green.isSelected()) {
r.setFill(Color.GREEN);
if (red2.isSelected()) {
r.setStroke(Color.RED);
} else if (green2.isSelected()) {
r.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
r.setStroke(Color.BLUE);
}
} else if (blue.isSelected()) {
r.setFill(Color.BLUE);
if (red2.isSelected()) {
r.setStroke(Color.RED);
} else if (green2.isSelected()) {
r.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
r.setStroke(Color.BLUE);
}
}
} else if (elipse.isSelected()) {
double ex = Double.parseDouble(setCenterX.getText());
double ey = Double.parseDouble(setCenterY.getText());
double erx = Double.parseDouble(setRadiusX.getText());
double ery = Double.parseDouble(setRadiusY.getText());
Ellipse e = new Ellipse();
e.setCenterX(ex);
e.setCenterY(ey);
e.setRadiusX(erx);
e.setRadiusY(ery);
canvas.getChildren().add(e);
if (red.isSelected()) {
e.setFill(Color.RED);
if (red2.isSelected()) {
e.setStroke(Color.RED);
} else if (green2.isSelected()) {
e.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
e.setStroke(Color.BLUE);
}
} else if (green.isSelected()) {
e.setFill(Color.GREEN);
if (red2.isSelected()) {
e.setStroke(Color.RED);
} else if (green2.isSelected()) {
e.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
e.setStroke(Color.BLUE);
}
} else if (blue.isSelected()) {
e.setFill(Color.BLUE);
if (red2.isSelected()) {
e.setStroke(Color.RED);
} else if (green2.isSelected()) {
e.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
e.setStroke(Color.BLUE);
}
}
} else if (polygon.isSelected()) {
double po = Double.parseDouble(p1.getText());
double pt = Double.parseDouble(p2.getText());
double ph = Double.parseDouble(p3.getText());
double pf = Double.parseDouble(p4.getText());
double pi = Double.parseDouble(p5.getText());
double ps = Double.parseDouble(p6.getText());
Polygon p = new Polygon();
p.getPoints().addAll(new Double[]{
po, pt,
ph, pf,
pi, ps
});
canvas.getChildren().add(p);
if (red.isSelected()) {
p.setFill(Color.RED);
if (red2.isSelected()) {
p.setStroke(Color.RED);
} else if (green2.isSelected()) {
p.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
p.setStroke(Color.BLUE);
}
} else if (green.isSelected()) {
p.setFill(Color.GREEN);
if (red2.isSelected()) {
p.setStroke(Color.RED);
} else if (green2.isSelected()) {
p.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
p.setStroke(Color.BLUE);
}
} else if (blue.isSelected()) {
p.setFill(Color.BLUE);
if (red2.isSelected()) {
p.setStroke(Color.RED);
} else if (green2.isSelected()) {
p.setStroke(Color.GREEN);
} else if (blue2.isSelected()) {
p.setStroke(Color.BLUE);
}
}
}
}
});
circle.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (circle.isSelected()) {
final Label dimensions = new Label("Enter radius: ");
GridPane.setConstraints(dimensions, 1, 4);
}
}
});
circle.setOnAction(e -> {
if (circle.isSelected()) {
rec.getChildren().clear();
ell.getChildren().clear();
pol.getChildren().clear();
dimensions.setText("Enter Center X, Center Y, and Radius: ");
centerX.setPromptText("X");
centerY.setPromptText("Y");
radius.setPromptText("R");
cir.getChildren().addAll(dimensions, centerX, centerY, radius);
GridPane.setConstraints(cir, 1, 18);
root.getChildren().addAll(cir);
}
});
rectangle.setOnAction(e -> {
if (rectangle.isSelected()) {
cir.getChildren().clear();
ell.getChildren().clear();
pol.getChildren().clear();
dimensions.setText("Enter X and Y coordinates, length, width: ");
setX.setPromptText("X");
setY.setPromptText("Y");
setWidth.setPromptText("W");
setHeight.setPromptText("L");
rec.getChildren().addAll(dimensions, setX, setY, setWidth, setHeight);
GridPane.setConstraints(rec, 1, 18);
root.getChildren().addAll(rec);
}
});
elipse.setOnAction(e -> {
if (elipse.isSelected()) {
cir.getChildren().clear();
rec.getChildren().clear();
pol.getChildren().clear();
dimensions.setText("Enter Center X and Y, and Radius X and Y: ");
setCenterX.setPromptText("X");
setCenterY.setPromptText("Y");
setRadiusX.setPromptText("X");
setRadiusY.setPromptText("Y");
ell.getChildren().addAll(dimensions, setCenterX, setCenterY, setRadiusX, setRadiusY);
GridPane.setConstraints(ell, 1, 18);
root.getChildren().addAll(ell);
}
});
polygon.setOnAction(e -> {
if (polygon.isSelected()) {
cir.getChildren().clear();
rec.getChildren().clear();
ell.getChildren().clear();
dimensions.setText("Enter six points: ");
p1.setPromptText("P1");
p2.setPromptText("P2");
p3.setPromptText("P3");
p4.setPromptText("P4");
p5.setPromptText("P5");
p6.setPromptText("P6");
pol.getChildren().addAll(dimensions, p1, p2, p3, p4, p5, p6);
GridPane.setConstraints(pol, 1, 18);
root.getChildren().addAll(pol);
}
});
//GRIDPANE ADD CHILDREN
root.getChildren().addAll(fp, fp2, fp2h, fp3, fp4, fp5);
//ADDING CHILDREN TO THE FLOWPANES AND SETTING THE FLOWPANES' POSITION
fp.getChildren().addAll(shape, circle, rectangle, elipse, polygon);
GridPane.setConstraints(fp, 1, 1);
fp2.getChildren().addAll(fill, red, green, blue);
GridPane.setConstraints(fp2, 1, 6);
fp2h.getChildren().addAll(line, red2, green2, blue2);
GridPane.setConstraints(fp2h, 1, 12);
fp3.getChildren().addAll(dimensions);
GridPane.setConstraints(fp3, 1, 18);
fp4.getChildren().addAll(drawLabel, canvas);
GridPane.setConstraints(fp4, 1, 24);
fp5.getChildren().addAll(reset, exit, draw);
GridPane.setConstraints(fp5, 1, 30);
fp5.setHgap(138.0);
//SCENE
Scene scene = new Scene(root);
primaryStage.setTitle("Lab 4");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
运行代码时没有注意到任何异常吗?在动作处理程序中,您不断向场景图添加相同的节点,这是不允许的。例如,在重置处理程序中:
root.getChildren().addAll(cir);
第一次通话后#34; cir&#34;已经在场景图中,您无法再次添加它。