Many GUI applications have meters that indicate the progress made so far towards completing an activity as a percentage of the anticipated total activity. Write a program to display a progress meter as follows. The meter consists of a window with a column of 11 labels. The first 10 labels indicate progress steps of 10% and are initially red. The bottom label shows the amount of activity remaining as a textual percentage, and is initially set to “0%”. Then create functionality to simulate steps of 10% progress. For this the user repeatedly presses return on the console inside Eclipse (use a Scanner object for this). Every time, the top-most red label is changed to green and the progress label value is incremented by 10 until that value is 100%. Make sure that no errors occur if the user tries to go past 100%.
时间: 2024-02-05 08:03:12 浏览: 77
Sure, here's a sample Java program that implements the progress meter as described:
```java
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.Scanner;
public class ProgressMeter {
private static final int NUM_STEPS = 10;
private static final String TITLE = "Progress Meter";
private static final int WIDTH = 200;
private static final int HEIGHT = 300;
private static final Color RED = Color.RED;
private static final Color GREEN = Color.GREEN;
private static final String REMAINING_LABEL = "Remaining: ";
private JFrame frame;
private JLabel[] stepLabels;
private JLabel remainingLabel;
private int currentStep;
public ProgressMeter() {
frame = new JFrame(TITLE);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(NUM_STEPS + 1, 1));
stepLabels = new JLabel[NUM_STEPS];
for (int i = 0; i < NUM_STEPS; i++) {
stepLabels[i] = new JLabel(i*10 + "%", JLabel.CENTER);
stepLabels[i].setOpaque(true);
stepLabels[i].setBackground(RED);
frame.add(stepLabels[i]);
}
remainingLabel = new JLabel(REMAINING_LABEL + "0%", JLabel.CENTER);
frame.add(remainingLabel);
currentStep = 0;
}
public void setVisible(boolean visible) {
frame.setVisible(visible);
}
public void simulateProgress() {
Scanner scanner = new Scanner(System.in);
while (currentStep < NUM_STEPS) {
String input = scanner.nextLine();
if (input.equals("")) {
currentStep++;
stepLabels[currentStep - 1].setBackground(GREEN);
remainingLabel.setText(REMAINING_LABEL + currentStep*10 + "%");
}
if (currentStep == NUM_STEPS) {
System.out.println("Progress completed.");
}
}
}
public static void main(String[] args) {
ProgressMeter pm = new ProgressMeter();
pm.setVisible(true);
pm.simulateProgress();
}
}
```
The program creates a `JFrame` with a `GridLayout` containing 11 `JLabel` components, where the first 10 labels are the progress steps of 10% and the bottom label shows the remaining percentage. The labels are initialized to red, and as the user presses the enter key, the top-most red label is changed to green and the remaining percentage label is updated accordingly. The program uses a `Scanner` object to read user input from the console inside Eclipse. The program terminates when the progress reaches 100%.
阅读全文
相关推荐
















