这是我的java代码的一部分,在这段代码中,我希望进度条应始终保留在标签下(标签后面),当我启动程序时,进度条位于标签下,但进度条当我点击按钮并开始进度时,标签上方/上方。那么请告诉我如何将标签始终保持在进度条上???
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class Test {
JFrame frame = new JFrame();
JLabel label = new JLabel();
JProgressBar bar = new JProgressBar(JProgressBar.VERTICAL,0,100);
JButton button = new JButton();
public static void main(String arg[]) {
new Test();
}
public Test() {
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setLayout(null);
label.setOpaque(true);
label.setBackground(Color.BLACK);
label.setBounds(100,100,100,50);
bar.setBounds(140,25,20,200);
button.setBounds(220,100,50,50);
button.addActionListener(new Progress());
frame.add(label);
frame.add(bar);
frame.add(button);
frame.setVisible(true);
}
class Progress extends SwingWorker<Void, Void> implements ActionListener {
public Void doInBackground() {
for(int i=0; i<101; i++) {
bar.setValue(i);
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
public void actionPerformed(ActionEvent e) {
this.execute();
}
}
}
答案 0 :(得分:5)
当JProgressBar
已经提供显示String
值的方法时,我不确定你为什么要这么做...
有关详细信息,请参阅JProgressBar#setStringPainted
...
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ProgressBarTest {
public static void main(String[] args) {
new ProgressBarTest();
}
public ProgressBarTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
JProgressBar pb = new JProgressBar();
pb.setValue(0);
pb.setStringPainted(true);
pb.setString("Look ma, no hands");
frame.add(pb);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
SwingWorker worker = new SwingWorker() {
@Override
protected Object doInBackground() throws Exception {
for (int index = 0; index < 1000; index++) {
int progress = (int)Math.round((index / 1000f) * 100);
setProgress(progress);
Thread.sleep(10);
}
return null;
}
};
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
int value = (int) evt.getNewValue();
System.out.println(value);
pb.setValue(value);
}
}
});
worker.execute();
}
});
}
}
<强>更新强>
现在,如果您“真的”想要在进度条上添加标签,可以使用一些技巧,例如......
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ProgressBarTest {
public static void main(String[] args) {
new ProgressBarTest();
}
public ProgressBarTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JLabel label = new JLabel("I feel the need for speed");
JProgressBar pb = new JProgressBar();
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.ipady = 20;
frame.add(pb, gbc);
gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5, 0, 5, 0);
frame.add(label, gbc);
frame.add(pb, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
SwingWorker worker = new SwingWorker() {
@Override
protected Object doInBackground() throws Exception {
for (int index = 0; index < 1000; index++) {
int progress = (int) Math.round((index / 1000f) * 100);
setProgress(progress);
Thread.sleep(10);
}
return null;
}
};
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
int value = (int) evt.getNewValue();
System.out.println(value);
pb.setValue(value);
}
}
});
worker.execute();
}
});
}
}
基本上,这会将标签和进度条放在GridBagLayout
内的同一位置......
答案 1 :(得分:0)
根据@MadProgrammer给我的帖子和想法
"You can use:
Initialising:
progressBar.setStringPainted(true);
Updating:
progressBar.setValue(newValue);
progressBar.setString(newValue + "%");"
这个答案的来源:
How to add text on jprogressbar?
我对这个问题的回答是
代码:
JFrame frame = new JFrame();
JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
JButton button = new JButton("Button");
public static void main(String arg[]) {
new Test();
}
public Test() {
bar.setStringPainted(true);
bar.setString("I am a JProgressBar and ready to go here");
button.addActionListener(new Progress());
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 2;
c.gridx = 2;
c.gridy = 2;
pane.add(bar, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 3;
pane.add(button, c);
frame.add(pane);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class Progress extends SwingWorker<Void, Void> implements ActionListener {
@Override
public Void doInBackground() {
for (int i = 0; i < 101; i++) {
bar.setValue(i);
bar.setString(Integer.toString(i) + "%");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
public void actionPerformed(ActionEvent e) {
this.execute();
}
}
}
输出: