你好,我有这样的东西
public class GUI{
public JCheckBox box;
public boolean value;
void init(){
box.addActionListener(new BoxListener(this));
}
}
public class BoxListener implements ActionListener{
GUI gui;
public BoxListener(GUI gui){
this.gui = gui;
}
@override
public void actionperformed(ActionEvent a){
gui.value = true;
}
}
现在我想在单击复选框时更改 boolean 值。 我正在寻找比将类 GUI 作为自编写监听器的输入参数传递更好的解决方案。
还有更好的解决方案吗?
最佳答案
我会加入一些 MVC,这需要做更多的工作,但它可以实现更灵活的编码。
首先是一个带有 boolean 检查
的ModelBean
和用于触发属性更改事件的propertyChangeSupport
import java.beans.*;
import java.io.Serializable;
public class ModelBean implements Serializable {
private boolean checked;
private PropertyChangeSupport propertySupport;
public ModelBean() {
propertySupport = new PropertyChangeSupport(this);
}
public boolean getChecked() {
return checked;
}
public void setChecked(boolean checked) {
boolean oldValue = this.checked;
this.checked = checked;
propertySupport.firePropertyChange("checked", oldValue, this.checked);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertySupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertySupport.removePropertyChangeListener(listener);
}
}
然后您就有了主 GUI
类,它采用 ModelBean
作为参数
class GUI extends JFrame {
private ModelBean model;
private JCheckBox cbox;
public GUI(ModelBean model) {
this.model = model;
cbox = new JCheckBox("Check and watch me print");
cbox.addItemListener(new CheckListener(model));
setLayout(new GridBagLayout());
add(cbox);
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
您的 ItemListener
类采用相同的 ModelBean
作为参数,并将 PropertyChangeListener
添加到 model
>
class CheckListener implements ItemListener {
private ModelBean model;
public CheckListener(ModelBean newModel) {
this.model = newModel;
model.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
System.out.println(model.getChecked());
}
});
}
@Override
public void itemStateChanged(ItemEvent e) {
JCheckBox source = (JCheckBox) e.getSource();
if (source.isSelected()) {
model.setChecked(true);
} else {
model.setChecked(false);
}
}
}
然后你类来运行程序
public class TestMVC {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final ModelBean model = new ModelBean();
GUI gui = new GUI(model);
}
});
}
}
对于一个愚蠢的 boolean 值
来说似乎需要做很多工作,但这个概念是为大型程序提供更好的整体设计,在这些程序中需要操纵和监听大量不同的数据以进行更改。
更新
另一种选择,如果您的 GUI 是父容器,并且您不想通过引用传递它来公开它,则可以创建一个 interface
并拥有 GUI
实现它
public interface BooleanInterface {
public void setBoolean(boolean bool);
}
public class GUI extends JFrame implements BooleanInterface {
boolean bool;
@Override
public void setBoolean(boolean bool) {
this.bool = bool;
}
}
public BoxListener implements ActionListener {
BooleanInterface boolFace;
public BoxListener(BooleanInterface boolFace) {
this.boolFace = boolFace;
}
}
然后您可以将 GUI
传递给监听器。虽然它看起来与您已经在做的事情相同,但实际上并非如此,因为它不再公开 GUI,而是使用接口(interface)。
关于java - JCheckBox在其他类中设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21628599/