我正在做家谱,这是我的问题。我有屏幕NewFamilyTree.javaNewPerson.java

NewFamilyTree.java:

public class NewFamilyTree {
...
 private void initialize() {
    frame = new JFrame();
    frame.setTitle("New family tree");
    frame.setBounds(100, 100, 906, 569);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

    JScrollPane scrollPane = new JScrollPane();
    tabbedPane.addTab("Tree", null, scrollPane, null);

    panel_1 = new JPanel();
    scrollPane.setViewportView(panel_1);
    panel_1.setLayout(new MigLayout("", "[][][][][][][][]", "[][][][][][]"));


NewPerson.java:

public class NewPerson{

...

buttonAdd = new JButton("Add");
        buttonAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                String names = textFieldNames.getText();
                String dateBirth = textFieldDateOfBirth.getText();
                String bio = textAreaBio.getText();

                Data newData = new Data(names, dateBirth, bio, fileID);

               //code that puts new label on scrollpane from NewFamilyTree.java
            }
        });
        buttonAdd.setBackground(new Color(30, 144, 255));
        frame.getContentPane().add(buttonAdd, "cell 2 6,grow");
    }


我需要通过按new JLabelclass NewPerson上的添加按钮,将JScrollPaneNewFamilytree.java放入。希望有人可以提供帮助,我进行了很多搜索,但也无济于事。

编辑:@mjr的答案后。

我在public JPanel panel_1;中添加了NewFamilyTree。在执行的Add操作中,我添加了:

JLabel lblHomer = new JLabel("Homer");
                lblHomer.setIcon(new ImageIcon("C:\\Users\\Tinmar\\Desktop\\HomerSimpson3.gif"));
                panel_1.add(lblHomer, "cell 7 5");


没有错误,但是-按添加按钮后没有任何反应。我还添加了NewPerson EXTENDS NewFamilyTreeofc

最佳答案

NewPerson不需要从NewFamilyTree扩展,也没有向类添加任何功能
代替在JFrame中使用NewPerson,可以考虑使用模式JDialog。有关更多详细信息,请参见How to Make Dialogs
限制类之间组件的暴露。没有理由为什么NewFamilyTree应该能够访问NewPerson使用的“窗口”。没有理由为什么NewPerson应该向NewFamilyTree添加任何内容
不要将重量较大的成分(例如java.awt.Button)与重量较轻的成分混合使用,这样就不会导致问题的终结。


您需要改变思维方式。不用让NewPerson更新NewFamilyTree的UI,而是让NewPerson从用户那里收集详细信息,并将此信息传递回NewFamilyTree,以便它可以使用...

例如...

JButton newPersonButton = new JButton("New Person");
newPersonButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        Data data = NewPerson.createPerson(frame);
        if (data != null) {
            JLabel lblHomer = new JLabel(data.names);
            panel_1.add(lblHomer, "cell 7 5");
            panel_1.revalidate();
        }
    }
});


这基本上使用了static方法createPerson,该方法传回Data的实例(如果用户取消了操作,则传回null),然后NewFamilyTree可以使用该实例。它使代码解耦,因为NewPerson不依赖NewFamilyTree中的任何内容,并且NewFamilyTree保持控制。这清楚地定义了两个类之间的职责范围,也意味着可以从任何地方调用NewPerson

createPerson方法看起来像这样...

public static Data createPerson(Component comp) {
    NewPerson newPerson = new NewPerson();
    Window win = SwingUtilities.getWindowAncestor(comp);
    JDialog dialog = null;
    if (win instanceof Frame) {
        dialog = new JDialog((Frame) win, "New person", true);
    } else if (win instanceof Dialog) {
        dialog = new JDialog((Dialog) win, "New person", true);
    } else {
        dialog = new JDialog((Frame) null, "New person", true);
    }
    newPerson.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            if (source instanceof Component) {
                Window win = SwingUtilities.getWindowAncestor((Component) source);
                win.dispose();
            }
        }
    });
    dialog.add(newPerson);
    dialog.setVisible(true);

    return newPerson.getData();
}


它基本上创建JDialog的实例,将其显示给用户并等待,直到NewPerson类触发ActionEvent,它用于处理对话框。然后它向NewPerson实例询问数据...

而且由于没有提到很多功能,因此这里有一个完全可运行的示例...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

public class NewFamilyTree {

    private JFrame frame;
    private JPanel panel_1;
    private JScrollPane scrollPane;
    private JTabbedPane tabbedPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NewFamilyTree window = new NewFamilyTree();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });
    }

    /**
     * Create the application.
     */
    public NewFamilyTree() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setTitle("New family tree");
        frame.getContentPane().setBackground(new Color(135, 206, 250));
        frame.setBounds(100, 100, 906, 569);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBackground(new Color(30, 144, 255));
        frame.getContentPane().add(panel, BorderLayout.EAST);
        panel.setLayout(new MigLayout("", "[]", "[][][][][][][][]"));

        JButton newPersonButton = new JButton("New Person");
        newPersonButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Data data = NewPerson.createPerson(frame);
                if (data != null) {
                    JLabel lblHomer = new JLabel(data.names);
//                    lblHomer.setIcon(new ImageIcon("C:\\Users\\Tinmar\\Desktop\\HomerSimpson3.gif"));
                    panel_1.add(lblHomer, "cell 7 5");
                    panel_1.revalidate();
                }
            }
        });
        panel.add(newPersonButton, "cell 0 5");

        JButton btnNewButton_1 = new JButton("New button");
        panel.add(btnNewButton_1, "cell 0 6");

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

        scrollPane = new JScrollPane();
        tabbedPane.addTab("Tree", null, scrollPane, null);

        panel_1 = new JPanel();
        scrollPane.setViewportView(panel_1);
        panel_1.setLayout(new MigLayout("", "[][][][][][][][]", "[][][][][][]"));

//        JLabel lblHomer = new JLabel("Homer");
//        lblHomer.setIcon(new ImageIcon("C:\\Users\\Tinmar\\Desktop\\HomerSimpson3.gif"));
//        panel_1.add(lblHomer, "cell 7 5");
        JScrollPane scrollPane_1 = new JScrollPane();
        tabbedPane.addTab("Info", null, scrollPane_1, null);

        frame.repaint();

    }
//
//    /**
//     * Launch the application.
//     */
//    public static void main(String[] args) {
//        EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                try {
//                    NewPerson window = new NewPerson();
//                    window.frame.setVisible(true);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

    public static class NewPerson extends JPanel {

        private JTextField textFieldNames;
        private JButton selectPictureButton;
        private JLabel labelDateOfBirth;
        private JTextField textFieldDateOfBirth;
        private JLabel labelShortBio;
        private JTextArea textAreaBio;
        private JLabel labelSelectPicture;
        private JButton buttonAdd;
        private String fileID;

        private Data data;

        /**
         * Create the application.
         */
        private NewPerson() {
            initialize();
        }

        public static Data createPerson(Component comp) {
            NewPerson newPerson = new NewPerson();
            Window win = SwingUtilities.getWindowAncestor(comp);
            JDialog dialog = null;
            if (win instanceof Frame) {
                dialog = new JDialog((Frame) win, "New person", true);
            } else if (win instanceof Dialog) {
                dialog = new JDialog((Dialog) win, "New person", true);
            } else {
                dialog = new JDialog((Frame) null, "New person", true);
            }
            newPerson.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Object source = e.getSource();
                    if (source instanceof Component) {
                        Window win = SwingUtilities.getWindowAncestor((Component) source);
                        win.dispose();
                    }
                }
            });
            dialog.add(newPerson);
            dialog.pack();
            dialog.setLocationRelativeTo(comp);
            dialog.setVisible(true);

            return newPerson.getData();
        }

        public void addActionListener(ActionListener listener) {
            listenerList.add(ActionListener.class, listener);
        }

        protected void fireActionPerformed() {
            ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
            if (listeners != null && listeners.length > 0) {
                ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "created");
                for (ActionListener listener : listeners) {
                    listener.actionPerformed(evt);
                }
            }
        }

        public Data getData() {
            return data;
        }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            setBackground(new Color(135, 206, 250));
            setLayout(new MigLayout("", "[][][grow]", "[][][][grow][][][]"));

            JLabel labelNames = new JLabel("Name and Surname:");
            add(labelNames, "cell 1 1,alignx trailing");

            textFieldNames = new JTextField();
            add(textFieldNames, "cell 2 1,growx");
            textFieldNames.setColumns(10);

            labelDateOfBirth = new JLabel("Date of birth:");
            add(labelDateOfBirth, "cell 1 2,alignx center,aligny center");

            textFieldDateOfBirth = new JTextField();
            add(textFieldDateOfBirth, "cell 2 2,growx");
            textFieldDateOfBirth.setColumns(10);

            labelShortBio = new JLabel("Bio:");
            add(labelShortBio, "cell 1 3,alignx center,aligny center");

            textAreaBio = new JTextArea();
            add(textAreaBio, "cell 2 3,grow");

            labelSelectPicture = new JLabel("Select picture:");
            add(labelSelectPicture, "cell 1 4,alignx center,aligny center");

            selectPictureButton = new JButton("...");
            selectPictureButton.setBackground(new Color(30, 144, 255));
            selectPictureButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {

                    JFileChooser chooser = new JFileChooser(new File(System.getProperty("user.home") + "\\Desktop"));

                    chooser.setDialogTitle("Select Location");
                    chooser.setFileSelectionMode(JFileChooser.APPROVE_OPTION);
                    chooser.setAcceptAllFileFilterUsed(false);

                    if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                        fileID = chooser.getSelectedFile().getPath();
                        // txtField.setText(fileID);
                    }

                }
            });
            add(selectPictureButton, "cell 2 4");

            buttonAdd = new JButton("Add");
            buttonAdd.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {

                    String names = textFieldNames.getText();
                    String dateBirth = textFieldDateOfBirth.getText();
                    String bio = textAreaBio.getText();

                    data = new Data(names, dateBirth, bio, fileID);
                    fireActionPerformed();
                }
            });
            buttonAdd.setBackground(new Color(30, 144, 255));
            add(buttonAdd, "cell 2 6,grow");
        }

    }

    public static class Data {

        private final String names;
        private final String dateBirth;
        private final String bio;
        private final String fileID;

        private Data(String names, String dateBirth, String bio, String fileID) {
            this.names = names;
            this.dateBirth = dateBirth;
            this.bio = bio;
            this.fileID = fileID;
        }

    }

}



不要依赖static提供跨类的功能。如果您需要其他课程提供的知识,请将其作为参考。 static不是您的朋友,您应该小心并小心使用它
不要在没有充分理由的情况下公开类的字段,请依靠interface允许类提供或获取信息。这限制了其他类可以做什么。
分离和隔离责任
看看How to Use Trees

10-08 10:52