如何将一个窗格连接到另一个窗格 [英] How to wire one pane to another

查看:32
本文介绍了如何将一个窗格连接到另一个窗格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将输出连接到 paneWithList?

PaneWithList 在其 JList 上有一个侦听器,以便将所选行输出到控制台.如何将该输出定向到输出时的 JTextPane?

PaneWithList has a listener on its JList so that the selected row is output to the console. How can I direct that output to the JTextPane on output?

能否PaneWithList 触发一个Main 拾取的事件?PropertyChangeSupport 就足够了吗?

Could PaneWithList fire an event which Main picks up? Would PropertyChangeSupport suffice?

Main.java:

package dur.bounceme.net;

import javax.swing.JTabbedPane;

public class Main {

    private static JTabbedPane tabs;
    private static PaneWithList paneWithList;
    private static PaneWithTable paneWithTable;
    private static Output output;

    public static void main(String[] args) {
        tabs = new javax.swing.JTabbedPane();
        paneWithList = new PaneWithList();
        paneWithTable = new PaneWithTable();
        tabs.addTab("list", paneWithList);
        tabs.addTab("table", paneWithTable);
        tabs.addTab("output", output);
    }
}

推荐答案

这是一个使用 观察者模式,也见这里此处此处.请注意,还可以收听组合的模型.

Here's an example using the observer pattern, also seen here, here and here. Note that it would also be possible to listen to the combo's model.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;

/**
* @see http://en.wikipedia.org/wiki/Observer_pattern
* @see https://stackoverflow.com/a/10523401/230513
*/
public class PropertyChangeDemo {

    public PropertyChangeDemo() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        f.add(new ObserverPanel());
        f.pack();
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                PropertyChangeDemo example = new PropertyChangeDemo();
            }
        });
    }
}

class ObserverPanel extends JPanel {

    private JLabel title = new JLabel("Value received: ");
    private JLabel label = new JLabel("null", JLabel.CENTER);

    public ObserverPanel() {
        this.setBorder(BorderFactory.createTitledBorder("ObserverPanel"));
        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(title);
        panel.add(label);
        this.add(panel);
        ObservedPanel observed = new ObservedPanel();
        observed.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent e) {
                if (e.getPropertyName().equals(ObservedPanel.PHYSICIST)) {
                    String value = e.getNewValue().toString();
                    label.setText(value);
                }
            }
        });
        this.add(observed);
    }
}

class ObservedPanel extends JPanel {

    public static final String PHYSICIST = "Physicist";
    private static final String[] items = new String[]{
        "Alpher", "Bethe", "Gamow", "Dirac", "Einstein"
    };
    private JComboBox combo = new JComboBox(items);
    private String oldValue;

    public ObservedPanel() {
        this.setBorder(BorderFactory.createTitledBorder("ObservedPanel"));
        combo.addActionListener(new ComboBoxListener());
        this.add(combo);
    }

    private class ComboBoxListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            String newValue = (String) combo.getSelectedItem();
            firePropertyChange(PHYSICIST, oldValue, newValue);
            oldValue = newValue;
        }
    }
}

这篇关于如何将一个窗格连接到另一个窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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