自己用javafx練習(xí)組合框時寫了一個小程序,使用到了文件輸入,但是調(diào)用需要拋出異常的函數(shù)時,明明已經(jīng)拋出了,為什么還提示未報告異常?
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import java.util.*;
public class ComboBoxDemo extends Application
{
private String[] flagTitle = {"Canada","America","China"};
private ImageView[] flagImage = {new ImageView("card/canada.gif"),new ImageView("card/america.gif"),new ImageView("card/china")};
private String[] flagDescription = {"Canada.txt","America.txt","China.txt"};
private DescriptionPane descriptionPane = new DescriptionPane();
private ComboBox<String> cbo = new ComboBox<>();
@Override
public void start(Stage primaryStage) throws Exception
{
setDisplay(0);
BorderPane pane = new BorderPane();
BorderPane paneForComboBox = new BorderPane();
paneForComboBox.setLeft(new Label("Select a Country"));
paneForComboBox.setCenter(cbo);
pane.setTop(paneForComboBox);
cbo.setPrefWidth(400);
cbo.setValue("Canada");
ObservableList<String> item = FXCollections.observableArrayList(flagTitle);
cbo.getItems().addAll(item);
pane.setCenter(descriptionPane);
***cbo.setOnAction(e->setDisplay(item.indexOf(cbo.getValue())));***
Scene scene = new Scene(pane,400,500);
primaryStage.setTitle("ComboBoxDemo");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setDisplay(int index) throws Exception
{
descriptionPane.setTitle(flagDescription[index]);
descriptionPane.setImage(flagImage[index]);
descriptionPane.setDescription(flagDescription[index]);
}
}
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import java.util.Scanner;
import java.io.File;
public class DescriptionPane extends BorderPane
{
private Label lblImageTitle = new Label();
private TextArea taDescription = new TextArea();
public DescriptionPane()
{
lblImageTitle.setContentDisplay(ContentDisplay.TOP);
lblImageTitle.setPrefSize(200,200);
lblImageTitle.setFont(new Font("SansSerif",16));
taDescription.setFont(new Font("Serif",14));
taDescription.setEditable(false);
taDescription.setWrapText(true);
ScrollPane scrollPane = new ScrollPane(taDescription);
setLeft(lblImageTitle);
setCenter(scrollPane);
setPadding(new Insets(5,5,5,5));
}
public void setTitle(String title)
{
lblImageTitle.setText(title);
}
public void setImage(ImageView icon)
{
lblImageTitle.setGraphic(icon);
}
public void setDescription(String name) throws Exception
{
File file = new File(name);
String description = new Scanner(file).next();
taDescription.setText(description);
}
}
D:javaJCComboBoxDemo.java:38: 錯誤: 未報告的異常錯誤Exception; 必須對其進(jìn)行捕獲或聲明以便拋出
cbo.setOnAction(e->setDisplay(item.indexOf(cbo.getValue())));
作為菜鳥的我百思不得其解·自己弄了一下午了!
6 回答

慕運維8079593
TA貢獻(xiàn)1876條經(jīng)驗 獲得超5個贊
public void start(Stage primaryStage) throws Exception{}
你的這個方法你用了throws Exception拋出異常,當(dāng)你調(diào)用這個方法時,必須把這個方法放在try{}catch里捕獲,你直接調(diào)用這個方法不用try{}catch,當(dāng)這個方法發(fā)生錯誤時,就會報你出現(xiàn)的那個錯誤。
另外try{}catch是為了捕獲別人的異常,throws是把異常拋給別人
添加回答
舉報
0/150
提交
取消