I currently have a program written that outputs a canvas object and adds a picture of 5 taxis to it.
I have now added a jtextfield so that a user can add an interger. Ideally if the user was to enter the number 8, there would be 8 taxis added to the canvas.
I am having trouble with the final part i mentioned... im not sure how to delete the old canvas and output a new one with the amount that the user wrote in the jtextfield.
Could you please tell me where i am going wrong on my code and offer a solution, any code you can include would be a great help.
thanks alot
ps. i am very new to this so please be nice
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.*; public class TaxiFrame extends JFrame implements ActionListener { private JLabel L1 = new JLabel("Number of Taxis:"); private JLabel L2 = new JLabel("Type an interger and press enter"); private JTextField t1 = new JTextField (" "); public TaxiFrame() { super("This is the Frame"); setSize(600, 400); getContentPane().setBackground(Color.CYAN); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new BorderLayout(10, 10)); Random rx = new Random(); Random ry = new Random(); for(int i = 0; i < 5; i ++) { TaxiCanvas tax = new TaxiCanvas(); tax.setBounds(rx.nextInt(600 - 100), ry.nextInt(400 - 100), 100, 100); add(tax); } JPanel p = new JPanel(); p.setOpaque(false); p.add(L1); getContentPane(). add("South", p); p.setOpaque(false); p.add(t1); getContentPane(). add("South", p); p.setOpaque(false); p.add(L2); getContentPane(). add("South", p); setVisible(true); t1.addActionListener(this); } public static void main(String[] args) { new TaxiFrame(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == t1) { if(Integer.parseInt(t1.getText()) > 0) { getContentPane().removeAll(); TaxiCanvas tax = new TaxiCanvas(); add(tax); } } } }