本文介绍了如何在其他框架中更改setIndeterminate jProgressbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个框架,框架A和框架B,在框架A中我添加了一个名称为buttonA的按钮以显示框架B和进度条(setIndeterminate(false)),在框架B中我添加了名称为buttonB的一个按钮,当我单击时buttonB,frameA中的进​​度栏​​.setindeterminate(true)

i have a two frame, frameA and frameB, in frameA i add one button with name buttonA to show frameB and progressbar (setIndeterminate(false)), in frameB i add one button with name buttonB , i want to when i click buttonB, progressbar in frameA.setindeterminate(true)

在框架A

frameB b;

public frameA() {
   initComponents();
   progressbar.setIndeterminate(false);
   b = new frameB();
}

public JProgressBar getProgressbar() {
   return progressbar;
}

private void buttonAActionPerformed(java.awt.event.ActionEvent evt) 
{                                       
    b.setVisible(true);
}

在框B中我在事件buttonB单击中使用了此代码

in frameBi use this code in event buttonB clicked

private void buttonBActionPerformed(java.awt.event.ActionEvent evt) { 
    frameA a= new frameA();
    a.getProgressbar().setIndeterminate(true);
}

但是没有用

推荐答案

此...

private void buttonBActionPerformed(java.awt.event.ActionEvent evt) { 
    frameA a= new frameA();
    a.getProgressbar().setIndeterminate(true);
}

将不起作用,您刚刚创建了另一个不可见的frameA实例.它与当前打开的框架没有关系.

Isn't going to work, you've just created another instance of frameA that's not visible. It has no relationship to the frame that is currently open.

有很多方法可以实现这一目标...

There are any number of ways you could achieve this...

您可以...

frameA的引用传递给frameB作为frameB的构造函数调用的一部分.然后,在您的actionPerformed方法中,您只需使用此引用来更改进度栏状态.

Pass a reference of frameA to frameB as part of the constructor call for frameB. Then in you actionPerformed method, you would simply use this reference to change the progress bar state.

但这会在frameAframeB之间建立紧密的耦合,这将大大减少frameB

But this would create a tight coupling between frameA and frameB which would greatly reduce the re-use of frameB

您可以...

提供一种使感兴趣的方可以将ActionListener附加到frameB的方法,该方法将在单击按钮时触发.

Provide a means by which an interested party could attach an ActionListener to frameB which would be triggered when the button is clicked.

这假设工作流程并将组件暴露于外部资源(通过ActionEvent#getSource),这可能使人们可以更改您的组件...

This assumes a work flow and exposes components to outside sources (via the ActionEvent#getSource), which could allow people to change your component...

您可能应该...

触发PropertyChanged事件.

这可能是我提出的所有选项中最简单,最安全的选择.以这种方式使用属性更改侦听器意味着您无需公开JProgressBarJButton或在两个框架之间建立紧密的链接.

This is probably the easiest and safest of all the options I've come up with. Using a property change listener in this manner means you don't need to expose the JProgressBar, the JButton or produce a tight link between the two frames.

Container中内置了属性更改支持,因此所有组件/控件都具有它.

Property change support is built in to Container so all components/controls have it.

例如,在构造PropertyChangeListener时,会将PropertyChangeListener附加到b.

For example, you would attach a PropertyChangeListener to b when you construct it.

b = new frameB();
b.addPropertyChangeListener(new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("progressState")) {
            progressbar.setIndeterminate((Boolean)evt.getNewValue());
        }
    }
});

添加bFrameactionPerformed方法,您可以简单地调用...

Add in bFrame's actionPerformed method, you would simple call...

firePropertyChange("progressState", false, true);

要设置不确定状态时(如果需要,可以交换boolean值以将其重置)

When you want to set the indeterminate state (you could swap the boolean values to reset it if you wanted to)

这篇关于如何在其他框架中更改setIndeterminate jProgressbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:15