Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
 <pre lang="C#">
import javax.swing.*; // Graphics
import java.awt.Color; // Graphics Colors
import java.awt.event.ActionListener; // Evrnts
import java.awt.event.ActionEvent; // Evrnts

public class ButtonDemo_Extended implements ActionListener {

    // Definition of global values and items that are part of the GUI.
    int redScoreAmount = 0;
    int blueScoreAmount = 0;

    JPanel titlePanel, scorePanel, buttonPanel;
    JLabel redLabel, blueLabel, redScore, blueScore;
    JButton redButton, blueButton, resetButton;

    public JPanel createContentPane() {

        // We create a bottom JPanel to place everything on.
        JPanel totalGUI = new JPanel();
        totalGUI.setLayout(null);

        // Creation of a Panel to contain the title labels
        titlePanel = new JPanel();
        titlePanel.setLayout(null);
        titlePanel.setLocation(10, 10);
        titlePanel.setSize(250, 30);
        totalGUI.add(titlePanel);

        redLabel = new JLabel(&quot;Red Team&quot;);
        redLabel.setLocation(0, 0);
        redLabel.setSize(120, 30);
        redLabel.setHorizontalAlignment(0);
        redLabel.setForeground(Color.red);
        titlePanel.add(redLabel);

        blueLabel = new JLabel(&quot;Blue Team&quot;);
        blueLabel.setLocation(130, 0);
        blueLabel.setSize(120, 30);
        blueLabel.setHorizontalAlignment(0);
        blueLabel.setForeground(Color.blue);
        titlePanel.add(blueLabel);

        // Creation of a Panel to contain the score labels.
        scorePanel = new JPanel();
        scorePanel.setLayout(null);
        scorePanel.setLocation(10, 40);
        scorePanel.setSize(260, 30);
        totalGUI.add(scorePanel);

        redScore = new JLabel(&quot;&quot; + redScoreAmount);
        redScore.setLocation(0, 0);
        redScore.setSize(120, 30);
        redScore.setHorizontalAlignment(0);
        scorePanel.add(redScore);

        blueScore = new JLabel(&quot;&quot; + blueScoreAmount);
        blueScore.setLocation(130, 0);
        blueScore.setSize(120, 30);
        blueScore.setHorizontalAlignment(0);
        scorePanel.add(blueScore);

        // Creation of a Panel to contain all the JButtons.
        buttonPanel = new JPanel();
        buttonPanel.setLayout(null);
        buttonPanel.setLocation(10, 80);
        buttonPanel.setSize(260, 70);
        totalGUI.add(buttonPanel);

        // We create a button and manipulate it using the syntax we have
        // used before. Now each button has an ActionListener which posts 
        // its action out when the button is pressed.
        redButton = new JButton(&quot;Red Score!&quot;);
        redButton.setLocation(0, 0);
        redButton.setSize(120, 30);
        redButton.addActionListener(this);
        buttonPanel.add(redButton);

        blueButton = new JButton(&quot;Blue Score!&quot;);
        blueButton.setLocation(130, 0);
        blueButton.setSize(120, 30);
        blueButton.addActionListener(this);
        buttonPanel.add(blueButton);

        resetButton = new JButton(&quot;Reset Score&quot;);
        resetButton.setLocation(0, 40);
        resetButton.setSize(250, 30);
        resetButton.addActionListener(this);
        buttonPanel.add(resetButton);
        return totalGUI;
    }

    // This is the new ActionPerformed Method.
    // It catches any events with an ActionListener attached.
    // Using an if statement, we can determine which button was pressed
    // and change the appropriate values in our GUI.
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == redButton) {
            redScoreAmount = redScoreAmount + 1;
            redScore.setText(&quot;&quot; + redScoreAmount);
            JOptionPane.showMessageDialog(buttonPanel, &quot;GOOOOOOOOOOOL&quot;);
        } else if (e.getSource() == blueButton) {
            blueScoreAmount = blueScoreAmount + 1;
            blueScore.setText(&quot;&quot; + blueScoreAmount);
            JOptionPane.showMessageDialog(buttonPanel, &quot;GOOOOOOOOOOOL&quot;);
        } else if (e.getSource() == resetButton) {
            redScoreAmount = 0;
            blueScoreAmount = 0;
            redScore.setText(&quot;&quot; + redScoreAmount);
            blueScore.setText(&quot;&quot; + blueScoreAmount);
        }
    }

    private static void createAndShowGUI() { // For this Class Onlyyyyyyyyyyyy .

        JFrame.setDefaultLookAndFeelDecorated(true); // Style Of Frame
        JFrame frame = new JFrame(&quot;[=] JButton Scores! [=]&quot;);

        //Create and set up the content pane.
        ButtonDemo_Extended demo = new ButtonDemo_Extended();
        frame.setContentPane(demo.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(280, 190);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        createAndShowGUI();

    }
}</pre>


What I have tried:

( my java program is score app that increase score by 1 when i click on the button and pops up new window says goal what i want it to make the new window close itself by timer ) some help please
Posted
Updated 25-Apr-16 22:38pm
Comments
Sergey Alexandrovich Kryukov 25-Apr-16 11:32am    
And why would you need a timer for that? Anyway, everything is hard-coded in your code; this is not maintainable...
—SA
samehosama 25-Apr-16 12:31pm    
i need it for college project and i need to add timer to just make it better this will be semster project for college
Sergey Alexandrovich Kryukov 25-Apr-16 13:26pm    
Why can it be better? :-)
You have to describe the ultimate goals of all that.
—SA
samehosama 25-Apr-16 14:42pm    
convince the professor that my project worth to be graded is something easy for me but i need a full project to use xD and if i have that timer thing it will be completed for me so far i hope that anyone can really help me on such thing
Sergey Alexandrovich Kryukov 25-Apr-16 16:25pm    
Should I? If you "need a full project" and don't need an advice, develop this project.
You are the one who needs to understand what you need and why; it's not related to what you professor said or required...

Your assignment is probably designed for you to complete, not for other people. You did not ask any certain question and failed to answer a very basic "why" question related to the code you've written. How such person could possibly get help?

And if you want your professor to talk to me, make him address me. Perhaps he needs and advice, not you.

—SA

1 solution

showMessageDialog is no use because it needs user input to close. You need to use a Modeless dialog as described in https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html[^].
 
Share this answer
 
Comments
samehosama 26-Apr-16 6:30am    
Richard MacCutchan thank you alot thats was helpfull for me ^_^ <3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900