[go: up one dir, main page]

0% found this document useful (0 votes)
13 views3 pages

JAVA Experiment No-8

Uploaded by

jayshree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

JAVA Experiment No-8

Uploaded by

jayshree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment No - 8

Design a basic AWT/Swing-based GUI application for a temperature


monitoring dashboard

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class TemperatureDashboard extends JFrame {

private JLabel temperatureLabel;


private JButton startButton;
private JButton stopButton;
private Timer timer; // Swing Timer to update temperature every second
private Random random = new Random();

public TemperatureDashboard() {
setTitle("Temperature Monitoring Dashboard");
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center window

// Setup layout
setLayout(new BorderLayout());

// Temperature display label


temperatureLabel = new JLabel("Temperature: -- °C", SwingConstants.CENTER);
temperatureLabel.setFont(new Font("Arial", Font.BOLD, 30));
temperatureLabel.setOpaque(true);
temperatureLabel.setBackground(Color.LIGHT_GRAY);
temperatureLabel.setPreferredSize(new Dimension(400, 100));
add(temperatureLabel, BorderLayout.CENTER);

// Panel for buttons


JPanel buttonPanel = new JPanel();
startButton = new JButton("Start");
stopButton = new JButton("Stop");
stopButton.setEnabled(false); // Initially disabled

buttonPanel.add(startButton);
buttonPanel.add(stopButton);
add(buttonPanel, BorderLayout.SOUTH);

// Timer setup (updates temperature every 1000 ms)


timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double temp = 15 + random.nextDouble() * 20; // Simulate 15°C to 35°C
temperatureLabel.setText(String.format("Temperature: %.2f °C", temp));
updateTemperatureColor(temp);
}
});

// Button actions
startButton.addActionListener(e -> {
timer.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
});

stopButton.addActionListener(e -> {
timer.stop();
startButton.setEnabled(true);
stopButton.setEnabled(false);
temperatureLabel.setText("Temperature: -- °C");
temperatureLabel.setBackground(Color.LIGHT_GRAY);
});
}

private void updateTemperatureColor(double temp) {


// Change background color based on temperature range
if (temp < 20) {
temperatureLabel.setBackground(Color.CYAN);
} else if (temp < 30) {
temperatureLabel.setBackground(Color.GREEN);
} else {
temperatureLabel.setBackground(Color.RED);
}
}

public static void main(String[] args) {


// Run GUI in Event Dispatch Thread
SwingUtilities.invokeLater(() -> {
TemperatureDashboard dashboard = new TemperatureDashboard();
dashboard.setVisible(true);
});
}
}

What you'll see:

●​ A window with a big temperature label.​

●​ Press Start to begin simulated temperature updates every second.​

●​ Background color changes:​

○​ Cyan for cool (<20°C)​

○​ Green for moderate (20-30°C)​

○​ Red for hot (>30°C)​

●​ Press Stop to pause and reset.

You might also like