java – 在JPanel中绘制文本

前端之家收集整理的这篇文章主要介绍了java – 在JPanel中绘制文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找如何在JPanel中绘制文本的最基本的描述.我知道有十亿个教程,但没有一个与我点击,我有一些具体的问题可以帮助其他困惑的人.作为一个设置(测试应用程序),我有一个类,它有一个JLabel,一个JTextField,一个JButton和一个JPanel.应用程序从外部文件读取整数,并在按下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);
}
}

任何和所有帮助/方向表示赞赏.我知道我最近已经将这段代码用于其他问题,我只是想知道这一切!理想情况下,面板会在单击按钮时显示读取的平均值,并显示当焦点位于文本框架上并输入按下时输入的内容,但是我正在采取小步骤,就像我说的那样,我希望这个帖子能成为其他有类似问题但没有从sun文档或其他网站找到答案的人的一般教程.非常感谢提前.丹:)

解决方法

创建一个在Main类中扩展JPanel的内部类:
class MyPanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString(Integer.toString(avg),75);
    }

}

然后,您需要在actionPerformed中调用computeAverage()后在该面板上调用repaint:

//event handling
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == avgBtn) {
        computeAverage();
        panel.repaint();
    }
}

猜你在找的Java相关文章