2013-08-06 54 views
0

我想创建一个简单的独立应用程序,它将接受来自用户的某些输入(某些数字和数学函数f(x,y ...))并将它们写入文件。然后在这个文件的帮助下,我将运行一个命令。如何读取用户在Java中的输入并将其写入文件

我需要的基本成分:

- 用户输入的JTextArea。

- ButtonHandler/ActionListener的和输入的写入(TXT)文件

- ButtonHandler/ActionLister执行命令

什么是做到这一点的最好方法是什么?

当前运行的代码,我有(基本上是一个玩具) - 不写什么,只是执行 - 是:

import java.applet.*; 
import java.lang.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.Dialog; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.*; 
import java.util.*; 
import java.io.BufferedWriter; 

public class Runcommand3 
{ 
    public static void main(String[] args) throws FileNotFoundException, IOException 
    { 
    //JApplet applet = new JApplet(); 
    //applet.init(); 

    final JFrame frame = new JFrame("Change Backlight"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel panel = new JPanel(); 
    panel.setLayout(null); 
    frame.add(panel); 
    JButton button = new JButton("Click me to Run"); 
    button.setBounds(55,100,160,30); 
    panel.add(button); 

    frame.setSize(260,180); 
    frame.setVisible(true); 
    //This is an Action Listener which reacts to clicking on the button 
    button.addActionListener(new ButtonHandler()); 
    } 
} 
class ButtonHandler implements ActionListener{ 
       public void actionPerformed(ActionEvent event){ 
       double value = Double.parseDouble(
       JOptionPane.showInputDialog("please enter backlight value")); 
       //File theFile = new File("thisfile.txt"); 
       //theFile.write(value); 
       String command = "xbacklight -set " + value; 
       try{Runtime run = Runtime.getRuntime(); 
       Process pr = run.exec(command);} 
       catch(IOException t){t.printStackTrace();} 
        } 
       } 

在上面的例子中,我怎么能写“价值”文件?那么,我怎样才能添加更多的输入(更多的文本框)?我可以在同一班上做,还是需要更多? 我的困惑(主要但不仅仅是)来自于ButtonHandler类内部我无法定义任何其他对象(即打开和写入文件等)的事实。

+1

*如何从用户输入的Java读取和写入到一个文件*正是你的问题,不要问与您的问题无关的其他问题,请在另一个问题上提问。 – Azad

+0

为什么不接受? – 2013-08-06 18:59:56

+0

林不知道我该如何使用接受按钮,但我的问题没有解决,我不希望它出现'解决'。 –

回答

3

这是我写入文件的方式。我会让你将这些代码转换成你的GUI进行练习。查看更多关于BufferedWriterFileWriter

import java.io.BufferedWriter; 
import java.io.FileWriter; 
import java.util.Scanner; 

public class Files { 

    public static void main(String args[]){ 

     System.out.print("Enter Text: "); 
     Scanner scan = new Scanner(System.in); 
     String text = scan.nextLine(); 
     FileWriter fWriter = null; 
     BufferedWriter writer = null; 
     try { 
      fWriter = new FileWriter("text.txt"); 
      writer = new BufferedWriter(fWriter); 
      writer.write(text); 
      writer.newLine(); 
      writer.close(); 
      System.err.println("Your input of " + text.length() + " characters was saved."); 
     } catch (Exception e) { 
      System.out.println("Error!"); 
     } 
    } 

} 
+0

谢谢,但我不清楚如何将它合并到我的GUI代码中,以及如何将它与ActionListener结合使用。问题依然存在:我无法将一个类(公有)传递给另一个类(ButtonHandler)。 –

+0

使用'全局变量',然后设置它们并在动作侦听器中使用它们。 – 2013-08-06 20:03:27

1

关于第二个问题,你可能要考虑其对你的JFrame一个JTextField用户进入到线代替的JOptionPane。这只是一个简单的文本框,您可以在框中的内容每次按下按钮时添加到文件:

public static void main(String[] args) { 
    JTextField myTextField = new JTextField(); 
    // Your code, set size and position of textfield 
    panel.add(myTextField); 
} 

class ButtonHandler implements ActionListener { 
    public void actionPerformed(ActionEvent event) { 
     String text = myTextField.getText(); 
     myTextField.setText(""); 
     new BufferedWriter(new FileWriter("text.txt")).write(text).newLine().close(); 
     // the rest of your code 
    } 
} 
+0

感谢您的帮助,但我收到错误消息,它无法找到myTextField变量。我曾尝试过;出于某种原因(我不理解),在公共类中定义的变量在ButtonHandler类中无法识别。 –

相关问题