java JProgressBar.setValue
时间: 2024-03-24 22:24:58 浏览: 129
The JProgressBar.setValue() method sets the current value of the progress bar. The method takes an integer value as its parameter and updates the progress bar to reflect that value.
For example, if you have a JProgressBar object called progressBar and you want to set its value to 50%, you can use the following code:
progressBar.setValue(50);
This will update the progress bar to show that it is 50% complete. Note that the maximum value of the progress bar needs to be set beforehand, using the JProgressBar.setMaximum() method.
相关问题
import javax.swing.*;import java.awt.*;import java.util.Random;public class TicketSeller { private JFrame mainFrame; private JPanel panel; private JProgressBar progressBar1; private JProgressBar progressBar2; private JLabel label1; private JLabel label2; private JButton startButton; private int totalTickets = 108; private int ticketSold1 = 0; private int ticketSold2 = 0; public TicketSeller() { prepareGUI(); addComponents(); } private void prepareGUI() { mainFrame = new JFrame("Ticket Seller"); mainFrame.setSize(400, 200); mainFrame.setLayout(new GridLayout(2, 1)); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void addComponents() { panel = new JPanel(new GridLayout(2, 2)); label1 = new JLabel("LYH_1_108_1"); progressBar1 = new JProgressBar(0, totalTickets); progressBar1.setValue(0); panel.add(label1); panel.add(progressBar1); label2 = new JLabel("LYH_1_108_2"); progressBar2 = new JProgressBar(0, totalTickets); progressBar2.setValue(0); panel.add(label2); panel.add(progressBar2); mainFrame.add(panel); startButton = new JButton("Start"); startButton.addActionListener(e -> { Thread thread1 = new Thread(() -> sellTickets(1)); Thread thread2 = new Thread(() -> sellTickets(2)); thread1.start(); thread2.start(); }); mainFrame.add(startButton); mainFrame.setVisible(true); } private void sellTickets(int windowNum) { while (ticketSold1 + ticketSold2 < totalTickets) { int ticketSold = new Random().nextInt(5) + 1; if (windowNum == 1) { if (ticketSold1 + ticketSold <= totalTickets) { ticketSold1 += ticketSold; progressBar1.setValue(ticketSold1); } } else { if (ticketSold2 + ticketSold <= totalTickets) { ticketSold2 += ticketSold; progressBar2.setValue(ticketSold2); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { new TicketSeller(); }}
这段代码是一个简单的模拟售票程序,它创建了一个窗口,包含两个进度条和一个“开始”按钮。总共有 108 张票,两个窗口分别出售随机数量的票,直到所有票都售出为止。
要显示已售票的份数,可以在程序中添加一个标签或文本框,每次销售后更新它的文本。例如,在 `sellTickets()` 方法中,可以添加以下代码:
```java
if (windowNum == 1) {
if (ticketSold1 + ticketSold <= totalTickets) {
ticketSold1 += ticketSold;
progressBar1.setValue(ticketSold1);
label1.setText("LYH_1_108_1: " + ticketSold1 + " sold");
}
} else {
if (ticketSold2 + ticketSold <= totalTickets) {
ticketSold2 += ticketSold;
progressBar2.setValue(ticketSold2);
label2.setText("LYH_1_108_2: " + ticketSold2 + " sold");
}
}
```
这将在售票时更新标签的文本,以显示已售票的份数。
Java Swing 实现loading...
Java Swing 中实现 "loading..." 动画通常是为了向用户显示某个操作正在进行,比如数据加载或者界面初始化。你可以通过以下步骤来创建一个简单的 Swing 加载指示器:
1. **创建 JProgressBar**:`JProgressBar` 是 Swing 提供的一个组件,用于显示进度条。你可以创建一个新的 `JProgressBar` 对象。
```java
JProgressBar progressBar = new JProgressBar(0, 100); // 设置最大进度为100
```
2. **设置初始状态**:将进度条设置为0,表示开始加载。
```java
progressBar.setValue(0);
```
3. **添加到容器**:将进度条添加到窗口的适当位置,如 `JFrame` 或 `JDialog` 的中心。
```java
frame.getContentPane().add(progressBar, BorderLayout.CENTER);
```
4. **动态更新进度**:在需要的地方更新进度条的值,例如每次加载一定量的数据时增加进度。
```java
public void loadProgress(int increment) {
int currentValue = progressBar.getValue();
progressBar.setValue(currentValue + increment);
if (currentValue < 100) {
SwingUtilities.invokeLater(() -> loadProgress(increment)); // 使用SwingUtils.invokeLater避免同步阻塞UI
}
}
```
5. **隐藏/显示进度条**:当加载完成时,可以调用 `setVisible(false)` 来隐藏进度条。
完整的例子:
```java
import javax.swing.*;
import java.awt.*;
public class LoadingExample extends JFrame {
private JProgressBar progressBar;
public LoadingExample() {
progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true); // 显示文本
JButton button = new JButton("Start Load");
button.addActionListener(e -> startLoad());
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(button);
panel.add(progressBar);
setContentPane(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
}
private void startLoad() {
progressBar.setVisible(true);
loadProgress(10); // 示例:每10次更新进度条
// 在这里处理实际的加载操作...
progressBar.setValue(100); // 加载完成后设为100%
progressBar.setVisible(false);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
LoadingExample example = new LoadingExample();
example.setVisible(true);
});
}
private void loadProgress(int increment) {
SwingUtilities.invokeLater(() -> {
int currentValue = progressBar.getValue();
progressBar.setValue(currentValue + increment);
if (currentValue < 100) {
loadProgress(increment);
}
});
}
}
```
阅读全文