
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...
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...
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();
//------------------------------------------------------...
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...
Add appropriate descriptive comments to each line of code in the project explaining why the code is in the application. public class FutureValueFrame extends JFrame { private JTextField investmentField; private JTextField interestRateField; private JComboBox yearsComboBox; private JList futureValueList; private DefaultListModel futureValueModel; public FutureValueFrame() { initComponents(); } private void initComponents() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { System.out.println(e); } setTitle("Future Value Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationByPlatform(true); investmentField = new JTextField(); interestRateField = new JTextField();...
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************************************...
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...
Why when executed my frame does not contain what i have added to it? Here is my code import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JPanel; @SuppressWarnings("serial") public class AddressBook1 extends JFrame implements ActionListener { /** * */ JLabel name = new JLabel("Name: "); JLabel address = new JLabel("Address: "); JLabel phone = new JLabel("Phone: "); JLabel email = new JLabel("Email: ");...
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...
Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box that displays the temporary password should be read-only. The temporary password consists of the user’s first name, an asterisk (*), and the user’s birth year. If the user enters data in the first three fields, display a temporary password in the appropriate text field and a welcome message in the label below the text fields. If the user does not enter data, clear the...