Hi,
I'm new here and am just starting programming Java. (Started last year but failed )
I'm trying to figure out why my code doesn't work. I get the JFrame and JPanel but I just wanted to test whether or not I could draw 'something' (ex Oval) on my JPanel
and that seems not to work. I don't understand why because in school we have done it in about the same way (I wanted to try it
out this way) and it worked.
(In school we worked with --> Bal extends JFrame AND BalPaneel extends JPanel)
Can anybody point me out my mistake?
Thank you in advance!
package extraOef; import javax.swing.*; public class Bal { JFrame venster; public Bal() { venster = new JFrame(); venster.setSize(800,600); venster.setResizable(true); venster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); venster.setTitle("Bal tekenen"); venster.setLocation(venster.getWidth()/2,venster.getHeight()/2); BalPaneel BalPaneel = new BalPaneel(); venster.add(BalPaneel.BalPaneel); venster.setVisible(true); } public static void main(String[] args) { new Bal(); } } package extraOef; import javax.swing.*; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random; public class BalPaneel implements KeyListener, ActionListener { JPanel BalPaneel; JButton knop; JTextField tekstveld; int diameter; public BalPaneel() { BalPaneel = new JPanel(); BalPaneel.add(new JLabel("Geef de diameter in")); tekstveld = new JTextField(4); tekstveld.addKeyListener(this); knop = new JButton("Teken de bal"); knop.addActionListener(this); BalPaneel.add(tekstveld); BalPaneel.add(knop); } public void paintComponent(Graphics g) { int hoogte = BalPaneel.getHeight(); int breedte = BalPaneel.getWidth(); g.drawOval(200, 200, 100, 100); } @Override public void keyPressed(KeyEvent k) { int key = k.getKeyCode(); if (key == KeyEvent.VK_ENTER) { try { diameter = Integer.parseInt(tekstveld.getText()); BalPaneel.repaint(); } catch(Exception ex) { JOptionPane.showMessageDialog(null, "Diameter moet een getal zijn", "Foute Ingave", JOptionPane.ERROR_MESSAGE); } } } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void actionPerformed(ActionEvent e) { try { diameter = Integer.parseInt(tekstveld.getText()); BalPaneel.repaint(); } catch(Exception ex) { JOptionPane.showMessageDialog(null, "Diameter moet een getal zijn", "Foute Ingave", JOptionPane.ERROR_MESSAGE); } } }