Help please! I need help putting the timer in the event, and putting it in the class. I am having problems with the class particularly the scoreboard timer. I believe I figured every thing else out..Please view attached.
pattyps36 0 Newbie Poster
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Toolkit;
public class ScoreboardApp
{
public ScoreboardApp()
{
Frame frame = new ScoreboardFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.setVisible(true);
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
new ScoreboardApp();
}
}
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.*;
import java.text.*;
public class ScoreboardFrame extends JFrame
{
JMenuItem menuFileExit = new JMenuItem();
JMenu menuFile = new JMenu();
JMenuBar menuBar1 = new JMenuBar();
BorderLayout borderLayout1 = new BorderLayout();
JPanel panelScoreboard = new JPanel();
GridBagLayout gridBagLayout1 = new GridBagLayout();
JTextField txtHomeScore = new JTextField();
JTextField txtTimerdisplay = new JTextField();//changed
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField txtGuests = new JTextField();
JTextField txtTime = new JTextField();
JPanel panelButtons = new JPanel();
JButton btnTimeControl = new JButton();
JButton btnHomeAdd = new JButton();
JButton btnHomeSub = new JButton();
JButton btnGuestsAdd = new JButton();
JButton btnGuestsSub = new JButton();
DecimalFormat df = new DecimalFormat("00");
int homeScore = 0;
int guestsScore = 0;
int timerdisplay=10;//changed
public ScoreboardFrame()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.setJMenuBar(menuBar1);
this.getContentPane().setLayout(borderLayout1);
this.setSize(new Dimension(534, 300));
this.setTitle("Basketball Scoreboard");
menuFile.setText("File");
panelScoreboard.setBackground(new Color(99, 82, 227));
panelScoreboard.setLayout(gridBagLayout1);
txtHomeScore.setText("00");
txtHomeScore.setPreferredSize(new Dimension(100, 75));
txtHomeScore.setFont(new Font("SansSerif", 1, 60));
txtHomeScore.setForeground(Color.red);
txtHomeScore.setEditable(false);
jLabel1.setText("Home");
jLabel1.setFont(new Font("Dialog", 1, 20));
jLabel1.setForeground(Color.white);
jLabel2.setText("Guests");
jLabel2.setForeground(Color.white);
jLabel2.setFont(new Font("Dialog", 1, 20));
txtGuests.setText("00");
txtGuests.setCaretColor(Color.red);
txtGuests.setForeground(Color.red);
txtGuests.setEditable(false);
txtGuests.setPreferredSize(new Dimension(100, 75));
txtGuests.setFont(new Font("SansSerif", 1, 60));
txtTime.setText("10:00");
txtTime.setFont(new Font("SansSerif", 1, 60));
txtTime.setForeground(Color.red);
txtTime.setEditable(false);
btnTimeControl.setText("Start/Stop");
btnTimeControl.setForeground(Color.red);
btnTimeControl.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnTimeControl_actionPerformed(e);
}
});
btnHomeAdd.setText("Home +");
btnHomeAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnHomeAdd_actionPerformed(e);
}
});
btnHomeSub.setText("Home -");
btnHomeSub.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnHomeSub_actionPerformed(e);
}
});
btnGuestsAdd.setText("Guests +");
btnGuestsAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnGuestsAdd_actionPerformed(e);
}
});
btnGuestsSub.setText("Guests -");
btnGuestsSub.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnGuestsSub_actionPerformed(e);
}
});
menuFileExit.setText("Exit");
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fileExit_ActionPerformed(ae);
}
});
menuFile.add(menuFileExit);
menuBar1.add(menuFile);
panelScoreboard.add(txtHomeScore, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
panelScoreboard.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
panelScoreboard.add(jLabel2, new GridBagConstraints(4, 0, 1, 1, 1.0, 1.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
panelScoreboard.add(txtGuests, new GridBagConstraints(4, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
panelScoreboard.add(txtTime, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
this.getContentPane().add(panelScoreboard, BorderLayout.CENTER);
panelButtons.add(btnHomeAdd, null);
panelButtons.add(btnHomeSub, null);
panelButtons.add(btnTimeControl, null);
panelButtons.add(btnGuestsAdd, null);
panelButtons.add(btnGuestsSub, null);
this.getContentPane().add(panelButtons, BorderLayout.SOUTH);
}
void fileExit_ActionPerformed(ActionEvent e)
{
System.exit(0);
}
void btnTimeControl_actionPerformed(ActionEvent e)
{
timerdisplay--;//added line to decrement timer
txtTimerdisplay.setText(df.format(timerdisplay));//added line
}
void btnHomeAdd_actionPerformed(ActionEvent e)
{
homeScore++;
txtHomeScore.setText(df.format(homeScore));
}
void btnHomeSub_actionPerformed(ActionEvent e)
{
homeScore--;
txtHomeScore.setText(df.format(homeScore));
}
void btnGuestsAdd_actionPerformed(ActionEvent e)
{
guestsScore++;
txtGuests.setText(df.format(guestsScore));
}
void btnGuestsSub_actionPerformed(ActionEvent e)
{
guestsScore--;
txtGuests.setText(df.format(guestsScore));
}
}
public class ScoreboardTimer extends Thread
{
// declare you instance variables. You will need a counter for the seconds remaining, a boolean value to toggle
// the clock on and off and a text field reference so the GUI frame can pass you a reference to the object
// displaying the clock on the scoreboard. You will use this reference to update the timer display.
// constructor
public ScoreboardTimer(int initialTime, JTextField timerdisplay)
{
// private int minute;
// private int second;
int initialTime 10;
}
public boolean toggle()
{
// turn the clock control variable on if it is off and off if it is on
}
// create a getter and a setter for the counter variable
// override the run() method from Thread class
public void run()
{
while (true) // let the clock thread run for as long as the scoreboard application is running
{
timerdisplay -= initialTime ;
try
{ timerdisplay.sleep(1000)
}
catch (InterruptedException e)
{
}
if (running) // if your clock control variable is true
{
initialTime --;
// displayJTextField.setText(timeFormat//counter and update the timer display; change the seconds to a string in MM:SS format
}
}
}
}
pattyps36 0 Newbie Poster
mport java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JTextField;
public class scoreboardclock extends Thread
{
private int counter = 0;
private boolean running = false;
private JTextField timerDisplay = null;
public scoreboardclock(int seconds, JTextField timerDisplay)
{
this.counter = seconds;
this.timerDisplay = timerDisplay;
}
public int getCounter()
{
return counter;
}
public void toggle()
{
running = !running;
}
public void run()
{
while (counter > 0)
{
DecimalFormat df = new DecimalFormat("00");
try
{
sleep(1000);
}
catch (InterruptedException e)
{
}
if (running)
{
counter--;
timerDisplay.setText((counter/60) + ":" + df.format(counter%60));
}
}
}
}
hurly 0 Newbie Poster
I have the same assigment but the files given I already have.
I have been trying to get it to work together for some time now, I have been trying to make a new variable in the frame file but with no luck, should the timer variable look similar the the button ones?
Just not moving forward now so figured it wouldn't hurt to ask the quesiton.
Any guidance is greatly appreciated.
Hurly
pattyps36 0 Newbie Poster
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JTextField;
public class ScoreBoardTimer extends Thread
{
private int counter = 0;
private boolean running = false;
private JTextField timerDisplay = null;
public ScoreBoardTimer(int seconds, JTextField timerDisplay)
{
this.counter = seconds;
this.timerDisplay = timerDisplay;
}
public int getCounter()
{
return counter;
}
public void toggle()
{
running = !running;
}
public void run()
{
while (counter > 0)
{
DecimalFormat df = new DecimalFormat("00");
try
{
sleep(1000);
}
catch (InterruptedException e)
{
}
if (running)
{
counter--;
timerDisplay.setText((counter/60) + ":" + df.format(counter%60));
}
}
}
}
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.*;
import java.text.*;
import java.util.Timer;
import java.util.TimerTask;
public class ScoreboardFrame extends JFrame
{
JMenuItem menuFileExit = new JMenuItem();
JMenu menuFile = new JMenu();
JMenuBar menuBar1 = new JMenuBar();
BorderLayout borderLayout1 = new BorderLayout();
JPanel panelScoreboard = new JPanel();
GridBagLayout gridBagLayout1 = new GridBagLayout();
JTextField txtHomeScore = new JTextField();
JTextField txtTimerdisplay = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField txtGuests = new JTextField();
JTextField txtTime = new JTextField();
JPanel panelButtons = new JPanel();
JButton btnTimeControl = new JButton();
JButton btnHomeAdd = new JButton();
JButton btnHomeSub = new JButton();
JButton btnGuestsAdd = new JButton();
JButton btnGuestsSub = new JButton();
DecimalFormat df = new DecimalFormat("00");
int homeScore = 0;
int guestsScore = 0;
ScoreBoardTimer timer = new ScoreBoardTimer(600, txtTime);
public ScoreboardFrame()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.setJMenuBar(menuBar1);
this.getContentPane().setLayout(borderLayout1);
this.setSize(new Dimension(534, 300));
this.setTitle("Basketball Scoreboard");
menuFile.setText("File");
panelScoreboard.setBackground(new Color(99, 82, 227));
panelScoreboard.setLayout(gridBagLayout1);
txtHomeScore.setText("00");
txtHomeScore.setPreferredSize(new Dimension(100, 75));
txtHomeScore.setFont(new Font("SansSerif", 1, 60));
txtHomeScore.setForeground(Color.red);
txtHomeScore.setEditable(false);
jLabel1.setText("Home");
jLabel1.setFont(new Font("Dialog", 1, 20));
jLabel1.setForeground(Color.white);
jLabel2.setText("Guests");
jLabel2.setForeground(Color.white);
jLabel2.setFont(new Font("Dialog", 1, 20));
txtGuests.setText("00");
txtGuests.setCaretColor(Color.red);
txtGuests.setForeground(Color.red);
txtGuests.setEditable(false);
txtGuests.setPreferredSize(new Dimension(100, 75));
txtGuests.setFont(new Font("SansSerif", 1, 60));
txtTime.setText("10:00");
txtTime.setFont(new Font("SansSerif", 1, 60));
txtTime.setForeground(Color.red);
txtTime.setEditable(false);
btnTimeControl.setText("Start/Stop");
btnTimeControl.setForeground(Color.red);
btnTimeControl.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnTimeControl_actionPerformed(e);
}
});
btnHomeAdd.setText("Home +");
btnHomeAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnHomeAdd_actionPerformed(e);
}
});
btnHomeSub.setText("Home -");
btnHomeSub.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnHomeSub_actionPerformed(e);
}
});
btnGuestsAdd.setText("Guests +");
btnGuestsAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnGuestsAdd_actionPerformed(e);
}
});
btnGuestsSub.setText("Guests -");
btnGuestsSub.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnGuestsSub_actionPerformed(e);
}
});
menuFileExit.setText("Exit");
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fileExit_ActionPerformed(ae);
}
});
menuFile.add(menuFileExit);
menuBar1.add(menuFile);
panelScoreboard.add(txtHomeScore, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
panelScoreboard.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
panelScoreboard.add(jLabel2, new GridBagConstraints(4, 0, 1, 1, 1.0, 1.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
panelScoreboard.add(txtGuests, new GridBagConstraints(4, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
panelScoreboard.add(txtTime, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
this.getContentPane().add(panelScoreboard, BorderLayout.CENTER);
panelButtons.add(btnHomeAdd, null);
panelButtons.add(btnHomeSub, null);
panelButtons.add(btnTimeControl, null);
panelButtons.add(btnGuestsAdd, null);
panelButtons.add(btnGuestsSub, null);
this.getContentPane().add(panelButtons, BorderLayout.SOUTH);
timer.start();
}
void fileExit_ActionPerformed(ActionEvent e)
{
System.exit(0);
}
void btnTimeControl_actionPerformed(ActionEvent e)
{
timer.toggle();
}
void btnHomeAdd_actionPerformed(ActionEvent e)
{
homeScore++;
txtHomeScore.setText(df.format(homeScore));
}
void btnHomeSub_actionPerformed(ActionEvent e)
{
homeScore--;
txtHomeScore.setText(df.format(homeScore));
}
void btnGuestsAdd_actionPerformed(ActionEvent e)
{
guestsScore++;
txtGuests.setText(df.format(guestsScore));
}
void btnGuestsSub_actionPerformed(ActionEvent e)
{
guestsScore--;
txtGuests.setText(df.format(guestsScore));
}
}
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Toolkit;
public class ScoreboardApp
{
public ScoreboardApp()
{
Frame frame = new ScoreboardFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.setVisible(true);
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
new ScoreboardApp();
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.