在Java中添加简单按钮,但是Java不允许我这样做 [英] Adding a Simple Button in java, but java is not allowing me to

查看:66
本文介绍了在Java中添加简单按钮,但是Java不允许我这样做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,从我的角度来看,我的代码相当不错,可以通过及格,但我很难添加一个简单的刷新/混洗按钮.不使用JOptionPane的帮助. Eclipse似乎没有意识到我已经创建了对我根本没有意义的按钮,因为它告诉我有关Node的一些信息,而Button实际上是一个节点,并且它已经创建.但是,当我进入另一个班级并添加带有3行示例的另一个按钮时,它确实起作用.但是,当我将其移至我的家庭作业程序时,它只是使我对add方法产生了错误,从而破坏了整个程序! 说

Ok so from my stand point my code is pretty decent enough to get a passing grade but I am having trouble adding a simple refresh/shuffle button. NOT USING the aids of JOptionPane. Eclipse doesnt seem to recognize that I have created the button which doesnt make sense at all for me because its telling me something about a Node which the Button is in fact a node and it is created. But when I go into another class and add another button with the 3 line example it simply works. But when I move it to my homework program it simply gives me an error on the add method which breaks the whole program! Says

列表类型中的add(Node)方法不适用于争论(按钮)"

"The method add(Node) in the type List is not applicable for the arguements (Button)"

有人可以阐明我的代码在哪里出错吗?它必须是沿着节点进行字符串转换的东西,或者我似乎无法弄清楚的东西.愿意接受给我的任何提示,但请不要为我解决问题.

Could anyone shed some light of where I could be going wrong in my code? It has to be something along the a node to string conversion or something I just cant seem to figure it out. Willing to take any hints given to me but please DO NOT SOLVE THE PROBLEM FOR ME.

这基本上是本书中的问题. 写一个程序,让用户单击刷新按钮以显示54张牌中的四张."

Here is the question from the book basically. "Write a program that lets the user click the refresh button to display four cards from a deck of 54 cards."

我只需要按钮的全部帮助即可.我真的剩下了.

I just need some help on the button thats all. I literally have the rest.

到目前为止,这是我的代码. 我已经取消了进口,因为进口太多了.

Here is my code so far. I Have left the imports out as there is just too many.

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)的原因.

You're using the AWT-button with your import java.awt.Button;, that's why you can use the method public void addActionListener(ActionListener l).

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

Replace your import to import javafx.scene.control.Button;. Furthermore you could use (analogue to your code) the following lambda:

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

试试看:)

这篇关于在Java中添加简单按钮,但是Java不允许我这样做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆