package vraag1; import java.awt.Graphics; import java.awt.event.*; import javax.swing.*; @SuppressWarnings("serial") public class LijnPaneel extends JPanel implements ActionListener { private JTextField afstandVeld; private JButton tekenKnop; private int afstand = -1; public LijnPaneel() { afstandVeld = new JTextField(4); tekenKnop = new JButton("Teken de lijnen"); tekenKnop.addActionListener(this); this.add(new JLabel("Afstand tussen de lijnen")); this.add(afstandVeld); this.add(tekenKnop); } public void actionPerformed(ActionEvent e) { try { afstand = Integer.parseInt(afstandVeld.getText()); if (afstand <= 0) throw new ArithmeticException(); repaint(); } catch(NumberFormatException ex) { JOptionPane.showMessageDialog(null, "De input moet een getal zijn", "Foute ingave", JOptionPane.ERROR_MESSAGE); } catch(ArithmeticException ex) { JOptionPane.showMessageDialog(null, "Het getal moet groter dan 0 zijn", "Foute ingave", JOptionPane.ERROR_MESSAGE); } } public void paintComponent(Graphics g) { super.paintComponent(g); // hier code voor lijnen schrijven } public static void main(String[] args) { JFrame f = new JFrame(); f.setSize(500,500); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("Lijnen tekenen"); f.setLocation(100, 100); //standaard in de hoek van het scherm JPanel hoofdpaneel = new LijnPaneel(); f.add(hoofdpaneel); f.setVisible(true); } }