gpt4 book ai didi

java - 如果 JFrame 调整大小,则调整 JProgressBar 的大小

转载 作者:搜寻专家 更新时间:2023-11-01 02:41:02 25 4
gpt4 key购买 nike

我想在调整 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);
}
}

最佳答案

使用布局管理器对您有利。例如,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);
}
}

关于java - 如果 JFrame 调整大小,则调整 JProgressBar 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33169268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com