gpt4 book ai didi

java - .getText 在用户能够输入之前抓取文本

转载 作者:行者123 更新时间:2023-12-01 21:59:28 25 4
gpt4 key购买 nike

我的程序有三个功能(不包括退出):读取文件、在文件内搜索和写入文件。我有这个功能齐全的控制台应用程序,并且正在为其开发 GUI (Swing)。我有一个主窗口,其中包含所有已创建和运行的组件(单击按钮 -> 通过控制台运行)。我现在所追求的是完全消除我对控制台的需求。

我(目前)仅对读取功能进行此更改,以便使其正常工作。我遇到的问题是用户无法在 JTextField 内指定所需的文件(类型路径)并在获取值(空)并将其分配给文件变量之前单击与所需功能相对应的按钮。

这里是包含所有内容的类,除了包含特定函数的类之外,我通过 main 链接到它:

public class SimpleDBGUI{
static File targetFile; //Declare File var to be used in methods below for holding user's desired file

public void mainWindow(){


//Create main window for Program
JFrame mainWindow = new JFrame("Simple Data Base"); //Init frame
mainWindow.setSize(500, 180); //Set frame size
mainWindow.setVisible(true); //Make frame visible

//Create panel for the main window of the GUI
JPanel simpleGUI = new JPanel( new GridBagLayout());
GridBagConstraints gbCons = new GridBagConstraints();
mainWindow.getContentPane().add(simpleGUI); //Adds JPanel container to the ContentPane of the JFrame

//Create button linking to read function
JButton readButton = new JButton("Read"); //Init button, and give text
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 2;
gbCons.weightx = .1;
readButton.setActionCommand("Read");
readButton.addActionListener( new ButtonClickListener());
simpleGUI.add(readButton, gbCons); //Adds the "Read" button to the JPanel

//Create button linking to the search function
JButton searchButton = new JButton("Search"); //Init button with text
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 1;
gbCons.gridy = 2;
gbCons.weightx = .1;
searchButton.setActionCommand("Search");
searchButton.addActionListener( new ButtonClickListener());
simpleGUI.add(searchButton, gbCons); //Adds the "Search" button to the JPanel

//Create button linking to the write function
JButton writeButton = new JButton("Write"); //Init button with text
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 2;
gbCons.gridy = 2;
gbCons.weightx = .1;
writeButton.setActionCommand("Write");
writeButton.addActionListener( new ButtonClickListener());
simpleGUI.add(writeButton, gbCons); //Adds the "Write" button to the JPanel


//Create label prompting user to specify desired function
JLabel promptText = new JLabel("Click 'Read' to read a file, 'Search' to search within a file, 'Write' to write to a file:"); //Give user a prompt to select desired function
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 0;
gbCons.gridwidth = 3;
simpleGUI.add(promptText, gbCons); //Add prompt to the JPanel

//Create button to exit program
JButton exitButton = new JButton("Exit");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 3;
gbCons.gridwidth = 3;
exitButton.setActionCommand("Exit");
exitButton.addActionListener( new ButtonClickListener());
simpleGUI.add(exitButton, gbCons); //Add "Exit" button to the JPanel

//Create TextField for user to input a desired file
JTextField sdbTarget = new JTextField();
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 1;
gbCons.gridwidth = 3;
simpleGUI.add(sdbTarget, gbCons); //Adds TextField to GUI
targetFile = new File(sdbTarget.getText()); //Writes input string to a File var, works but out of time resulting in null value
}

public class ButtonClickListener implements ActionListener{ //Sets the EventListener for every function

public void actionPerformed(ActionEvent event){

//targetFile = File(sdbTarget.getText()); //Was a proposed solution, but kept giving type resolving errors
try{ //Leftovers from the console version of the app,
Scanner inputChoice = new Scanner(System.in); //these were left so my Search and Write functions would still work in the console
File file = new File("C:/Users/Joshua/Desktop/jOutFiles/SimpleDB.txt"); //
FileWriter writer = new FileWriter(file, true); //

String function = event.getActionCommand();

//Reads the ActionCommand into a string for use in performing desired function
if( function.equals("Read")){ //Read function
Desktop desktop = Desktop.getDesktop(); //For the GUI version, supposed to open "targetFile" upon readButtonClick
desktop.open(targetFile); //
// SimpleDBReader sdbrObject = new SimpleDBReader(); //Bit left from console version, still functional
// sdbrObject.sdbReader(inputChoice, file); //
}else if( function.equals("Search")){ //Search Function
SimpleDBSearch sdbsObject = new SimpleDBSearch();
sdbsObject.sdbSearch(inputChoice, writer, file);
}else if( function.equals("Write")){ //Write function
SimpleDBWriter sdbwObject = new SimpleDBWriter();
sdbwObject.sdbWriter(inputChoice, file);
}else if( function.equals("Exit")){
System.exit(0);
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

最佳答案

与大多数 GUI 一样,Swing 是事件驱动的,也就是说,发生某些事情并且您对其做出响应。您的代码在方法上相当线性,您创建 UI 组件,但是,甚至在 UI 呈现给用户之前,您就执行 targetFile = new File(sdbTarget.getText());

但是 sdbTarget 尚未应用任何值。

但更好的解决方案是使用 ActionListenerButtonClickListener 来检查值

String target = sdbTarget.getText();
if (target != null && !target.trim().isEmpty()) {
File targetFile = new File(target)
String function = event.getActionCommand(); //Reads the ActionCommand into a string for use in performing desired function
if (function.equals("Read")) { //Read function
Desktop desktop = Desktop.getDesktop(); //For the GUI version, supposed to open "targetFile" upon readButtonClick
desktop.open(targetFile);
//...

这样,每次调用 actionPerformed 时,您总是会评估 targetFile

关于java - .getText 在用户能够输入之前抓取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33817308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com