Results 1 to 6 of 6
Thread: Writing GUI
- 02-11-2012, 05:55 PM #1
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Writing GUI
Hi,
Still a noob at Java, but I think I eventually gets it....gif)
I have followed at tutorial I found.
This is the code:
The program runs fin, but the event seems not to work.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(); } }); } }
When I click the buttons nothing happens.
What could be wrong?
- 02-11-2012, 06:22 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Writing GUI
You didn't add the ActionListener to the buttons. For example:
Java Code:redButton.addActionListener( this );
- 02-11-2012, 07:44 PM #3
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Writing GUI
Added this code:
But it still don't work. Theres no errors...Java Code:JButton redButton = new JButton("Rød knapp"); redButton.setLocation(0, 0); redButton.setSize(100, 30); redButton.addActionListener(this); buttonPanel.add(redButton);
-
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:
Do you see the difference? It's subtle but very important.Java Code:class Foo { JButton bar; public void myMethod() { bar = new JButton("bar"); } }
- 02-11-2012, 08:20 PM #5
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Writing GUI
Awesome! Thanks.
Works like a charm!
-
Similar Threads
-
Need Help W/ Writing to DB.
By Sabinno in forum JDBCReplies: 2Last Post: 05-26-2011, 09:25 AM -
writing to ram
By Timmins in forum New To JavaReplies: 0Last Post: 04-03-2011, 08:52 AM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
Writing in XLS
By selva.bics in forum Advanced JavaReplies: 1Last Post: 11-09-2009, 09:09 AM -
writing to gui
By rob in forum New To JavaReplies: 2Last Post: 02-13-2009, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks