Drawing text inside JPanel

I am looking for the most detailed description of how to draw text inside JPanel. I know there are a billion textbooks, but none of them click with me, and I have some specific questions that may help others who are embarrassed. As an installation (test application), I have one class that has JLabel, JTextField, JButton and JPanel. The application reads in ints from an external file and should display the average value in the panel when pressing JButton. I have all the basic program settings (that is, the button responds and prints the average value on the command line), but I just can not figure out how to print the average value on the panel. I think my biggest question is how to include the paint () or paintComponet () method along with the rest of the code. Should he be his own class? Should JPanel be its own class? Most of the textbooks seem to tell me, I'm just not sure what the first step is. The code looks like this:

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); } } 

Any help / direction is appreciated. I know I recently used this code for other questions, I just want to know everything! Ideally, the panel displays the average reading in ints when the button is pressed and displays everything that was entered in the textfeild when the focus was on it and the click was pressed, but I am taking steps for the child, and, as I said, I would like so that this thread is a general training tool for others with similar questions that do not find answers from solar documents or other sites. Thank you very much in advance. Dan:)

+6
java graphics paint
source share
4 answers

Create an inner class that extends JPanel inside your main class:

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

Then you need to call a redraw on this panel after calling computeAverage () in actionPerformed:

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

Add JLabel to JPanel.

Call setText (String) in the JLabel and your text will be drawn inside the JPanel.

+7
source share

1) JFrame does not have a paintComponent () method, so the code you submitted will not do anything. You need to create a custom JPanel and override its paintComponent () method to make your own paint.

2) Even if you do this, the picture will still not be displayed, because the panel has a default size of zero. Therefore, you will need to set your preferred panel size to make sure it is visible.

3) Why are you doing this. All you have to do is use JLabel and set the text to JLabel.

I find it hard to believe that you looked at other lessons. The Swing tutorial on Custom Painting contains a 20-line program that shows the basics.

+2
source share

I think you should not subclass JFrame. Make an instance of a JFrame
Main class variable and add JPanel etc. to him.

-one
source share

All Articles