ユーザーからの入力 (いくつかの数値と数学関数 f(x,y...)) を受け取り、それらをファイルに書き込む単純なスタンドアロン アプリケーションを作成したいと考えています。次に、このファイルを使用してコマンドを実行します。
私が必要とする基本的な成分:
-- ユーザー入力用の 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 クラス内で他のオブジェクト (つまり、ファイルのオープンと書き込みなど) を定義できないという事実から (主に、しかしそれだけではありません) 発生します。