【发布时间】:2016-05-29 08:50:56
【问题描述】:
我想知道单击按钮时如何为进度条设置循环。 我已经用循环尝试过了,但这是浪费时间。我在下面附上了我想要的图片。There is button in the North of the form and there is Progress Bar in the south
【问题讨论】:
标签: java swing components progress-bar
我想知道单击按钮时如何为进度条设置循环。 我已经用循环尝试过了,但这是浪费时间。我在下面附上了我想要的图片。There is button in the North of the form and there is Progress Bar in the south
【问题讨论】:
标签: java swing components progress-bar
这是一个简单的进度条示例,当您将鼠标悬停在按钮上时(根据您的图片),当鼠标悬停在按钮上时,您可以在线程中执行背景操作:-
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
public class GUI extends JFrame
{
boolean isMouseOver = false;
int count = 0;
public GUI()
{
this.setTitle("Progress Bar");
this.setBounds(100, 100, 407, 119);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
JProgressBar progressBar = new JProgressBar();
progressBar.setBounds(10, 45, 371, 22);
this.getContentPane().add(progressBar);
JButton btnMousOver = new JButton("Mouse Over");
btnMousOver.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent evt)
{
isMouseOver = true;
btnMousOver.setEnabled(false);
Thread go = new Thread()
{
public void run()
{
while (isMouseOver && count < 101)
{
count = count + 2;
progressBar.setValue(count);
// do some stuffs here
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
};
go.start();
}
public void mouseExited(MouseEvent evt)
{
isMouseOver = false;
btnMousOver.setEnabled(true);
}
public void mousePressed(MouseEvent evt)
{
JOptionPane.showMessageDialog(null, "Mouse CLicked");
}
public void mouseReleased(MouseEvent evt)
{
JOptionPane.showMessageDialog(null, "Mouse released");
}
});
btnMousOver.setBounds(142, 11, 108, 23);
this.getContentPane().add(btnMousOver);
this.getContentPane().add(progressBar);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args)
{
new GUI();
}
}
【讨论】: