【问题标题】:Adding a Simple Button in java, but java is not allowing me to在java中添加一个简单的按钮,但java不允许我这样做
【发布时间】:2017-03-21 15:07:54
【问题描述】:

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

“List类型中的add(Node)方法不适用于论点(Button)”

谁能说明我的代码哪里出错了?它必须是沿着a节点到字符串转换的东西,或者我似乎无法弄清楚的东西。愿意接受给我的任何提示,但请不要为我解决问题。

这基本上是书中的问题。 “编写一个程序,让用户单击刷新按钮以显示一副 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);

    }

}

【问题讨论】:

  • 不确定,但在你的代码前面,你会这样做Pane pane = new HBox(20); pane.setPadding(new Insets(5, 5, 5, 5));你需要遵循这个模式吗?
  • @ScaryWombat 我的意思是我确实需要那里的窗格,因为一旦我弄清楚如何将按钮正确地放入程序中,它就会在刷新按钮上方适当地设置卡片。我会将 cmets 添加到程序中。
  • 能否请您向我们展示您的 import 语句?我不是 Swing 开发人员,但我猜您使用的是 Java Swing (docs.oracle.com/javase/7/docs/api/javax/swing/…) 而不是 JavaFX 按钮...
  • @SSchuette 添加了我的导入语句。我根本不使用 Java Swing,因为我也不喜欢这种方法。我想首先使用 javafx 按钮。

标签: java button javafx imageview shuffle


【解决方案1】:

您将 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);
});

试一试:)

【讨论】:

  • @Sschutte 先生,我爱你:P 非常感谢!我知道这是一件愚蠢的事情。现在只需让按钮实际工作:P
  • 不用担心,这就是 StackOverflow 的理念!如果回答对你有帮助,请采纳。
  • 完成了我的朋友。
猜你喜欢
  • 2011-08-17
  • 2012-09-25
  • 1970-01-01
  • 2014-03-20
  • 2013-09-07
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
相关资源
最近更新 更多