java + swing + mvc:如何创建一个带有多个控件的接口模型? [英] java+swing+mvc: how to make a model that interfaces w/ multiple controls?

查看:110
本文介绍了java + swing + mvc:如何创建一个带有多个控件的接口模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用的Swing应用程序,它具有一对同时显示&控制两个数字的值.现在,我将各种东西融合在一起以使其正常工作,拦截更改事件并更新显示.但是,我在想一种更好的方法是制作一个具有两个数字的模型,并以某种方式将此模型链接到我的GUI视图.我知道如何制作模型,但是对于如何将其链接到我的GUI视图感到迷茫……有什么建议吗?

I have this Swing app I'm working on that has a pair of JSliders and JTextFields that both display & control the value of two numbers. Right now I kind of hacked things together to make it work, intercepting change events and updating the display. However I'm thinking a better way would be to make a model that has the two numbers and somehow link this model to my GUI view. I know how to make a model, but am kind of lost on how I could link that to my GUI view... any suggestions?

更新:我的模型已准备就绪,并且包含触发属性更改.我想我想使用某种数据绑定方法(例如JGoodies Binding),但是我不确定如何……JGoodies Binding不支持滑块.

update: my model is ready + includes firing property changes. I think I would like to use some kind of data binding approach (e.g. JGoodies Binding) but I am not sure how... JGoodies Binding doesn't support sliders.

edit:还请注意,当滑块更改位置时,文本字段应更改其值;当文本字段更改值时,滑块应更改位置;当模型更改值时,两个文本字段和滑块都应更新.

edit: also note, when the slider changes position, the text field should change its value; when the text field changes value, the slider should change position; and when the model changes value, both text field + slider should update.

推荐答案

要使两个组件保持同步,只需让每个组件的 JSlider JSpinner :

To keep the two components synchronized, just have each one's EventListener update the other. Here's an example connecting a JSlider and JSpinner:

public class SpinSlider extends JPanel {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("SpinSlider");
                frame.add(new SpinSlider());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public SpinSlider() {
        this.setLayout(new FlowLayout());
        final JSpinner spinner = new JSpinner();
        final JSlider slider = new JSlider();
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider s = (JSlider) e.getSource();
                spinner.setValue(s.getValue());
            }
        });
        this.add(slider);
        spinner.setModel(new SpinnerNumberModel(50, 0, 100, 1));
        spinner.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner s = (JSpinner) e.getSource();
                slider.setValue((Integer) s.getValue());
            }
        });
        this.add(spinner);
    }
}

这篇关于java + swing + mvc:如何创建一个带有多个控件的接口模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆