Results 1 to 3 of 3
  1. #1
    dbomb is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default having issues getting my action listener to write the variables

    Hi, sorry if this isn't how you guys like the code to be posted, but i need some help with my code. I want a play button to pop up when the user selects the number of players and the ball speed. I set both of these to limited amounts and I think that it is how i get my action listener to get the variables, but I'm not sure. some help would be really appreciated, Thx



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

    public class PongInterface extends JFrame implements ActionListener
    {
    public PongInterface()
    {
    createControlPanel();
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }
    public void createControlPanel()
    {
    JPanel facenamePanel1 = createComboBox1();
    JPanel facenamePanel2 = createComboBox2();

    controlPanel = new JPanel();
    controlPanel.setLayout(new GridLayout(1,2));
    controlPanel.add(facenamePanel1);
    controlPanel.add(facenamePanel2);

    add(controlPanel, BorderLayout.SOUTH);
    }
    public JPanel createComboBox1()
    {
    facenameCombo1 = new JComboBox();
    facenameCombo1.addItem("1 Player");
    facenameCombo1.addItem("2 Player");
    facenameCombo1.setEditable(false);
    facenameCombo1.addActionListener(this);
    JPanel panel = new JPanel();
    panel.add(facenameCombo1);
    return panel;
    }
    public JComboBox getplayer ()
    {
    return facenameCombo1;
    }
    public JPanel createComboBox2()
    {
    facenameCombo2 = new JComboBox();
    facenameCombo2.addItem("1 speed");
    facenameCombo2.addItem("3 speed");
    facenameCombo2.addItem("5 speed");
    facenameCombo2.addItem("bullet speed");
    facenameCombo2.setEditable(false);
    facenameCombo2.addActionListener(this);
    JPanel panel = new JPanel();
    panel.add(facenameCombo2);
    return panel;
    }
    public JComboBox getspeed()
    {
    return facenameCombo2;
    }
    public void actionPerformed(ActionEvent e)
    {
    JComboBox player = facenameCombo1;
    String player1 = player.getActionCommand();
    updatePlayer(player1);
    JComboBox speed = facenameCombo2;
    String speed1 = speed.getActionCommand();
    updateSpeed(speed1);
    }
    public void updatePlayer(String player1)
    {
    if (player1.equals("1 Player"))
    {
    player = 2;
    }
    else
    {
    player = 2;
    }
    }
    public void updateSpeed(String speed1)
    {
    if (speed1.equals("1 speed"))
    {
    speed = 5;
    }
    else if (speed1.equals("3 speed"))
    {
    speed = 10;
    }
    else if (speed1.equals("5 speed"))
    {
    speed = 15;
    }
    else
    {
    speed = 20;
    }
    }
    public int getPlayer()
    {
    return player;
    }
    public int getSpeed()
    {
    return speed;
    }
    private JPanel controlPanel;
    private static final int FRAME_HEIGHT = 600;
    private static final int FRAME_WIDTH = 600;
    private JComboBox facenameCombo1;
    private JComboBox facenameCombo2;
    private int speed;
    private int player;
    }

    by the way any tips if i need to post in the future?

  2. #2
    rithish is offline Member
    Join Date
    Apr 2011
    Posts
    40
    Rep Power
    0

    Default

    hello please place this program in code tag

  3. #3
    dbomb is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    Hi thanks for responding, my teacher wanted us to reformat our programs so that they are easier to read, but theres more of them. So, heres what i have so far. My end goal is to get a button to display when the variables are changed. However, im still having issues with the setting the variables part. So, i put in some tests to see what was printing and after reorganizing im getting javanullpointerexception.

    the control panel class:
    XML Code:
    import java.awt.*;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class PongInterface extends JFrame
    {	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	public PongInterface()
    	{
    		createControlPanel();
    		setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		ActionListener listener = (ActionListener) new ComboListener();
    		Pong = new SpeedCombo();
    		Pong1 = new PlayerCombo();
    		JCB = Pong1.getfacenameCombo1();
    		JCB1 = Pong.getfacenameCombo2();
    		JCB.addActionListener(listener);
    		JCB1.addActionListener(listener);
    	}
    	public void createControlPanel() 
    	{
    		controlPanel = new JPanel();
    		controlPanel.setLayout(new GridLayout(1,2));
    		controlPanel.add(JCB);
    		controlPanel.add(JCB1);
    		
    		add(controlPanel, BorderLayout.SOUTH);	
    	}
    private SpeedCombo Pong;
    private PlayerCombo Pong1;
    private JPanel controlPanel;
    private JComboBox JCB;
    private JComboBox JCB1;
    private static final int FRAME_HEIGHT = 590;
    private static final int FRAME_WIDTH = 850;
    }
    the interface for Pong:

    XML Code:
    import java.io.IOException;
    import javax.swing.JFrame;
    
    public class PongInterfaceViewer extends JFrame
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public static void main(String[]args) throws IOException
    	{
            JFrame frame = new PongInterface();
    		BackGround component = new BackGround( );
    		frame.getContentPane( ).add(component);
    		PlayerCombo pong2 = new PlayerCombo();
    		SpeedCombo pong3 = new SpeedCombo();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		int player = pong2.getPlayer();
    		int speed = pong3.getSpeed();
    		while (speed!=5 && player!=2)
    		{
    			player = pong2.getPlayer();
    			speed = pong3.getSpeed();
    			System.out.println(speed);
    			System.out.println(player);
    		}
    	}
    }
    the background i put for the interface:

    XML Code:
    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JComponent;
    
    
    public class BackGround extends JComponent
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	public BackGround () throws IOException
    	{
          	String inputFileName = "welcomescreen.gif";
          	backgroundImage = ImageIO.read(new File(inputFileName));
    	}
    	 public void paintComponent(Graphics g) 
    	{
    		 super.paintComponents(g);
    
    		 g.drawImage(backgroundImage, 0, 0, null);
    	}
    	private Image backgroundImage;
    }
    the action listener:

    XML Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JComboBox;
    
    public class ComboListener implements ActionListener
    {
    	public void actionPerformed(ActionEvent evt) 
    	{
    	    JComboBox cb = (JComboBox) evt.getSource();
    	    Object obj = cb.getSelectedItem();
    		SpeedCombo Pong = new SpeedCombo();
    		PlayerCombo Pong1 = new PlayerCombo();
    		Pong1.updatePlayer((String) obj);
    		Pong.updateSpeed((String) obj);
    	}
    }
    the speed variable combo box:

    XML Code:
    import javax.swing.JComboBox;
    
    public class SpeedCombo 
    {
    	public SpeedCombo ()
    	{
    		facenameCombo2 = new JComboBox();
    		facenameCombo2.addItem("1 speed");
    		facenameCombo2.addItem("3 speed");
    		facenameCombo2.addItem("5 speed");
    		facenameCombo2.addItem("bullet speed");
    		facenameCombo2.setEditable(false);
    	}
    	public JComboBox getfacenameCombo2()
    	{
    		return facenameCombo2;
    	}
    	public void updateSpeed(String Speed)
    	{
    		if (Speed.equals("1 speed"))
    			speed = 5;
    		else if (Speed.equals("3 speed"))
    			speed = 10;
    		else if (Speed.equals("5 speed"))
    			speed = 15;
    		else
    			speed = 20;
    	}
    	public int getSpeed()
    	{
    		return speed;
    	}
    	private int speed;
    	private JComboBox facenameCombo2;
    }
    the player combo box:

    XML Code:
    import javax.swing.JComboBox;
    
    public class PlayerCombo 
    {
    	public PlayerCombo()
    	{
    		facenameCombo1 = new JComboBox();
    		facenameCombo1.addItem("1 Player");
    		facenameCombo1.addItem("2 Player");
    		facenameCombo1.setEditable(false);
    	}
    	public JComboBox getfacenameCombo1()
    	{
    		return facenameCombo1;
    	}
    	public void updatePlayer(String Speed)
    	{
    		if (Speed.equals("1 Player"))
    			player = 2;
    		else
    			player = 2;
    	}
    	public int getPlayer()
    	{
    		return player;
    	}
    	private JComboBox facenameCombo1;
    	private int player;
    }

Similar Threads

  1. Problem in action listener
    By cool in forum AWT / Swing
    Replies: 4
    Last Post: 11-16-2010, 08:44 AM
  2. Simple Action/Listener Help GUI
    By aanders5 in forum New To Java
    Replies: 24
    Last Post: 10-18-2010, 07:43 PM
  3. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 06:39 PM
  4. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM
  5. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 11:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •