[英]how to change setIndeterminate jProgressbar in other frame
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) 我有一个两帧,frameA和frameB,在frameA我添加一个名称buttonA的按钮来显示frameB和进度条(setIndeterminate(false)),在frameB中我添加一个名为buttonB的按钮,我想当我点击buttonB,进度条在frameA.setindeterminate(true)
in frameA 在frameA中
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);
}
in frameB i use this code in event buttonB clicked 在frameB中,我在单击事件按钮B时使用此代码
private void buttonBActionPerformed(java.awt.event.ActionEvent evt) {
frameA a= new frameA();
a.getProgressbar().setIndeterminate(true);
}
but it doesnt worked 但它没有用
This... 这个...
private void buttonBActionPerformed(java.awt.event.ActionEvent evt) {
frameA a= new frameA();
a.getProgressbar().setIndeterminate(true);
}
Isn't going to work, you've just created another instance of frameA
that's not visible. 不会起作用,你刚刚创建了另一个不可见的
frameA
实例。 It has no relationship to the frame that is currently open. 它与当前打开的框架无关。
There are any number of ways you could achieve this... 你可以通过多种方式实现这一目标......
You could... 你可以...
Pass a reference of frameA
to frameB
as part of the constructor call for frameB
. 将
frameB
的引用frameA
给frameB
作为frameB
的构造函数调用的frameB
。 Then in you actionPerformed
method, you would simply use this reference to change the progress bar state. 然后在
actionPerformed
方法中,您只需使用此引用来更改进度条状态。
But this would create a tight coupling between frameA
and frameB
which would greatly reduce the re-use of frameB
但是,这将创造之间的紧密耦合
frameA
和frameB
这将大大降低再利用的frameB
You could... 你可以...
Provide a means by which an interested party could attach an ActionListener
to frameB
which would be triggered when the button is clicked. 提供一种方法,利益相关方可以将
ActionListener
附加到frameB
,这将在单击按钮时触发。
This assumes a work flow and exposes components to outside sources (via the ActionEvent#getSource
), which could allow people to change your component... 这假设一个工作流程并将组件暴露给外部源(通过
ActionEvent#getSource
),这可能允许人们更改您的组件......
You probably should... 你可能应该......
Fire a PropertyChanged
event. 触发
PropertyChanged
事件。
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. 以这种方式使用属性更改侦听器意味着您不需要公开
JProgressBar
, JButton
或在两个帧之间生成紧密链接。
Property change support is built in to Container
so all components/controls have it. 属性更改支持内置于
Container
因此所有组件/控件都具有它。
For example, you would attach a PropertyChangeListener
to b
when you construct it. 例如,在构造时,可以将
PropertyChangeListener
附加到b
。
b = new frameB();
b.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("progressState")) {
progressbar.setIndeterminate((Boolean)evt.getNewValue());
}
}
});
Add in bFrame
's actionPerformed
method, you would simple call... 添加
bFrame
的actionPerformed
方法,你可以简单地调用...
firePropertyChange("progressState", false, true);
When you want to set the indeterminate state (you could swap the boolean
values to reset it if you wanted to) 当你想设置不确定状态时(如果你愿意,你可以交换
boolean
值来重置它)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.