- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我希望能够隐藏 JProgressBar 并让它所在的面板不改变大小。
目前,当我将进度条设置为可见时(通过 pb.setVisible(true)
),我得到如下布局:
然后当我将它设置为不可见时,我得到以下布局:
我想我在这里遗漏了一些简单的东西,我看不出它是什么。
我如何强制隐藏的 JProgressBar 仍然占据窗体中的不动产(尤其是垂直不动产)?
代码如下,物有所值。与代码的细节相比,我对我错过的概念更感兴趣。 :-)
package pbtest;
public class MyFrame extends javax.swing.JFrame {
public MyFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
pb = new javax.swing.JProgressBar();
bnVis = new javax.swing.JButton();
bnInvis = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
bnVis.setText("Visible");
bnVis.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bnVisActionPerformed(evt);
}
});
bnInvis.setText("Not Visible");
bnInvis.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bnInvisActionPerformed(evt);
}
});
jButton1.setText("Some Other Random Component");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(pb, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(45, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(bnVis)
.addGap(28, 28, 28)
.addComponent(bnInvis)
.addGap(47, 47, 47))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(30, 30, 30))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(20, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bnInvis)
.addComponent(bnVis))
.addGap(4, 4, 4)
.addComponent(pb, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(22, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(64, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void bnVisActionPerformed(java.awt.event.ActionEvent evt) {
pb.setVisible(true);
}
private void bnInvisActionPerformed(java.awt.event.ActionEvent evt) {
pb.setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bnInvis;
private javax.swing.JButton bnVis;
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JProgressBar pb;
// End of variables declaration
最佳答案
选项:将其放置在 JPanel 中,该 JPanel 使用 CardLayout 将 View 换入和换出。你可以用一个空的 JLabel 来交换它。 CardLayout - 使用 JPanel 将以显示所有交换组件所需的最大尺寸调整大小。然后在要显示 JProgressBar 的位置添加 CardLayout-using JPanel。
以您的代码为例(尽管我讨厌 GroupLayout):
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MyFrame extends javax.swing.JFrame {
private static final String PROGRESS_BAR = "progress bar";
private static final String EMPTY = "empty";
public MyFrame() {
initComponents();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
pb = new javax.swing.JProgressBar();
bnVis = new javax.swing.JButton();
bnInvis = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
//!!
cardLayout = new CardLayout();
cardHolderPanel = new JPanel(cardLayout);
cardHolderPanel.add(pb, PROGRESS_BAR);
cardHolderPanel.add(new JLabel(), EMPTY);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
bnVis.setText("Visible");
bnVis.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bnVisActionPerformed(evt);
}
});
bnInvis.setText("Not Visible");
bnInvis.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bnInvisActionPerformed(evt);
}
});
jButton1.setText("Some Other Random Component");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(jPanel1Layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(cardHolderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 217,
javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(45, Short.MAX_VALUE))
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout
.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(
jPanel1Layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup().addComponent(bnVis)
.addGap(28, 28, 28).addComponent(bnInvis)
.addGap(47, 47, 47))
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup().addComponent(jButton1)
.addGap(30, 30, 30)))));
jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout
.createSequentialGroup()
.addContainerGap(20, Short.MAX_VALUE)
.addGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bnInvis).addComponent(bnVis))
.addGap(4, 4, 4)
.addComponent(cardHolderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(22, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(64, Short.MAX_VALUE)));
pack();
}// </editor-fold>
private void bnVisActionPerformed(java.awt.event.ActionEvent evt) {
// pb.setVisible(true);
cardLayout.show(cardHolderPanel, PROGRESS_BAR);
}
private void bnInvisActionPerformed(java.awt.event.ActionEvent evt) {
// pb.setVisible(false); // !!
cardLayout.show(cardHolderPanel, EMPTY);;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);
}
});
}
private javax.swing.JButton bnInvis;
private javax.swing.JButton bnVis;
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private CardLayout cardLayout;
private JPanel cardHolderPanel;
private javax.swing.JProgressBar pb;
}
如果这是我的项目,我会考虑为此创建一个类,比如称为 SwappingComponent 的东西,它可能看起来像这样:
import java.awt.CardLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
public @SuppressWarnings("serial")
class SwappingComponent extends JPanel {
private static final String SHOW = "show";
private static final String HIDE = "hide";
JComponent component;
CardLayout cardLayout = new CardLayout();
public SwappingComponent(JComponent component) {
setLayout(cardLayout);
this.component = component;
add(component, SHOW);
add(new JLabel(), HIDE);
}
public JComponent getComponent() {
return component;
}
public void showComponent(boolean show) {
String key = show ? SHOW : HIDE;
cardLayout.show(this, key);
}
}
然后我可以像这样使用它:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.Border;
@SuppressWarnings("serial")
public class MyGui2 extends JPanel {
private JProgressBar progressBar = new JProgressBar(0, 100);
private SwappingComponent swappingComponent = new SwappingComponent(progressBar);
public MyGui2() {
progressBar.setStringPainted(true);
int delay = 100;
new Timer(delay, new ActionListener() {
private int value = 0;
@Override
public void actionPerformed(ActionEvent e) {
progressBar.setValue(value);
if (value >= 100) {
value = 0;
} else {
value += 2;
value = Math.min(value, 100);
}
}
}).start();
JPanel northPanel = new JPanel(new GridLayout(1, 0, 5, 5));
northPanel.add(new JButton(new ShowAction("Show")));
northPanel.add(new JButton(new HideAction("Hide")));
JPanel southPanel = new JPanel();
southPanel.add(new JButton("Some Component"));
Border outsideBorder = BorderFactory.createEmptyBorder(15, 15, 15, 15);
Border insideBorder = BorderFactory.createEtchedBorder();
Border border = BorderFactory.createCompoundBorder(outsideBorder, insideBorder);
setBorder(border);
setLayout(new BorderLayout(6, 6));
add(swappingComponent, BorderLayout.CENTER);
add(northPanel, BorderLayout.PAGE_START);
add(southPanel, BorderLayout.PAGE_END);
}
private class ShowAction extends AbstractAction {
public ShowAction(String name) {
super(name);
putValue(MNEMONIC_KEY, (int) name.charAt(0));
}
@Override
public void actionPerformed(ActionEvent e) {
swappingComponent.showComponent(true);
}
}
private class HideAction extends AbstractAction {
public HideAction(String name) {
super(name);
putValue(MNEMONIC_KEY, (int) name.charAt(0));
}
@Override
public void actionPerformed(ActionEvent e) {
swappingComponent.showComponent(false);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("MyGui2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyGui2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
关于java - JProgressBar 不可见时不占用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34518725/
我正在尝试将 2 个 JProgressBars 组合在一起,以便我可以表示有关单个事物的统计信息。 我的问题是如何将两个进度条组合或叠加才能起作用? 我想到的一个解决方案是将 2 个进度条放在一个
继续 Setting the colors of a JProgressBar text我希望能够根据进程状态在我的程序中设置 JProgressBar 的文本颜色,而不偏离系统外观。 来自 Sett
注意到这个链接后: http://www.newscientist.com/blogs/nstv/2010/12/best-videos-of-2010-progress-bar-illusion.h
我正在尝试在这个问题的投票答案中找到的代码:Download file using java apache commons? 是一个下载应用,稍微看一下,(我对JFrames和ActionEvents
我只是想在 JFrame 中使用 Windows L&F .现在在使用进度条时,默认颜色是绿色,类似于其他 windows 功能中使用的颜色,如复制文件等。在复制文件的情况下覆盖)。如何在我的进度条的
你好堆栈交换器, 我在 java Swing 中的进度条有问题。我认为我的困惑源于我对线程和 Swing 事件队列的理解不足(我不太了解 java 线程,以及 AWTEventQueue 上到底发生了
如何从另一个线程更新 JProgressBar.setValue(int)? 我的次要目标是尽可能少地上课。 这是我现在拥有的代码: // Part of the main class.... pp.
我正在尝试使用 JProgressBar 作为简单游戏的健康栏。我试图让它完整且可见以启动程序。这是我的 JProgressBar 代码: progressBar = new JProgres
我无法更新我的进度条...这是我的代码 Thread t=new Thread(new Runnable(){ public void run(){ int i
您好,我想知道是否可以扩展 JProgressBar 以使用 double 值来表示最小值最大值,而不是 int。谢谢。 最佳答案 如果您知道值的范围和精度,那么使用包装器就很容易做到。我已将这种方法
我正在尝试将 JProgressBar 添加到我的程序中,但它不会更新!该值仅在推理 100% 后才会更改。这是我的方法。 public void downloadImages(List images
我正在使用 JProgressBar 在我的框架上显示进度条。进度条设置为不确定模式,因为我不知道任务何时结束。显示的不是正常的进度条,而是奇怪的橙色波浪。 任务运行时波浪会持续移动。结束后,该值设置
我最近的项目遇到了一个严重的问题:我正在一个名为 writer(...) 的方法中进行长时间比较,大约需要 20 秒。该方法在 View 类中进行标记,我的 GUI 元素也在其中设置。当方法进行比较时
以下是将图像转换为字节数组的简单代码(本论坛已显示): File imgPath= new File(textFiled_Path.getText()); BufferedImage original
我已经尝试从库接收字节来设置我的 JProgressBar 更新很长一段时间,但不幸的是我没有得到我想要的结果。 问题是我不知道如何从lib接收字节以及如何在接收字节时更新JProgressBar所有
我想在我的代码中添加一个 JProgressBar,它计数到 5(以秒为单位)。之后,它将产生新的值,但我当前的问题是,我什至无法用我的 ActionListener 制作 ProgressBar,所
我在函数中使用 POI 来填充 Excel 文档的内容(需要 10 秒),当我调用函数时,我想使用 JProgressBar 来查看进度,但按钮和程序被阻止,我需要在其他线程中制作吗?我该怎么做?我的
我编写了一个非常简单的代码来在此处显示它,我有一个按钮应该显示 JDialog 来检查进度状态,我正在使用延迟调用来完成 EDT 并且我的循环不在 run 方法中,那么为什么我的栏没有更新呢?这是代码
我一直在尝试更改 jProgressBar 颜色,但一直是橙色 这是属性 运行 我想将颜色更改为绿色或其他颜色,但不知道如何实现 最佳答案 尝试这个教学示例。它将修改 JProgressBar 的 4
我在创 build 置为不确定的 JProgressBar 时遇到问题。以下代码是我对 JProgressBar 的实现,是从另一个类调用/构造的: public class Progress imp
我是一名优秀的程序员,十分优秀!