Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JPanel

Options
  • 23-03-2008 1:23am
    #1
    Closed Accounts Posts: 20


    i need a good site for explaining how a ticketmachinepanal is created

    my class is
    public class TicketMachinePanel extends JPanel implements ActionListener

    and i have to take information though the ticketmachine class that i have that is
    used by the panel.

    public TicketMachinePanel(TicketMachine machine)
    {
    this.machine = machine; //connect GUI to machine

    setPreferredSize(new Dimension(250, 100));

    getContentPane().add(machine);
    getContentPane().add(input);
    getContentPane().add(priceButton);
    getContentPane().add(prompt);

    display.setLineWrap(true);
    display.setEditable(false);

    priceButton.addActionListener(this);

    input.addActionListener(this);


Comments

  • Closed Accounts Posts: 619 ✭✭✭Afuera


    Have you implemented the ActionListener interface in your TicketMachinePanel class?
    (All it needs is an actionPerformed(ActionEvent e) method.)

    The addActionListener() method calls, that you already have, register a class to listen on the actions of a component. In your case, you have registered the TicketMachinePanel (i.e. "this") class to listen to the "priceButton" and the "input" components.

    When there is an action performed on these components the actionPerformed(ActionEvent e) method will get triggered. You will need to catch and parse each type of action, by reading the ActionEvent parameter that comes as part of the call. Check out the API docs for more details on what you can extract from the ActionEvent.


  • Closed Accounts Posts: 20 heavy482


    can you help..
    i need a
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    /**
    *
    */
    public class TicketMachinePanel extends JPanel implements ActionListener
    {
    private TicketMachine machine;
    private JTextField input=new JTextField(6);
    private JButton priceButton=new JButton("Button");
    private JLabel inputmsg=new JLabel("");
    private JLabel outputmsg=new JLabel("");

    public TicketMachinePanel(TicketMachine machine)
    {
    this.machine = machine; //connect GUI to machine

    setPreferredSize(new Dimension(250, 100));

    getContentPane().add(machine);
    getContentPane().add(input);
    getContentPane().add(priceButton);
    getContentPane().add(inputmsg);
    getContentPane().add(outputmsg);
    display.setLineWrap(true);
    display.setEditable(false);

    priceButton.addActionListener(this);

    input.addActionListener(this);


    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==priceButton || e.getSource()==input)

    {

    System.out.print(inputmsg);

    input.setText("");
    }

    }

    public static void main(String args[])
    {
    ButtonGUI f=new ButtonGUI();
    f.setSize(400,300);
    f.setVisible(true);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
    }


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    my java is fairly rusty but in your main method you aren't creating an instance of the TicketMachinePanel class.

    where has this ButtonGUI come from?


  • Closed Accounts Posts: 20 heavy482


    i have everything conpilling except the main class and im not sure why it cant reckonise it

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    /**
    *
    */
    public class TicketMachinePanel extends JPanel implements ActionListener
    {
    private TicketMachine machine;
    private JTextField display;
    private JButton priceButton;
    private JLabel inputmsg;
    private JLabel outputmsg;

    public TicketMachinePanel(TicketMachine machine)
    {
    this.machine = machine; //connect GUI to machine

    setPreferredSize(new Dimension(250, 100));

    JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));


    this.add(new JTextField(""));
    this.add(new JButton("Button"));
    this.add(new JLabel(""));
    this.add(new JLabel(""));


    priceButton.addActionListener(this);

    display.addActionListener(this);


    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==priceButton)
    {
    inputmsg.setText("dewjhdne");
    outputmsg.setText("dewjhdne");

    }

    }
    public static void main(String args[])
    {
    TicketMachinePanel p=new TicketMachinePanel();
    p.setSize(400,300);
    p.setVisible(true);

    TicketMachinePanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    }


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    p.setSize(400,300);
    p.setVisible(true);

    TicketMachinePanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    these should be in a constructor of some kind.

    also the setVisible() method has to be the last method called otherwise the way you have it now the setdefaulCloseOperation() will not act on the window.


  • Advertisement
  • Closed Accounts Posts: 20 heavy482


    how come it works in this one

    also i have JFrame at the EXIT_ON_CLOSE will this still work if its extending to JPanel


  • Closed Accounts Posts: 20 heavy482


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class ButtonGUI extends JFrame implements ActionListener
    {

    private JLabel prompt=new JLabel("Enter number to double:");
    private JTextField input=new JTextField(6);
    private JTextArea display= new JTextArea(10,20);
    private JButton doubleButton=new JButton("Double it!");

    public ButtonGUI()
    {
    setLayout(new FlowLayout());
    getContentPane().add(prompt);
    getContentPane().add(input);
    getContentPane().add(doubleButton);
    getContentPane().add(display);

    display.setLineWrap(true);
    display.setEditable(false);

    doubleButton.addActionListener(this);

    input.addActionListener(this);


    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==doubleButton || e.getSource()==input)

    {
    double value=Double.valueOf(input.getText()).doubleValue();
    double valueDoubled=value*2;
    display.append("Double of "+value+"is;"+valueDoubled+"\n");

    input.setText("");
    }

    }

    public static void main(String args[])
    {
    ButtonGUI f=new ButtonGUI();
    f.setSize(400,300);
    f.setVisible(true);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
    }


  • Closed Accounts Posts: 324 ✭✭radioactiveman


    basically because you extend JFrame in that code and set it to visible which is fine. If you extend JPanel, you'll have to add it to the jframe in main. It would be a call like f.add(this) - because 'this' is the JPanel. It's just a different approach.


  • Closed Accounts Posts: 20 heavy482


    thanks all sorted


Advertisement