?? jprogressbardemo.java
字號:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JProgressBarDemo extends JFrame
{
private JProgressBar pronnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnngressBar; //進度條
private JButton startButton, stopButton; //啟動, 暫停, 重啟動按鈕
private Timer timer; //定時器
JPanel centerPanel,southPanel;
JLabel label;
public JProgressBarDemo()
{
super("java游戲加載中");
setSize(400, 400);
centerPanel=new JPanel();
southPanel=new JPanel();
label=new JLabel("漢偌塔游戲加載中",JLabel.CENTER);
label.setFont(new Font("TimesRoman",Font.BOLD,24));
label.setForeground(Color.orange);
//獲取內容面板
Container container = getContentPane();
//設置內容面板的布局管理器
//container.setLayout(new FlowLayout(FlowLayout.CENTER));
container.setLayout(new BorderLayout());
container.setBackground(Color.YELLOW);
//創建進度條
progressBar = new JProgressBar();
//設置最小值,最大值,初值
progressBar.setMinimum(0); //將進度條的最小值(存儲在進度條的數據模型中)設置為 0。
progressBar.setMaximum(100);//將進度條的最大值(存儲在進度條的數據模型中)設置為 100。
progressBar.setValue(0);// 將進度條的當前值(存儲在進度條的數據模型中)設置為 0
//顯示進度條進度文本
progressBar.setStringPainted(true);
//顯示進度條邊框
progressBar.setBorderPainted(true);
//設置進度條大小,背景色,前景色
progressBar.setPreferredSize(new Dimension(100,30));
progressBar.setBackground(Color.WHITE);
progressBar.setForeground(Color.GREEN);
//創建按鈕
startButton = new JButton("開始");
stopButton = new JButton("暫停");
//設置按鈕背景顏色
startButton.setBackground(Color.WHITE);
stopButton.setBackground(Color.WHITE);
//設置字體
startButton.setFont(new Font("Serif", Font.PLAIN, 14));
stopButton.setFont(new Font("Serif", Font.PLAIN, 14));
stopButton.setEnabled(false);
//注冊監聽器
TimerHandler handler = new TimerHandler();
startButton.addActionListener(handler);
stopButton.addActionListener(handler);
//設置面板
centerPanel.setLayout(new GridLayout(2,1,100,100));
southPanel.add(startButton);southPanel.add(stopButton);
centerPanel.add(progressBar);
centerPanel.add(southPanel);
container.add(label,BorderLayout.NORTH);
//container.add(progressBar,BorderLayout.CENTER);
container.add(centerPanel,BorderLayout.CENTER);
//創建定時器,時間間隔為50毫秒,設置監聽器
timer = new Timer(50, handler);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
JProgressBarDemo application = new JProgressBarDemo();
}
class TimerHandler implements ActionListener
{
//處理定時器事件
private int value = 0;
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == timer)
{
value = progressBar.getValue();//返回進度條的當前值,該值存儲在進度條的 BoundedRangeModel 中。
if( value < 100)
{
value++;
progressBar.setValue(value);
}
else
{
timer.stop();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
else if(event.getSource() == startButton)
{
if(progressBar.getValue() >= 100)
progressBar.setValue(0);
timer.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
else if(event.getActionCommand().equals("暫停"))
{
timer.stop();
stopButton.setText("重啟動");
}
else if(event.getActionCommand().equals("重啟動"))
{
timer.restart();
stopButton.setText("暫停");
}
}
} //TimerHandler類結束
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -