gpt4 book ai didi

java - 用户提供字符串后将 JTextField 中的文本分配给文件

转载 作者:行者123 更新时间:2023-12-01 10:52:03 25 4
gpt4 key购买 nike

我的程序被设计为能够读取文件、在文件中搜索以及写入文件(每个文件都有一个相应的 JButton)——具体取决于用户输入。它最初是一个控制台应用程序,但现在我正在为其开发一个 GUI(使用 SWING)。我遇到的问题是,用户指定的文件(字符串格式)(JTextField 中的类型)在用户提供输入之前被读入“targetFile”变量 - 导致空值。

我希望程序等到用户单击所需的按钮后将字符串读入“targetFile”变量。

这是我想要完成的最小代码:

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 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){

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
try {
desktop.open(targetFile); //Surrounded with Try/Catch because Java complains
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

我在 main 中引用了这个类,因为你不能直接使用 use main 来创建 GUI。 “targetFile = new File()”行返回的 null 值会导致 IllegalArgumentException,表示我的文件不存在。

我通过调试器运行了代码,全程单步执行,并且运行完美。当我正常运行它时,上述行不会给用户时间输入文件路径,或等待用户单击按钮,然后再尝试从 JTextField 获取文本(尚不存在)。

这是我最初的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file:  doesn't exist.
at java.awt.Desktop.checkFileValidation(Desktop.java:210)
at java.awt.Desktop.open(Desktop.java:270)
at simpleDatabase.SimpleDBGUI$ButtonClickListener.actionPerformed(SimpleDBGUI.java:108)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
<小时/>
public class ButtonClickListener implements ActionListener{         //Sets the EventListener for every function

public void actionPerformed(ActionEvent event){

File targetFile = new File(sdbTarget.getText());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);

我当前的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at simpleDatabase.SimpleDBGUI$ButtonClickListener.actionPerformed(SimpleDBGUI.java:101)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

最佳答案

只需在监听器内创建 targetFile 即可。

您想要在用户按下按钮时抓取文本。这意味着按下按钮时必须调用 sdbTarget.getText():

public void actionPerformed(ActionEvent e) {
File file = new File(sdbTarget.getText());

//...
}

您当前在创建组件后不久调用它,这就是它返回空String的原因。

<小时/>

如果您使用的是 Java 8+,您可以为 ActionListener 使用 lambda 表达式:

button.addActionListener(e -> {

});

虽然你现在拥有的很好,但如果你宁愿将代码分开。

JTextField 还支持 ActionListener。当用户在字段处于焦点状态时按 Enter 键时,它会触发一个事件。

关于java - 用户提供字符串后将 JTextField 中的文本分配给文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33813013/

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