JPanel 内でテキストを描画する方法の最も基本的な説明を探しています。そこには何十億ものチュートリアルがあることを私は知っていますが、それらのどれも私と一緒にクリックしていません.混乱している他の人を助けるかもしれないいくつかの特定の質問があります. セットアップ (テスト アプリ) として、JLabel、JTextField、JButton、および JPanel を持つ単一のクラスがあります。アプリケーションは外部ファイルから int を読み取り、JButton が押されたときにパネルにその平均を表示する必要があります。基礎となるすべてのプログラミングを整理しました (つまり、ボタンが応答して平均をコマンド ラインに出力します) が、平均をパネルに出力する方法を整理できないようです。私の最大の疑問は、paint() または paintComponet() メソッドを残りのコードと一緒に組み込む方法だと思います。それはそれ自身のクラスであるべきですか?JPanelがそれである必要があります' 自分のクラス?それがほとんどのチュートリアルで私に伝えていることのようですが、最初のステップが正確に何であるかはわかりません. コードは次のようになります。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Main extends JFrame implements ActionListener {
private int[] intArray = new int[10000];
private int numOfInts = 0;
private int avg = 0;
protected JButton avgBtn;
protected JTextField indexEntry;
protected JLabel instructions;
protected JPanel resultsPanel;
public Main(){
//create main frame
this.setTitle("Section V, question 2");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(350, 250);
this.setLayout(new GridLayout(4, 1));
//create instruction label and add to frame
instructions = new JLabel("Follow the instructions on the exam to use this program");
this.add(instructions);
//create textfield for index entry and add to frame
indexEntry = new JTextField();
this.add(indexEntry);
//create button for average and add to frame
avgBtn = new JButton("Click for Average");
this.add(avgBtn);
avgBtn.addActionListener(this);
//create panel to display results and add to frame
resultsPanel = new JPanel();
resultsPanel.setBackground(Color.BLUE);
this.add(resultsPanel);
//read in from file
readFromFile();
//compute average
computeAverage();
}
private void readFromFile() {
try {
// Open the file
FileInputStream fstream = new FileInputStream("numbers.dat");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
//create placeholder for read in ints
String strLine;
//Read File Line By Line
int i = 0;
while ((strLine = br.readLine()) != null) {
//place ints in array and increament the count of ints
System.out.println (strLine);
intArray[i] = Integer.parseInt(strLine);
numOfInts++;
i++;
}
//Close the input stream
in.close();
System.out.println ("numOfInts = " + numOfInts);
}
catch (Exception e) {
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
//compute averaage
private void computeAverage() {
int sum = 0;
for (int i = 0; i < numOfInts; i++)
sum += intArray[i];
avg = sum/numOfInts;
System.out.println("avg = " + avg);
}
//event handling
public void actionPerformed(ActionEvent e) {
if(e.getSource() == avgBtn) {
computeAverage();
}
}
//"main" function
public static void main(String[] args) {
Main m = new Main();
m.setVisible(true);
}
//paint
public void paintComponent(Graphics g){
g.drawString(avg, 75, 75);
}
}
すべてのヘルプ/指示をいただければ幸いです。最近このコードを他の質問に使用したことは知っていますが、すべてを知りたいだけです! 理想的には、パネルは、ボタンがクリックされたときに int で読み取られた平均値を表示し、フォーカスがその上にあり、Enter キーが押されたときにテキストフィールドに入力されたものを表示しますが、私は赤ちゃんの一歩を踏み出しています。私が言ったように、このスレッドが、sun docs や他のサイトから回答を見つけられない同様の質問を持つ他の人のための一般的なチュートリアルになることを願っています。よろしくお願いします。ダン:)