Results 1 to 6 of 6

Thread: Writing GUI

  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Writing GUI

    Hi,
    Still a noob at Java, but I think I eventually gets it...

    I have followed at tutorial I found.
    This is the code:
    Java Code:
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class ButtonDemo implements ActionListener{
    	
    	int redScoreAmount = 0;
    	int blueScoreAmount = 0;
    	
    	JPanel titlePanel, scorepanel, buttonpanel;
    	JLabel redlabel, bluelabel, redscore, bluescore;
    	JButton redButton, blueButton, resetButton;
    	
    	public JPanel createContentPane (){
    		JPanel totalGUI = new JPanel();
    		totalGUI.setLayout(null);
    		
    		JPanel titlePanel = new JPanel();
    		titlePanel.setLayout(null);
    		titlePanel.setLocation(10, 0);
    		titlePanel.setSize(250, 30);
    		totalGUI.add(titlePanel);
    		
    		JLabel redlabel = new JLabel("Rød overskrift");
    		redlabel.setLocation(0, 0);
    		redlabel.setSize(100, 30);
    		redlabel.setHorizontalAlignment(0);
    		redlabel.setForeground(Color.red);
    		titlePanel.add(redlabel);
    		
    		JLabel bluelabel = new JLabel("Blå overskrift");
    		bluelabel.setLocation(120, 0);
    		bluelabel.setSize(100, 30);
    		bluelabel.setHorizontalAlignment(0);
    		bluelabel.setForeground(Color.blue);
    		titlePanel.add(bluelabel);
    		
    		JPanel scorepanel = new JPanel();
    		scorepanel.setLayout(null);
    		scorepanel.setLocation(10, 40);
    		scorepanel.setSize(250, 30);
    		totalGUI.add(scorepanel);
    		
    		JLabel redscore = new JLabel("0");
    		redscore.setLocation(0, 0);
    		redscore.setSize(100, 30);
    		redscore.setHorizontalAlignment(0);
    		scorepanel.add(redscore);
    		
    		JLabel bluescore = new JLabel("0");
    		bluescore.setLocation(120, 0);
    		bluescore.setSize(100, 30);
    		bluescore.setHorizontalAlignment(0);
    		scorepanel.add(bluescore);
    		
    		JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(null);
    		buttonPanel.setLocation(10, 80);
    		buttonPanel.setSize(250, 70);
    		totalGUI.add(buttonPanel);
    		
    		JButton redButton = new JButton("Rød knapp");
    		redButton.setLocation(0, 0);
    		redButton.setSize(100, 30);
    		buttonPanel.add(redButton);
    		
    		JButton blueButton = new JButton("Blå knapp");
    		blueButton.setLocation(120, 0);
    		blueButton.setSize(100, 30);
    		buttonPanel.add(blueButton);
    		
    		JButton resetButton = new JButton("Nullstill");
    		resetButton.setLocation(0, 40);
    		resetButton.setSize(220, 30);
    		buttonPanel.add(resetButton);
    		
    		totalGUI.setOpaque(true);
    		return totalGUI;
    	}
    	
    public void actionPerformed(ActionEvent e) {
    	if(e.getSource() == redButton)
    	{
    		redScoreAmount = redScoreAmount + 1;
    		redscore.setText(""+redScoreAmount);
    	}
    	else if(e.getSource() == blueButton)
    	{
    		blueScoreAmount = blueScoreAmount + 1;
    		bluescore.setText(""+blueScoreAmount);
    	}
    	else if(e.getSource() == resetButton)
    	{
    		redScoreAmount = 0;
    		blueScoreAmount = 0;
    		redscore.setText(""+redScoreAmount);
    		bluescore.setText(""+blueScoreAmount);
    	}
    }
    
    
    	private static void createAndShowGUI() {
    		
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		JFrame frame = new JFrame("[=] JButton Scores! [=]");
    		
    		ButtonDemo demo = new ButtonDemo();
    		frame.setContentPane(demo.createContentPane());
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(250, 190);
    		frame.setVisible(true);
    	}
    	
    	public static void main (String[] args) {
    		
    		SwingUtilities.invokeLater(new Runnable() { 
    			public void run() {
    				createAndShowGUI();
    			}
    		});
    		}
    	}
    The program runs fin, but the event seems not to work.
    When I click the buttons nothing happens.

    What could be wrong?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Writing GUI

    You didn't add the ActionListener to the buttons. For example:

    Java Code:
    redButton.addActionListener( this );

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Writing GUI

    Added this code:

    Java Code:
    JButton redButton = new JButton("Rød knapp");
    		redButton.setLocation(0, 0);
    		redButton.setSize(100, 30);
    		redButton.addActionListener(this);
    		buttonPanel.add(redButton);
    But it still don't work. Theres no errors...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing GUI

    You're re-declaring your JButtons (all your fields actually) in the createContentPane method. Those JButtons declared inside of this method are visible only inside of this method, and so are not visible your actionPerformed method. The JButton class fields are all in fact null. The solution is not to re-declare the variable inside of the method but rather to initialize the JButtons inside of the createContentPane method that have already been declared in the class.

    In other words, don't do this:

    Java Code:
    class Foo {
      JButton bar;
    
      public void myMethod() {
         JButton bar = new JButton("bar");
      }
    }

    But instead do this:

    Java Code:
    class Foo {
      JButton bar;
    
      public void myMethod() {
         bar = new JButton("bar");
      }
    }
    Do you see the difference? It's subtle but very important.

  5. #5
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Writing GUI

    Awesome! Thanks.

    Works like a charm!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Need Help W/ Writing to DB.
    By Sabinno in forum JDBC
    Replies: 2
    Last Post: 05-26-2011, 10:25 AM
  2. writing to ram
    By Timmins in forum New To Java
    Replies: 0
    Last Post: 04-03-2011, 09:52 AM
  3. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  4. Writing in XLS
    By selva.bics in forum Advanced Java
    Replies: 1
    Last Post: 11-09-2009, 10:09 AM
  5. writing to gui
    By rob in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 11:55 PM

Posting Permissions

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