我是 GUI 新手。我正在尝试为我已经制作的 java 程序创建一个 gui。我希望用户输入文件的端口号和位置,然后我想使用我已经制作的程序来完成剩下的工作。我对如何从用户输入中获取值并实施到程序感到困惑。
这是我的程序的框架
public class TcpServerCompareCSV extends Frame implements ActionListener , WindowListener {
private Label lblPort; // declare component Label
private TextField tfPort; // declare component TextField
private int port; // port number
/** WindowEvent handlers */
// Called back upon clicking close-window button
@Override
public void windowClosing(WindowEvent e) {
System.exit(0); // terminate the program
}
//constructor for frame
public TcpServerCompareCSV () {
setLayout(new FlowLayout());
// "this" Frame sets its layout to FlowLayout, which arranges the components
// from left-to-right, and flow to next row from top-to-bottom.
lblPort = new Label("Port"); // construct Label
add(lblPort); // "this" Frame adds Label
tfPort = new TextField("0", 10); // construct TextField
tfPort.setEditable(true); //edit text
add(tfPort); // "this" Frame adds tfCount
tfPort.addActionListener(this); // for event-handling
setTitle("compare"); // "this" Frame sets title
setSize(250, 100); // "this" Frame sets initial window size
setVisible(true); // "this" Frame shows
addWindowListener(this);
// "this" Frame fires WindowEvent its registered WindowEvent listener
// "this" Frame adds "this" object as a WindowEvent listener
}
/** ActionEvent handler - Called back when user clicks the button. */
@Override
public void actionPerformed(ActionEvent evt) {
// Get the String entered into the TextField tfPort, convert to int
port = Integer.parseInt(tfPort.getText());
}
/** The entry main() method */
public static void main(String[] args) throws IOException{
// Invoke the constructor to setup the GUI, by allocating an instance
TcpServerCompareCSV app = new TcpServerCompareCSV();