-
Writing GUI
Hi,
Still a noob at Java, but I think I eventually gets it... :(giggle):
I have followed at tutorial I found.
This is the code:
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?
-
Re: Writing GUI
You didn't add the ActionListener to the buttons. For example:
Code:
redButton.addActionListener( this );
-
Re: Writing GUI
Added this code:
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... :(-:
-
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:
Code:
class Foo {
JButton bar;
public void myMethod() {
JButton bar = new JButton("bar");
}
}
But instead do this:
Code:
class Foo {
JButton bar;
public void myMethod() {
bar = new JButton("bar");
}
}
Do you see the difference? It's subtle but very important.
-
Re: Writing GUI
Awesome! Thanks.
Works like a charm! :(handshake):
-
Re: Writing GUI