我希望在调整JFrame大小时调整JProgresbar的大小
我的JProgresBar的大小计算如下:
progressBar.setPreferredSize(new Dimension(getWidth() - 100, 50));
但是当我调整JFrame的大小时,进度条的大小将始终保持不变。
更新1
这是我的代码:
public class MinimalExampleJProgresbar {
public static void main(String[] args) {
JFrame frame = new JFrame("SimpleFrame");
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JProgressBar progressBar = new JProgressBar();
jPanel.add(progressBar);
frame.add(jPanel, 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);
}
}
答案 0 :(得分:3)
使用布局管理器。例如,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);
}
}
答案 1 :(得分:1)
将ComponentAdapter添加到Frame
myFrame.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ev) {
progressBar.setPreferredSize(new Dimension(getWidth() - 100, 50));
revalidate();
repaint();
}
});