- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是二级 OOP/Java 编程类(class)的学生。我们的任务是创建一个简单的 JavaFX 界面,允许用户使用箭头键在 Pane 中绘图。它让我想起了 eclipse 刻草图。这个程序运行良好,但我想在底部添加按钮来清除屏幕,以便用户可以重新开始而无需关闭程序和退出按钮,尽管它与 Pane 中右上角的 X 有点多余。在测试中,按下向下箭头的按键会进入按钮的 HBox Pane ,接合按钮并中断绘图功能。此后,按下向上箭头不会将光标返回到绘图 Pane 。此外,按向左或向右箭头(一旦启用按钮)可在按钮之间移动焦点。我正在寻找一种将关键事件限制在绘图 Pane 中的方法。我粘贴了下面的代码,没有导入库,但有 15 个库,包括 LineTo、MoveTo、Button、Path...我可以根据需要粘贴。
****编辑代码以反射(reflect) Slaw 建议的有效更改我现在知道 btClear 按钮的代码不会清除 Pane 并允许用户重新开始绘图。****
public class LineToDrawPath extends Application {
private double bX = 100.0, bY = 100.0;
private double segment = 20.0;
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Pane pane = new Pane();
HBox hBox = new HBox();
hBox.setPadding(new Insets(15,12,15,12));
hBox.setSpacing(20);
hBox.setAlignment(Pos.CENTER);
Button btClear = new Button("Clear");
btClear.setFocusTraversable(false); // effective solution
Button btExit = new Button ("Exit");
btExit.setFocusTraversable(false); // effective solution
hBox.getChildren().addAll(btClear, btExit);
// Creating a Path object
Path path = new Path(new MoveTo(bX, bY));
path.setStrokeWidth(1.7);
path.setStroke(Color.CHOCOLATE);
pane.getChildren().add(path);
path.setOnKeyPressed(e ->{
if (e.getCode().isArrowKey()){
switch (e.getCode()){
case DOWN: bY += segment; break;
case UP: bY -= segment; break;
case LEFT: bX -= segment; break;
case RIGHT: bX += segment; break;
}
path.getElements().add(new LineTo (bX, bY));
}
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(pane, Pos.CENTER);
btClear.setOnAction(e -> {
pane.getChildren().clear();
pane.getChildren().add(path);
});
btExit.setOnAction(e ->
System.exit(0)
);
// Create the scene object
Scene scene = new Scene(borderPane, 400, 400); // places the pane in the scene
primaryStage.setTitle("Draw a line with arrow keys");
primaryStage.setScene(scene); // places the scene in the stage
primaryStage.show(); // shows the window
path.requestFocus();
primaryStage.setResizable(false); // prevent user from resizing the window
}
public static void main(String[] args) {
launch(args);
}
}
最佳答案
这是我的 Etch-A-Sketch 的绘制 Controller
public class ButtonDrawController implements Initializable {
@FXML private Pane paneMain,paneButtonDraw;
@FXML private ColorPicker colorpicker;
@FXML private Canvas canvasPaper;
@FXML private ChoiceBox<String> penSize;
@FXML private ChoiceBox<String> pickCircle;
@FXML private Label msg;
@FXML private ToggleButton tbnHelp;
public Stage stage;
GraphicsContext graphicsContext;
KeyCode keycode;
double X,Y;
double startAngle,arcExtent;
final Printer selectedPrinter = Printer.getDefaultPrinter();
@FXML
private void showHelp(ActionEvent e){
if(tbnHelp.isSelected()){
tbnHelp.setText("Hide Directions");
tbnHelp.setStyle("-fx-background-color:darkgray;"
+"-fx-border-style: solid outside;"
+"-fx-border-width: 2;"
+"-fx-border-color: blue;");
String help = "\n\n"
+" These direction are for Draw with the Buttons\n\n"
+"To change Color or Pen Size see the Draw with the Mouse Directions.\n"
+"When Drawing with Buttons if Color or Pen Size is changed it is best\n"
+"to select a new draw position by clicking the Mouse on the paper.\n"
+"ERASE and PRINT is covered in the Draw with Mouse Directions also.\n\n"
+"Click the paper with the mouse to start drawing at that location.\n\n"
+"To use the Pick Circle Type click the button and select the Circle Type.\n"
+"To draw the Circle Type PRESS the Key Board 'A' key.\n"
+"To draw a paint scattered type image PRESS the Key Board 'S' key.\n\n"
+" Button Information for Drawing\n"
+"-------------------------------------------------------------------------------------\n"
+"To start drawing in the center of the paper PRESS the Key Board 'C' key.\n"
+"To draw at other positions on the paper Click the Mouse at that location.\n\n"
+"To draw a line UP - DOWN - LEFT - RIGHT PRESS the Arrow Keys.\n\n"
+"To draw a diagonal line PRESS the keys as described below.\n\n"
+"UP and RIGHT"+" "+"use the Numeric Key 9 or the Key Board 'H' key\n"
+"UP and LEFT"+" "+"use the Numeric Key 7 or the Key Board 'G' key\n"
+"DOWN and RIGHT use the Numeric Key 3 or the Key Board 'N' key\n"
+"DOWN and LEFT"+" "+"use the Numeric Key 1 or the Key Board 'V' key\n\n";
msg.setLayoutX(80);
msg.setLayoutY(80);
msg.setText(help);
msg.setVisible(true);
msg.toFront();
}else{
msg.setVisible(false);
tbnHelp.setText("Show Directions");
tbnHelp.setStyle("-fx-background-color:lightgray;"
+"-fx-border-style: solid outside;"
+"-fx-border-width: 2;"
+"-fx-border-color: blue;");
}
}
@FXML
private void onPrint(ActionEvent e){
printDialogSetup();
}
private void printDialogSetup(){
int lMargin = (int) 0.2;//Printer sets margins as inches
int rMargin = (int) 0.2;//Need to convert inches to pixels ?
int tMargin = (int) 0.2;
int bMargin = (int) 0.2;
PrinterJob job = PrinterJob.createPrinterJob();
//PageLayout pageLayout = selectedPrinter.createPageLayout(Paper.NA_LETTER,PageOrientation.PORTRAIT,Printer.MarginType.EQUAL);
PageLayout pageLayout = selectedPrinter.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, lMargin, rMargin, tMargin, bMargin);
JobSettings jobSettings = job.getJobSettings();
jobSettings.setPageLayout(pageLayout);
boolean printed = job.printPage(canvasPaper);
if (printed) {
// End the printer job
job.endJob();
}
}
@FXML
private void onPickCircle(MouseEvent e){
pickCircle.getSelectionModel().selectedIndexProperty().addListener((ChangeListener<? super Number>)(ov, old, newval) -> {
Number idx = (Number)newval;
switch(idx.intValue()){
case 0: startAngle = 0;
arcExtent = 0;
break;
case 1: startAngle = 0;// Circle
arcExtent = 360;
break;
case 2: startAngle = 0;// N
arcExtent = 180;
break;
case 3: startAngle = -90;// E
arcExtent = 180;
break;
case 4: startAngle = 0;// S
arcExtent = -180;
break;
case 5: startAngle = 90;//W
arcExtent = 180;
break;
default: startAngle = 0;
arcExtent = 360;
break;
}
});
canvasPaper.requestFocus();
}
@FXML
private void onPenSize(MouseEvent e){
penSize.getSelectionModel().selectedIndexProperty().addListener((ChangeListener<? super Number>)(ov, old, newval) -> {
Number idx = (Number)newval;
switch(idx.intValue()){
case 0: graphicsContext.setLineWidth(1);
break;
case 1: graphicsContext.setLineWidth(2);
break;
case 2: graphicsContext.setLineWidth(3);
break;
case 3: graphicsContext.setLineWidth(4);
break;
case 4: graphicsContext.setLineWidth(5);
break;
case 5: graphicsContext.setLineWidth(8);//REAL big
break;
default: graphicsContext.setLineWidth(1);
break;
}
graphicsContext.setLineCap(StrokeLineCap.ROUND);
});
canvasPaper.requestFocus();
}
@FXML
private void onPickColor(ActionEvent e){
// change cursor more exact placement on the screen
// graphicsContext.getCanvas().cursorProperty().set(Cursor.CROSSHAIR);
graphicsContext.setStroke(colorpicker.getValue());
canvasPaper.requestFocus();
}
@FXML
private void onPressed(MouseEvent e){
if(tbnHelp.isSelected()) {// Close the toggle button and HELP screen
tbnHelp.setSelected(false);
showHelp(null);
}
graphicsContext.getCanvas().cursorProperty().set(Cursor.CROSSHAIR);
graphicsContext.beginPath();
graphicsContext.moveTo(e.getX(), e.getY());
graphicsContext.setStroke(colorpicker.getValue());
X = e.getX();
Y = e.getY();
}
@FXML
private void onDraw(KeyEvent ev){
keycode = ev.getCode();
if(X == 0 && Y == 0 ){
Alert alert = new Alert(AlertType.INFORMATION);
alert.initStyle(StageStyle.UTILITY);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("CLICK ON THE PAPER TO START DRAWING");
alert.showAndWait();
return;
}
if(keycode == KeyCode.A){// Draws the Circle Type
if(arcExtent == 0){
Alert alert = new Alert(AlertType.INFORMATION);
alert.initStyle(StageStyle.UTILITY);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("To Draw a Circle Type\n\n"
+"First Use Pick Circle Type");
alert.showAndWait();
return;
}
double XX = 120;
graphicsContext.stroke();
graphicsContext.strokeArc(X, Y-60, XX, XX, startAngle, arcExtent, ArcType.OPEN);
}
if(keycode==KeyCode.S){
graphicsContext.beginPath();
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X+80, Y+80);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X+40, Y+60);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X+120, Y+20);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X+180, Y+120);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X-180, Y-120);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X-80, Y+80);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X-40, Y+60);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X-120, Y+20);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X-80, Y-80);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X-40, Y-60);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X-120, Y-20);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X+80, Y+80);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X+40, Y+60);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X+120, Y+20);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X+20, Y-80);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X+10, Y-60);
graphicsContext.moveTo(X,Y);
graphicsContext.lineTo(X+50, Y-120);
graphicsContext.setStroke(colorpicker.getValue());
graphicsContext.stroke();
}
if(keycode==KeyCode.C){// Center drawing position
X = 296;
Y = 381;
graphicsContext.beginPath();
graphicsContext.moveTo(X,Y);
graphicsContext.setStroke(colorpicker.getValue());
}
if(keycode==KeyCode.NUMPAD9){// 9
if(Y <= 21 || X >= 570){
return;
}
X = X + 15.5;
Y = Y - 20;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.H){// same as NUMPAD9
if(Y <= 21 || X >= 570){
return;
}
X = X + 15.5;
Y = Y - 20;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.NUMPAD7){// 7
if(Y <= 21 || X <= 19){
return;
}
X = X - 15.5;
Y = Y - 20;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.G){// same as NUMPAD7
if(Y <= 21 || X <= 19){
return;
}
X = X - 15.5;
Y = Y - 20;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.NUMPAD1){// 1
if(X <= 21 || Y >= 740){
return;
}
X = X - 15.5;
Y = Y + 20;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.V){// same as NUMPAD1
if(X <= 21 || Y >= 740){
return;
}
X = X - 15.5;
Y = Y + 20;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.NUMPAD3){
if(X >= 575 || Y >= 740){
return;
}
X = X + 15.5;
Y = Y + 20;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.N){// same as NUMPAD3
if(X >= 575 || Y >= 740){
return;
}
X = X + 15.5;
Y = Y + 20;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.UP){
if(Y <= 21){
return;
}
X = X;
Y = Y - 20;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.DOWN){
if(Y >= 740){
return;
}
X = X;
Y = Y + 20;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.LEFT){
if(X <= 21){
return;
}
X = X - 20;
Y = Y ;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
if(keycode==KeyCode.RIGHT){
if(X >= 570){
return;
}
X = X + 20;
Y = Y;
graphicsContext.lineTo(X,Y);
graphicsContext.stroke();
}
}
@FXML
private void onErase(ActionEvent e){
X = 0;
Y = 0;
graphicsContext.clearRect(0, 0,
graphicsContext.getCanvas().getWidth(),
graphicsContext.getCanvas().getHeight());
penSize.getSelectionModel().selectFirst();
pickCircle.getSelectionModel().selectFirst();
colorpicker.setValue(Color.RED);
}
@FXML
private void onBack(ActionEvent e) throws IOException{
stage = (Stage)paneButtonDraw.getScene().getWindow();
paneMain = FXMLLoader.load(getClass().getResource("etch.fxml"));
Scene scene = new Scene(paneMain);
stage.setScene(scene);
stage.setTitle("Etch A Sketch");
stage.show();
stage.sizeToScene();
stage.centerOnScreen();
}
private void Init(){
graphicsContext = canvasPaper.getGraphicsContext2D();
colorpicker.setValue(Color.RED);
graphicsContext.setStroke(colorpicker.getValue());
penSize.getItems().addAll("1","2","3","4","5","6");
penSize.getSelectionModel().selectFirst();
// Set values for Choice Box set here only needs to happen once
pickCircle.getItems().addAll("Click to Select","Circle","Half Circle N","Half circle E","Half Circle S","Half Circle W");
pickCircle.getSelectionModel().selectFirst();// Makes first value visable in Choice Box
tbnHelp.setStyle("-fx-background-color:lightgray;"
+"-fx-border-style: solid outside;"
+"-fx-border-width: 2;"
+"-fx-border-color: blue;");
}
@Override
public void initialize(URL location, ResourceBundle resources) {
Init();
}
}
关于JavaFX——按键事件被按钮中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58258650/
我不知道这是不是问这种问题的最佳地点, 我看到一些 JavaFX伙计们,重新标记 一些问题通过替换 javafx来自 javafx-2并采用新的 javafx-8 .它被弃用了还是什么? 编辑 : 不
错误本身: Error:java: invalid flag: --add-modules=javafx.fxml,javafx.graphics,javafx.controls,javafx.bas
这个想法是让一个应用程序在每个显示器上显示两个不同的窗口(阶段),该应用程序应该知道计算机有多少个显示器及其分辨率。 javafx有可能吗? 最佳答案 对于当前版本的 JavaFX (2.2),您可以
我正在将我的项目从 javafx 1.3 转换为 javafx 2.1。但我对 javafx.lang 有疑问包裹。 最佳答案 JavaFX 1.3 lang 包内容被拆分并移至下一个位置: 时长变为
当我尝试将标签添加到 gridpane 中时,如第二张图片所示,它不起作用。我已经尝试了很多东西,比如添加 CSS,但它仍然无法正常工作。为什么第 113 和 114 行不起作用? (opcje.se
我有一个JavaFX ContextMenu分配给滚动面板的鼠标右键单击。它会打开,但在滚动 Pane 外部单击时不会关闭。我可以在滚动 Pane 中添加另一个鼠标事件以将其隐藏,但这只能解决1个问题
我有一个tableview,其中附有一个可观察到的自定义类对象的列表(类类型:SalesInvoiceNetSale)。该表中的所有数据都可以正常显示。可观察列表中的最后一项是总计行(类类型:Sale
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
我想知道如何在JavaFX中绘制半圆。我尝试使用Shape和QuadCurve,但无法制作出完美的半圆。 这是我要绘制的图片: 最佳答案 您链接的图片实际上是一个半圆环。您可以通过绘制嵌套的2条圆弧和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在寻找 JavaFX 支持的图像类型(最新)列表,例如PNG、JPEG、TIFF。不同的搜索引擎没有帮助......知道从哪里开始吗? 更特别的是,我对 16 位灰度图像(不同格式)和罕见的受支持
我希望在 javafx 中让标签每 0.1 秒闪烁一次。文本显示在后台运行的 ImageView gif 的顶部。我将如何去做,或者您对最佳方法有什么建议? 谢谢 最佳答案 @fabian 的解决方案
我需要测试所选项目的值以调用不同的方法,因此我编写了添加侦听器的代码,但是该代码生成语法错误 @FXML private JFXComboBox cmbComp; cmbComp.valuePrope
我正在 Javafx 中编写一个非常简单的应用程序,其中舞台上有一个带有文本框的按钮作为一个场景。现在,我想要的行为是,当我单击按钮时,我可以使用另一个按钮加载另一个场景和舞台上的一个文本框,然后删除
编辑:如果用户单击“删除”以删除 ListView 中的项目,我会弹出一个警告框。它有效,但我希望它能超越原来的舞台。它出现在我的第一台显示器上。有什么方法可以设置警报显示时的位置吗? 请注意,“所有
我想使用 JavaFX 编写一个笔画绘图应用程序。我有一个压敏绘图板,如果能够读取笔的压力和倾斜值,那就太好了。 JavaFX 有一个 API 可以处理鼠标、触摸和滑动输入,但似乎没有任何东西可以产生
我在 JavaFX 中使用条形图和折线图。当我使两个图表大小相同并将它们放在同一位置时,它们完美地相互重叠。我如何使折线图显示在条形图的顶部。 目前我已将它们的不透明度设置为 0.7,这样它们“看起来
此问题与 this 相关。现在我想为字段值等于某个值的行着色。 @FXML private TableView tv_mm_view; @FXML private Ta
我有一个程序,可以生成高度图(0-255 的整数的 2D 数组),并使用 Shape3D“Box”对象为每个“像素”构建 3D View ,其高度与其在高度图中的值成比例。这会创建一个看起来很酷的四四
我想为 JavaFX 创建上下文菜单。这是我测试过的代码。但是由于某种原因,当我右键单击树节点时没有上下文菜单。你能帮我找出我的错误吗。 import java.util.Arrays; import
我是一名优秀的程序员,十分优秀!