Why can't I draw on my JFrame?

I am trying to use my JFrame , but I cannot force my super.paintComponents(g); work. Also, nothing draws my JFrame when I talk about this in my paintComponent() method.

Here is the code:

 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class MTGSAMPServerReference extends JFrame implements ActionListener { public static Toolkit tk = Toolkit.getDefaultToolkit(); static int ScrnWidth = ((int) tk.getScreenSize().getWidth()); static int ScrnHeight = ((int) tk.getScreenSize().getHeight()); private static final long serialVersionUID = 1L; private static JList list1; private static JButton next; public MTGSAMPServerReference() { // set flow layout for the frame this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING)); Object[]mainData = {"Vehicles", "Bikes/Bicycles", "Boats", "Houses", "Businesses", "Objects", "Jobs", "Ranks", "Licenses", "VIP"}; JPanel controls = new JPanel(new BorderLayout(5,5)); list1 = new JList<Object>(mainData); list1.setVisibleRowCount(10); next = new JButton("Next"); next.addActionListener(this); controls.add(new JScrollPane(list1)); controls.add(next, BorderLayout.PAGE_END); controls.setBorder(new EmptyBorder(25,25,0,0)); add(controls); } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Next")) { int index = list1.getSelectedIndex(); System.out.println("Index Selected: " + index); String s = (String) list1.getSelectedValue(); System.out.println("Value Selected: " + s); } } public void createAndShowGUI() { //Create and set up the window. JFrame f = new MTGSAMPServerReference(); //Display the window. f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(1200, 800); f.setLocationRelativeTo(null); list1.setSize(250, 250); list1.setLocation(0, 0); next.setSize(75, 25); next.setLocation(251, 276); MTGSAMPServerReference.this.repaint(); } protected void paintComponent(Graphics g) { //super.paintComponent(g); << Can't seem to get this to work. Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.drawRect(0, 0, 50, 50); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { MTGSAMPServerReference gui = new MTGSAMPServerReference(); gui.createAndShowGUI(); } }); } } 

I have worked with paintComponent() before, but still cannot figure out what I'm doing wrong. I know that this should be a simple fix, but cannot define it for me. Any ideas?

Any help is appreciated.

Thanks in advance!

+4
source share
2 answers

Use the @Override annotation for your paintComponent method for rude surprise. This is why using this annotation is very useful, as it will tell you at compile time if you do not override the method when you think it should be.

Solution: never "draw" in a JFrame for many reasons. Instead, do what the tutorials tell you - draw in the JPanel or JComponent method paintComponent(...) . If you search this site, you will find that we have told many people the same thing, and in fact I suggest that you do just that. I would not be surprised if this question is closed as a duplicate, since this is a fairly common question.

Note that this will not work (and will not actually compile):

 super.paintComponent(g); << Can't seem to get this to work. 

for the same reason - there is no super.paintComponent(g) for JFrame.

In addition, as far as

I used to work with paintComponent (), but still can't figure out what I'm doing wrong.

But if you look at your previous code, you will see that this method has never been used directly in the JFrame, and it should not either.

+4
source

paintComponent() is a member of the JPanel class, not the JFrame class in which you are trying to call it.

That is why you cannot call super.paintComponent(Graphics g) . The compiler believes that you create your own completely unrelated method, which is also called paintComponent() .

Create a class that inherits JPanel , and copy and draw your paintComponent() method.

As Hovercraft Full Of Eels commented, you can verify that you override methods correctly by adding the @Override tag directly above the method header; if you get an error message, you are doing something wrong.

For more information on JPanel and JFrame see my answer to this question .

+1
source

Source: https://habr.com/ru/post/1492816/


All Articles