0
回答
怎么在handle方法中调到getBook(Book book)方法
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

@海诺者 你好,想跟你请教个问题:

package cn.demo03;


import java.util.List;
import java.util.Scanner;


import cn.demo.Book;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Separator;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;


//1:继承Application
public class NewBooks extends Application {

VBox vbox;
ScrollPane sp = new ScrollPane();


// 2:开发start
@Override
public void start(Stage stage) throws Exception {
// 3:声明主面板
// /*
BorderPane borderPane = new BorderPane();
borderPane.setPadding(new Insets(15));
Button btn = new Button("查询新书");
borderPane.setTop(btn);
          vbox.getChildren().add(btn);

btn.setOnAction(new EventHandler<ActionEvent>() {


@Override
public void handle(ActionEvent event) {
// System.err.println("Hello....");
NewBooks.getBook(Book book);
}
});
final VBox vbox = new VBox();
ScrollPane sp = new ScrollPane(vbox);
// */
/*
// 1:声明一个容器
vbox = new VBox(10);
sp.setContent(vbox);
vbox.setPadding(new Insets(15));
vbox.setAlignment(Pos.TOP_CENTER);
// 声明button
Button btn = new Button("查询新书");
vbox.getChildren().add(btn);
*/

// 添加一本书
new Thread() {
public void run() {
try {
// 查询所有书
List<Book> list = QueryBooks.query();
// 遍历
for (Book book : list) {
final HBox box = getBook(book);
// 添加vbox
Platform.runLater(new Runnable() {
@Override
public void run() {
vbox.getChildren().add(box);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
///*
// 8:将sp放到中间
borderPane.setCenter(sp);
Scene scene = new Scene(borderPane, 600, 500);
stage.setScene(scene);
stage.setResizable(false);
stage.show();
//*/
/*
// 2:声明放内容的容器
       
Scene sc = new Scene(vbox, 600, 500);

stage.setScene(sc);
stage.setResizable(false);
stage.show();
*/
}


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


// 声明每一个hbox
public static HBox getBook(Book book) {
HBox box = new HBox();
box.setPadding(new Insets(5));
box.setSpacing(10);
ImageView iv = new ImageView(book.img);
box.getChildren().add(iv);


VBox vbox = new VBox();
vbox.setSpacing(10);
vbox.getChildren().add(new Text(book.title));
//Label auth = new Label(book.auth);
//auth.setWrapText(true);
vbox.getChildren().add(new Text(book.auth));
Text text = new Text(book.brief);
text.setWrappingWidth(400);//最多宽度
vbox.getChildren().add(text);
box.getChildren().add(vbox);
return box;
}
}

举报
CarlDing
发帖于2年前 0回/108阅
顶部