使用佈局管理器,你的優勢。例如,JFrame的contentPane使用BorderLayout。如果您將JProgressBar添加到此BorderLayout.PAGE_END或BorderLayout.PAGE_END位置,它將在其容器調整大小時自動調整大小。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SimpleFrame {
private static void createAndShowGUI() {
final JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setBorder(BorderFactory.createTitledBorder("My Progress"));
new Timer(200, new ActionListener() {
int value = 0;
@Override
public void actionPerformed(ActionEvent e) {
value += 10;
if (value > 100) {
value = 0;
}
progressBar.setValue(value);
}
}).start();
JFrame frame = new JFrame("SimpleFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(progressBar, BorderLayout.PAGE_START);
frame.add(Box.createRigidArea(new Dimension(400, 300)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
import java.awt.*;
import javax.swing.*;
public class MinimalExampleJProgresbar2 {
public static void main(String[] args) {
JFrame frame = new JFrame("SimpleFrame");
JPanel wrapperPanel = new JPanel(new BorderLayout());
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JProgressBar progressBar = new JProgressBar();
wrapperPanel.add(progressBar, BorderLayout.PAGE_START);
wrapperPanel.add(jPanel, BorderLayout.CENTER);
frame.add(wrapperPanel, BorderLayout.PAGE_START);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
progressBar.setPreferredSize(new Dimension(frame.getWidth() - 100, 50));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MinimalExampleJProgresbar2 {
public static void main(String[] args) {
JFrame frame = new JFrame("SimpleFrame");
JPanel wrapperPanel = new JPanel(new BorderLayout());
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
final JProgressBar progressBar = new JProgressBar();
new Timer(200, new ActionListener() {
int value = 0;
@Override
public void actionPerformed(ActionEvent e) {
value += 10;
if (value > 100) {
value = 0;
}
progressBar.setValue(value);
}
}).start();
JPanel boxUser = new JPanel();
boxUser.setLayout(new BoxLayout(boxUser, BoxLayout.LINE_AXIS));
boxUser.add(Box.createHorizontalStrut(50));
boxUser.add(progressBar);
boxUser.add(Box.createHorizontalStrut(50));
wrapperPanel.add(boxUser, BorderLayout.PAGE_START);
wrapperPanel.add(jPanel, BorderLayout.CENTER);
frame.add(wrapperPanel, BorderLayout.PAGE_START);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
tnx男人!它解決了 –
爲什麼這是downvoted? –
@KoenVanLooveren:我沒有對這個答案投下贊成票,但是當更好的解決方案存在時,它更像是一團糟。爲了最好的解決方案,再次顯示你的代碼,你的[mcve]。 –