在java中添加一个简单的按钮,但java不允许我这样做

2023-12-14

好吧,从我的角度来看,我的代码相当不错,足以获得及格分数,但我在添加简单的刷新/随机播放按钮时遇到了麻烦。不使用 JOptionPane 的帮助。 Eclipse 似乎没有意识到我创建了一个按钮,这对我来说根本没有意义,因为它告诉我一些关于节点的信息,该按钮实际上是一个节点并且它是被创建的。但是,当我进入另一个类并使用 3 行示例添加另一个按钮时,它就可以正常工作了。但是当我将它移动到我的作业程序时,它只是在 add 方法上给了我一个错误,这破坏了整个程序! 说

“列表类型中的方法 add(Node) 不适用于争论 (Button)”

任何人都可以阐明我的代码中可能出错的地方吗?它必须是节点到字符串转换过程中的某些东西,或者是我似乎无法弄清楚的东西。愿意接受给我的任何提示,但请不要为我解决问题。

这基本上是书中的问题。 “编写一个程序,让用户单击刷新按钮即可显示一副 54 张牌中的四张牌。”

我只需要一些有关按钮的帮助,仅此而已。剩下的我确实都有了。

到目前为止,这是我的代码。 我已经将进口产品排除在外,因为进口产品太多了。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.awt.Button;
import java.io.File;
import java.util.ArrayList;



public class Cards extends Application
{

    public void start(Stage primaryStage) 
    {
        ArrayList<String> cards = new ArrayList<>(); //Array list

        Shuffle(cards); //Shuffles the Cards

        String file1 = new File("cards" + "/" + cards.get(1) + ".png").toURI().toString();
        String file2 = new File("cards" + "/" + cards.get(2) + ".png").toURI().toString();
        String file3 = new File("cards" + "/" + cards.get(3) + ".png").toURI().toString();
        String file4 = new File("cards" + "/" + cards.get(4) + ".png").toURI().toString();

        Pane pane = new HBox(20); //Creates the Box for the Images
        pane.setPadding(new Insets(5, 5, 5, 5)); //Spreads the Images out


        Image image = new Image(file1); //Creates the String Image
        Image image2 = new Image(file2);
        Image image3 = new Image(file3);
        Image image4 = new Image(file4);

        pane.getChildren().add(new ImageView(image)); //Adds the First Image
        ImageView view1 = new ImageView(image);
        view1.setFitHeight(100);
        view1.setFitWidth(100);

        pane.getChildren().add(new ImageView(image2)); //Adds the Second Image
        ImageView view2 = new ImageView(image2);
        view2.setFitHeight(100);
        view2.setFitWidth(100);

        pane.getChildren().add(new ImageView(image3)); //Add the Third Image
        ImageView view3 = new ImageView(image3);
        view3.setFitHeight(100);
        view3.setFitWidth(100);

        pane.getChildren().add(new ImageView(image4)); //Add the Fourth Image
        ImageView view4 = new ImageView(image4);
        view4.setFitHeight(100);
        view4.setFitWidth(100);


        HBox hbox = new HBox(5); //Creates the Box for the Button

        Button shuffle = new Button("Shuffle"); //Creates the Button
        hbox.getChildren().add(shuffle); //Should add the button but doesn't
        shuffle.addActionListener( e -> //Listener for the button
        {
            Shuffle(cards);
        });


        BorderPane pane2 = new BorderPane();/ /Creates the Pane for the Button
        pane2.setCenter(pane); //Sets the cards in the Center
        pane2.setBottom(hbox); //Sets the Button on the bottom

        BorderPane.setAlignment(hbox, Pos.CENTER);
        hbox.setAlignment(Pos.BOTTOM_CENTER);//Aligns the Button to BOT_CENTER

        Scene scene = new Scene(pane2); //Creates the Scene
        primaryStage.setTitle("Cards");
        primaryStage.setScene(scene);
        primaryStage.show();


    }

    public void Shuffle(ArrayList<String> cards) 
    //Allows the cards to Shuffle when called. 
    {

        for (int i = 0; i <= 53; i++) //Sets the Number of Cards in Deck
            cards.add(String.valueOf(i+1));
        java.util.Collections.shuffle(cards);
    }

    public static void main(String[] args) 
    {
        launch(args);

    }

}

您正在使用 AWT 按钮import java.awt.Button;,这就是为什么你可以使用该方法public void addActionListener(ActionListener l).

将您的导入替换为import javafx.scene.control.Button;。此外,您可以使用(类似于您的代码)以下 lambda:

shuffle.setOnAction( (x) ->   //Listener for the button
{
    Shuffle(cards);
});

试一试 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在java中添加一个简单的按钮,但java不允许我这样做 的相关文章

随机推荐

  • 将 LibSVM 输出转换为浮点数向量

    我需要形成HOGDescriptor setSVMDetector input 我使用 openCV 计算描述符 然后使用 libSVM 获取模型文件 为了形成输入 我知道我需要获取支持向量的值并将它们与 alpha 进行元素相乘 然后在末
  • 如何在 C# 中将正则表达式设置为 MaskedTextBox 的掩码?

    我想从正则表达式设置 MaskedtextBox 的掩码 例如我想要针对 MaskedtextBox 的有效电子邮件 十进制值和其他正则表达式 作为参考 这描述了您可以使用面罩做什么 http msdn microsoft com en u
  • 使用 AppAssertionCredentials 来自 Google Admin SDK 的 403 错误

    我正在尝试通过 Google 管理目录 API 列出用户 import logging import os from google appengine api import memcache from googleapiclient imp
  • 如何禁用 Rails 应用程序中的迁移功能?

    背景 我们分别设计数据库模型和应用程序模型 RDMBS 架构师与 OOP 工程师 从我对 Rails 与域 键范式的了解来看 Rails 迁移无法轻松复制精心设计的企业 RDBMS 的所有功能 如果有的话 因此我们不进行迁移 而是使用其他工
  • LINQ to Entities 在 SQL 2005 Express 上生成包含 datetime2 的查询

    我有一个 LINQ to Entities 应用程序和一个数据库项目来管理面向 NET 4 0 的 VS 2010 解决方案中的架构 实体模型目前是从数据库进行逆向工程的 其中一个表是用日期时间类型的列定义的 数据库项目配置为使用 SQL
  • 为什么 R 中的回归会删除因子变量的索引 1? [复制]

    这个问题在这里已经有答案了 我正在尝试使用 R 进行回归lm和glm功能 我的因变量是基于给定时间段内事件与非事件比例的对数转换数据 所以我的因变量是连续的 而我的自变量是因子变量或虚拟变量 我有两个自变量 可以取以下值 i 年到 m 年
  • Android 后台服务在滑出时崩溃

    当我从任务管理器中删除我的应用程序时 剩下 0 个进程和 1 个服务 应用程序一次又一次崩溃 我应该做什么来解决它 这是我的服务等级 我还想问一件事OnStart 方法和OnStartCommand 方法之间的区别 package com
  • 在 JavaScript“窗口”对象中存储变量是使用该对象的正确方法吗?

    也许 我刚刚解决了我的问题 从对话框窗口成功提交表单后如何更新前端内容 通过在 JavaScript 中 存储 保存 变量window目的 然而 由于我是 JavaScript 方面的新手 我对是否在 JavaScript 中存储 保存变量
  • mysql 如果存在

    我已经盯着这个看了有一段时间了 也许一些新的眼睛能够指出我做错了什么 这是我的查询 IF EXISTS SELECT FROM admin rule module WHERE rule set id 13 AND entity id 2 b
  • 使用应用程序访问令牌时,Android 中的 OAUTH 访问令牌无效

    正如标题所示 当我尝试请求获取安装了该字段的好友列表时 me friends Fields installed access token 我的 logcat 中出现以下错误 Invalid OAuth access token 当查看 fa
  • SSIS包通过部署升级

    在我们的 QA 虚拟环境中 其中包含多个 SQL Server 我想部署一个通过 Visual Studio 2010 维护的 SSIS 2012 包 ispac 项目部署 目标 SSIS 服务器是 2012 但工作站上的客户端包括 SQL
  • php 邮件重复

    有什么原因会导致重复发送电子邮件吗 我有一个 PHP 页面 上面只有这段代码 每次运行它时 它都会向我发送两封电子邮件 感谢您的意见 事实证明这是一个浏览器插件问题 特别是 Firefox 4 0 1 上的 ySlow 2 1 0 我要求我
  • 在 FileZilla 中下载文件的命令行选项

    我正在使用 FileZilla GUI 从 FTP 服务器下载文件 谁能告诉我一个从 FTP 服务器下载文件到本地文件系统的命令行参数 FileZilla 没有任何允许自动传输的命令行参数 也没有任何其他方式 See FileZilla 客
  • 如何在 Google Maps API V3 中检索英文结果

    directionsDisplay new google maps DirectionsRenderer directionsDisplay setMap map directionsService new google maps Dire
  • 对 pandas 数据集执行 SQL 查询

    我有一个 pandas 数据集 称为 df 我怎样才能做类似下面的事情 df query select from df 谢谢 对于那些了解 R 的人 有一个名为 sqldf 的库 您可以在 R 中执行 SQL 代码 我的问题基本上是 pyt
  • 获取不带查询字符串的 url

    我有一个像这样的网址 http www example com mypage aspx myvalue1 hello myvalue2 goodbye 我想要得到http www example com mypage aspx从中 你能告诉
  • JQuery load() 和 .prepend()

    好的 我有以下代码 function ul postbit load load php fadeIn slow 3000 我想要实现的是加载 load php 之后将结果添加到 li 并让它淡入 此时它会覆盖其中的内容ul postbit
  • 未经身份验证的使用超出了 Google Api 日历的每日限制

    我正在测试示例代码 它一直有效 但突然我得到 error errors domain usageLimits reason dailyLimitExceededUnreg message Daily Limit for Unauthenti
  • Windows 不会从所有接口接收多播 IPv6 数据包

    我正在尝试使用此 python 2 7 代码在 Windows 上接收 IPv6 多播数据包 发送到 ff02 1 地址 import socket import win inet pton import struct socket IPP
  • 在java中添加一个简单的按钮,但java不允许我这样做

    好吧 从我的角度来看 我的代码相当不错 足以获得及格分数 但我在添加简单的刷新 随机播放按钮时遇到了麻烦 不使用 JOptionPane 的帮助 Eclipse 似乎没有意识到我创建了一个按钮 这对我来说根本没有意义 因为它告诉我一些关于节