Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off. Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like.
// Demonstrates mnemonics and tool tips.
//********************************************************************
import javax.swing.*;
import java.awt.*;
public class LightBulb
{
//-----------------------------------------------------------------
// Sets up a frame that displays a light bulb image
that can be
// turned on and off.
//-----------------------------------------------------------------
public static void main(String[] args)
{
JFrame frame = new JFrame("Light
Bulb");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LightBulbPanel bulb = new
LightBulbPanel();
LightBulbControls controls = new
LightBulbControls(bulb);
JPanel panel = new
JPanel();
panel.setBackground(Color.black);
panel.setLayout(new BoxLayout(panel,
BoxLayout.Y_AXIS));
panel.add(Box.createRigidArea(new
Dimension(0, 20)));
panel.add(bulb);
panel.add(Box.createRigidArea(new
Dimension(0, 10)));
panel.add(controls);
panel.add(Box.createRigidArea(new
Dimension(0, 10)));
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
// Represents the control panel for the LightBulb program.
//********************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LightBulbControls extends
JPanel
{
private LightBulbPanel bulb;
private JButton onButton, offButton;
//-----------------------------------------------------------------
// Sets up the lightbulb control panel.
//-----------------------------------------------------------------
public LightBulbControls(LightBulbPanel
bulbPanel)
{
bulb = bulbPanel;
onButton = new
JButton("On");
onButton.setEnabled(false);
onButton.setMnemonic('n');
onButton.setToolTipText("Turn it
on!");
onButton.addActionListener(new
OnListener());
offButton = new
JButton("Off");
offButton.setEnabled(true);
offButton.setMnemonic('f');
offButton.setToolTipText("Turn it
off!");
offButton.addActionListener(new
OffListener());
setBackground(Color.black);
add(onButton);
add(offButton);
}
//*****************************************************************
// Represents the listener for the On button.
//*****************************************************************
private class OnListener implements
ActionListener
{
//--------------------------------------------------------------
// Turns the bulb on and repaints
the bulb panel.
//--------------------------------------------------------------
public void
actionPerformed(ActionEvent event)
{
bulb.setOn(true);
onButton.setEnabled(false);
offButton.setEnabled(true);
bulb.repaint();
}
}
//*****************************************************************
// Represents the listener for the Off button.
//*****************************************************************
private class OffListener implements
ActionListener
{
//--------------------------------------------------------------
// Turns the bulb off and repaints
the bulb panel.
//--------------------------------------------------------------
public void
actionPerformed(ActionEvent event)
{
bulb.setOn(false);
onButton.setEnabled(true);
offButton.setEnabled(false);
bulb.repaint();
}
}
}
// Represents the image for the LightBulb program.
//********************************************************************
import javax.swing.*;
import java.awt.*;
public class LightBulbPanel extends
JPanel
{
private boolean on;
private ImageIcon lightOn, lightOff;
private JLabel imageLabel;
//-----------------------------------------------------------------
// Constructor: Sets up the images and the initial
state.
//-----------------------------------------------------------------
public LightBulbPanel()
{
lightOn = new
ImageIcon("lightBulbOn.gif");
lightOff = new
ImageIcon("lightBulbOff.gif");
setBackground(Color.black);
on = true;
imageLabel = new
JLabel(lightOff);
add(imageLabel);
}
//-----------------------------------------------------------------
// Paints the panel using the appropriate image.
//-----------------------------------------------------------------
public void paintComponent(Graphics page)
{
super.paintComponent(page);
if (on)
imageLabel.setIcon(lightOn);
else
imageLabel.setIcon(lightOff);
}
//-----------------------------------------------------------------
// Sets the status of the light bulb.
//-----------------------------------------------------------------
public void setOn(boolean lightBulbOn)
{
on = lightBulbOn;
}
}
//LightBulbControls.java
//Represents the control panel for the LightBulb program.
//********************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LightBulbControls extends JPanel {
private LightBulbPanel bulb;
private JButton onButton, offButton;
private Timer timer;
private int seconds;
//
-----------------------------------------------------------------
// Sets up the lightbulb control panel.
//
-----------------------------------------------------------------
public LightBulbControls(LightBulbPanel bulbPanel)
{
bulb = bulbPanel;
onButton = new
JButton("On");
onButton.setEnabled(false);
onButton.setMnemonic('n');
onButton.setToolTipText("Turn it
on!");
onButton.addActionListener(new
OnListener());
offButton = new
JButton("Off");
offButton.setEnabled(true);
offButton.setMnemonic('f');
offButton.setToolTipText("Turn it
off!");
offButton.addActionListener(new
OffListener());
seconds = 0;
timer = new Timer(1000, new
ActionListener() {
@Override
public void
actionPerformed(ActionEvent e) {
if (seconds < 10) {
bulb.setSecond("" +
seconds);
seconds += 1;
} else
offButton.doClick();
}
});
setBackground(Color.black);
add(onButton);
add(offButton);
timer.start();
}
//
*****************************************************************
// Represents the listener for the On button.
//
*****************************************************************
private class OnListener implements ActionListener
{
//
--------------------------------------------------------------
// Turns the bulb on and repaints
the bulb panel.
//
--------------------------------------------------------------
public void
actionPerformed(ActionEvent event) {
bulb.setOn(true);
onButton.setEnabled(false);
offButton.setEnabled(true);
bulb.repaint();
timer.start();
}
}
//
*****************************************************************
// Represents the listener for the Off button.
//
*****************************************************************
private class OffListener implements ActionListener
{
//
--------------------------------------------------------------
// Turns the bulb off and repaints
the bulb panel.
//
--------------------------------------------------------------
public void
actionPerformed(ActionEvent event) {
bulb.setOn(false);
onButton.setEnabled(true);
offButton.setEnabled(false);
bulb.repaint();
timer.stop();
bulb.setSecond("
");
seconds =
0;
}
}
}
//LightBulbPanel.java
//Represents the image for the LightBulb program.
//********************************************************************
import javax.swing.*;
import java.awt.*;
public class LightBulbPanel extends JPanel {
private boolean on;
private ImageIcon lightOn, lightOff;
private JLabel imageLabel;
private JLabel secondsLabel;
//
-----------------------------------------------------------------
// Constructor: Sets up the images and the initial
state.
//
-----------------------------------------------------------------
public LightBulbPanel() {
lightOn = new
ImageIcon("lightBulbOn.gif");
lightOff = new
ImageIcon("lightBulbOff.gif");
setBackground(Color.black);
on = true;
imageLabel = new
JLabel(lightOff);
add(imageLabel);
secondsLabel = new JLabel("
");
secondsLabel.setForeground(Color.WHITE);
add(secondsLabel);
}
//
-----------------------------------------------------------------
// Paints the panel using the appropriate image.
//
-----------------------------------------------------------------
public void paintComponent(Graphics page) {
super.paintComponent(page);
if (on)
imageLabel.setIcon(lightOn);
else
imageLabel.setIcon(lightOff);
}
//
-----------------------------------------------------------------
// Sets the status of the light bulb.
//
-----------------------------------------------------------------
public void setOn(boolean lightBulbOn) {
on = lightBulbOn;
}
//
-----------------------------------------------------------------
// Sets the number of seconds the bulb is on
//
-----------------------------------------------------------------
public void setSecond(String seconds) {
secondsLabel.setText(seconds);
}
}
//LightBulb.java
import javax.swing.*;
import java.awt.*;
public class LightBulb {
//
-----------------------------------------------------------------
// Sets up a frame that displays a light bulb image
that can be
// turned on and off.
//
-----------------------------------------------------------------
public static void main(String[] args) {
JFrame frame = new JFrame("Light
Bulb");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LightBulbPanel bulb = new
LightBulbPanel();
LightBulbControls controls = new
LightBulbControls(bulb);
JPanel panel = new
JPanel();
panel.setBackground(Color.black);
panel.setLayout(new
BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(Box.createRigidArea(new
Dimension(0, 20)));
panel.add(bulb);
panel.add(Box.createRigidArea(new
Dimension(0, 10)));
panel.add(controls);
panel.add(Box.createRigidArea(new
Dimension(0, 10)));
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
SAMPLE OUTPUT:
Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10...
Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with the files available here. public class app6 { public static void main(String args[]) { myJFrame6 mjf = new myJFrame6(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJFrame6 extends JFrame { myJPanel6 p6; public myJFrame6 () { super ("My First Frame"); //------------------------------------------------------ // Create components: Jpanel, JLabel and JTextField p6 = new myJPanel6(); //------------------------------------------------------...
tart from the following code, and add Action Listener to make it functional to do the temperature conversion in the direction of the arrow that is clicked. . (Need about 10 lines) Note: import javax.swing.*; import java.awt.GridLayout; import java.awt.event.*; import java.text.DecimalFormat; public class temperatureConverter extends JFrame { public static void main(String[] args) { JFrame frame = new temperatureConverter(); frame.setTitle("Temp"); frame.setSize(200, 100); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public temperatureConverter() { JLabel lblC = new...
Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file. Program...
Please fix and test my code so that all the buttons and function are working in the Sudoku. CODE: SudokuLayout.java: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class SudokuLayout extends JFrame{ JTextArea txtArea;//for output JPanel gridPanel,butPanel;//panels for sudoku bord and buttons JButton hint,reset,solve,newPuzzel;//declare buttons JComboBox difficultyBox; SudokuLayout() { setName("Sudoku Bord");//set name of frame // setTitle("Sudoku Bord");//set...
This is the java GUI program. I want to add an icon to a button, but it shows nothing. Can you fix it for me please? import java.awt.BorderLayout; import java.awt.Container; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; class Icon extends JFrame{ public static void main(String args[]){ Icon frame = new Icon("title"); frame.setVisible(true); } Icon(String title){ setTitle(title); setBounds(100, 100, 300, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); ImageIcon icon1 = new ImageIcon("https://static.thenounproject.com/png/610387-200.png"); JButton button1 = new JButton(icon1); p.add(button1); Container contentPane...
I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 { public TextArea courseInput; public Label textAreaLabel; public JButton addData; public Dialog confirmDialog; HashMap<Integer, ArrayList<String>> students; public Fall_2017(){ courseInput = new TextArea(20, 40); textAreaLabel = new Label("Student's data:"); addData = new JButton("Add...
In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...
I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...
With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...
Debug this java and post the corrected code. /* Creates a simple JPanel with a single "quit" JButton * that ends the program when clicked. * There are 3 errors, one won't prevent compile, you have to read comments. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class QuitIt extends JFrame { public QuitIt() { startIt(); //Create everything and start the listener } public final void startIt() { //Create the JPanel JPanel myPanel =...