我一直在考慮這個問題,但我真的不知道如何在 GUI 中進行檔案處理,因為作為初學者,我到目前為止只使用控制臺,而 GUI 對我來說非常混亂。
誰能給我解釋一下?
uj5u.com熱心網友回復:
您只在代碼端進行錯誤處理,您可以使用“try catch”或“if”陳述句
uj5u.com熱心網友回復:
這是一個使用我從這里提取的 AWT 編程 GUI 的示例:
import java.awt.*; // Using AWT container and component classes
import java.awt.event.*; // Using AWT event classes and listener interfaces
// An AWT program inherits from the top-level container java.awt.Frame
public class AWTCounter extends Frame {
private Label lblCount; // Declare a Label component
private TextField tfCount; // Declare a TextField component
private Button btnCount; // Declare a Button component
private int count = 0;
public AWTCounter () {
setLayout(new FlowLayout());
lblCount = new Label("Counter");
add(lblCount);
tfCount = new TextField(count "", 10); // TextField component with initial text
tfCount.setEditable(false); // read-only
add(tfCount); // container (frame) adds TextField component
btnCount = new Button("Count"); // Button component
add(btnCount); // container (frame) adds Button component
BtnCountListener listener = new BtnCountListener();
btnCount.addActionListener(listener);
// "btnCount" is the source object that fires an ActionEvent when clicked.
setTitle("AWT Counter"); // container title
setSize(300, 100); // container initial window size
setVisible(true); // container shows
}
public static void main(String[] args) {
// Invoke the constructor to setup the GUI
AWTCounter app = new AWTCounter();
}
// class to handle the "Count" button-click
private class BtnCountListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent evt) {
count;
tfCount.setText(count "");
// or invoke file-handling here
}
}
}
當使用 GUI 時,您的檔案處理只發生在代碼中的另一個點:例如,當單擊按鈕或初始化 GUI 時,但后端作業是相同的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/439426.html
