2016-11-08 46 views
5

好吧,从我的立场来看,我的代码是相当不错的,足以获得通过成绩,但我无法添加一个简单的刷新/随机按钮。不使用JOptionPane的帮助。 Eclipse似乎并没有意识到我已经创建了对我来说没有任何意义的按钮,因为它告诉我一些关于Button实际上是节点并且它已创建的节点。但是,当我进入另一个班级,并添加3行示例中的另一个按钮时,它就可以工作。但是当我把它移到我的作业程序时,它只是给了我一个关于添加方法的错误,它会打破整个程序! 说在java中添加一个简单的按钮,但java不允许我

“在类型列表Add方法(节点)不适用于arguements(按钮)”

任何人都可以摆脱在那里我可以在我的代码会错误的一些轻?它必须是一个节点上的字符串转换,或者我不能想象出来的东西。愿意接受任何提示给我,但请不要为我解决问题。

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

    } 

} 
+0

不知道,但前面,你做'窗格窗格=新HBox中(20); pane.setPadding(new Insets(5,5,5,5));'你需要遵循这种模式吗? –

+0

@ScaryWombat我的意思是我需要那个窗格,因为它会在刷新按钮上方正确设置卡片,一旦我找出如何正确地将按钮放入程序中。我会为该计划添加评论。 – Vinnie

+0

你能告诉我们你的__import__陈述吗?我不是Swing开发人员,但我想你是使用Java Swing(https://docs.oracle.com/javase/7/docs/api/javax/swing/ButtonModel.html#addActionListener(java)。 awt.event.ActionListener))而不是JavaFX按钮... – SSchuette

回答

4

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

将您的进口替换为import javafx.scene.control.Button;。此外,您还可以使用(模拟到您的代码)以下的λ:在你的代码

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

试试看吧:)

+0

@Schutte你先生我爱:P谢谢这么多!我知道这是愚蠢的。现在只需让按钮实际工作:P – Vinnie

+0

不用担心这是StackOverflow的理想!如果答案对您有帮助,请接受。 – SSchuette

+1

完成我的朋友。 – Vinnie

相关问题