import java.io.File;
import java.util.Scanner;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class ShakespeareUI extends Application {
public String Quotes(String fileName) throws Exception{
File file = new File (fileName);
String line ="";
Scanner sc = new Scanner(file);
while(sc.hasNextLine()){
line+= sc.nextLine();
}
return line;
}
@Override // Override the start method in the Application class
public void start(Stage primaryStage)throws Exception {
BorderPane pane = new BorderPane();
// Top of Pane with Text
Pane paneForText = new Pane();
paneForText.setPadding(new Insets(0,0,5,0));
Text shText = new Text(25, 50,"Shakespeare Quotes");
shText.setFont(Font.font("Arial", 28));
paneForText.getChildren().add(shText);
pane.setTop(paneForText);
// Center of Border Pane with TextArea
TextArea taQuote = new TextArea();
taQuote.setPrefColumnCount(30);
taQuote.setPrefRowCount(5);
pane.setCenter(taQuote);
// Bottom of Pane with Buttons
HBox paneForButtons = new HBox(20);
Button btLear = new Button("King Lear");
Button btMacBeth = new Button("MacBeth");
Button btHamlet = new Button("Hamlet");
Button btRichard = new Button("Richard III");
Button btOthello = new Button("Othello");
pane.setBottom(paneForButtons);
paneForButtons.getChildren().addAll(btLear, btMacBeth, btHamlet, btRichard, btOthello );
paneForButtons.setAlignment(Pos.CENTER);
paneForButtons.setStyle("-fx-border-color: green");
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 455, 150);
primaryStage.setTitle("Deep Patel"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
////// Your code here that handles events when buttons are clicked
btLear.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
shText.setText(btLear.getText());
taQuote.setText(Quotes("lear.txt"));
}
});
btMacBeth.setOnAction(new EventHandler<ActionEvent>(){
@Override public void handle(ActionEvent e){
shText.setText(btMacBeth.getText());
}
});
btHamlet.setOnAction(new EventHandler<ActionEvent>(){
@Override public void handle(ActionEvent e){
shText.setText(btHamlet.getText());
}
});
btRichard.setOnAction(new EventHandler<ActionEvent>(){
@Override public void handle(ActionEvent e){
shText.setText(btRichard.getText());
}
});
btOthello.setOnAction(new EventHandler<ActionEvent>(){
@Override public void handle(ActionEvent e){
shText.setText(btOthello.getText());
}
});
}
/////////////////////////////////////////////////////
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
嗨,我正在尝试运行此代码但是有关于异常的错误。我不知道该怎么做。在此先感谢您的帮助。我尝试将异常放在override方法中,在一般方法中然后我只是创建了新方法并在那里放了异常但是在这里仍然是相同的
我得到的错误是:
ShakespeareUI.java:79:错误:未报告异常;必须被抓住或 宣布被抛出
答案 0 :(得分:0)
EventHandler
方法不允许您为非运行时异常添加throws子句。因此,您需要使用try
- catch
来处理这些异常,即使您只是通过将异常重新抛出为RuntimeException
来处理它们(这不是处理代码失败执行的好方法)大多数情况):
btLear.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
shText.setText(btLear.getText());
try {
taQuote.setText(Quotes("lear.txt"));
} catch (Exception ex) {
// TODO: handle exception in a differnt way???
throw new RuntimeException(ex);
}
}
});
请注意,您应该在完成阅读器/写入器后立即关闭所有访问文件的类。 (在这种情况下为Scanner
):
public String Quotes(String fileName) throws Exception{
File file = new File (fileName);
StringBuilder builder = new StringBuilder(); // builder more efficient for concatenating multiple strings
try(Scanner sc = new Scanner(file)) { // try-with-resources automatically calls close on scanner
while(sc.hasNextLine()) {
builder.append(sc.nextLine());
}
return builder.toString();
}
}