小编典典

Java 如何将一个窗格连接到另一个窗格

java

如何将输出连接到paneWithList

PaneWithList上有一个侦听器,JList以便将选定的行输出到控制台。如何将输出定向到JTextPane打开输出?

PaneWithList引发一个事件Main吗?会的PropertyChangeSupport足矣?

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);
    }
}

阅读 524

收藏
2020-03-02

共1个答案

小编典典

这是一个使用观察者模式的示例,。请注意,也有可能收听组合的模型。

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;
        }
    }
}
2020-03-02