为什么GUI无法正常工作?

时间:2013-08-13 19:48:59

标签: java swing

我添加了一个可折叠的树,这是一个可折叠的复选框,但它没有出现在第三个面板上。你能看出问题所在并建议修复吗?我不知道问题是什么,我认为我做的方式是假设工作但是当我启动GUI时我无法在第三个面板上看到任何内容

import javax.swing.*;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class MyWizard {

    private JFrame frame = new JFrame("My Wizard");
    private JPanel panelContainer = new JPanel();
    private JPanel panelFirst = new JPanel();
    private JPanel panelSecond = new JPanel();
    private JPanel panelThird = new JPanel();
    private JButton btNext = new JButton("Next");
    private JButton btNextTwo = new JButton("Next");
    private JButton btNextThree = new JButton("Next");
    private JRadioButton btLdap, btKerbegos, btSpnego, btSaml2;
    private JCheckBox btSan, btNFS, btYUMserver;
    private CardLayout c1 = new CardLayout();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

    public MyWizard() {
        panelContainer.setLayout(c1);
        panelFirst.add(btNext);
        panelSecond.add(btNextTwo);
        panelThird.add(btNextThree);
        panelFirst.setLayout(null);
        panelSecond.setLayout(null);
        panelThird.setLayout(null);
        panelContainer.add(panelFirst, "1");
        panelContainer.add(panelSecond, "2");
        panelContainer.add(panelThird, "3");
        c1.show(panelContainer, "1");
        final DefaultMutableTreeNode accessibility =
                add(root, "Accessibility", true);
        add(accessibility, "Move system caret with focus/selection changes", false);
        add(accessibility, "Always expand alt text for images", true);
        root.add(accessibility);
        final DefaultMutableTreeNode browsing =
                new DefaultMutableTreeNode("Browsing");
        add(browsing, "Notify when downloads complete", true);
        add(browsing, "Disable script debugging", true);
        add(browsing, "Use AutoComplete", true);
        add(browsing, "Browse in a new process", false);
        root.add(browsing);
        final DefaultTreeModel treeModel = new DefaultTreeModel(root);
        final JTree tree = new JTree(treeModel);
        final CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
        tree.setCellRenderer(renderer);
        final CheckBoxNodeEditor editor = new CheckBoxNodeEditor(tree);
        tree.setCellEditor(editor);
        tree.setEditable(true);
        JScrollPane scrollPane = new JScrollPane(tree);
        panelThird.add(scrollPane);
        btNext.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                c1.show(panelContainer, "2");

            }
        });
        btNextTwo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                c1.show(panelContainer, "3");

            }
        });
        RadioButtons();
        Button();
        Buttons();
        CheckList();
        groupButton();
        ButtonsTwo();
        frame.add(panelContainer);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setSize(600, 360);
        frame.setVisible(true);
    }

    public void RadioButtons() {
        btLdap = new JRadioButton("Ldap");
        btLdap.setBounds(60, 85, 100, 20);
        panelFirst.add(btLdap);
        btKerbegos = new JRadioButton("Kerbegos");
        btKerbegos.setBounds(60, 115, 100, 20);
        panelFirst.add(btKerbegos);
        btSpnego = new JRadioButton("Spnego");
        btSpnego.setBounds(60, 145, 100, 20);
        panelFirst.add(btSpnego);
        btSaml2 = new JRadioButton("Saml2");
        btSaml2.setBounds(60, 175, 100, 20);
        panelFirst.add(btSaml2);
    }

    public void Button() {
        btNext.setBounds(400, 260, 100, 20);
    }

    public void CheckList() {
        btSan = new JCheckBox("San");
        btSan.setBounds(60, 85, 100, 20);
        panelSecond.add(btSan);
        btNFS = new JCheckBox("NFS");
        btNFS.setBounds(60, 115, 100, 20);
        panelSecond.add(btNFS);
        btYUMserver = new JCheckBox("Spnego");
        btYUMserver.setBounds(60, 145, 100, 20);
        panelSecond.add(btYUMserver);
    }

    public void Buttons() {
        btNextTwo.setBounds(400, 260, 100, 20);
    }

    public void ButtonsTwo() {
        btNextThree.setBounds(400, 260, 100, 20);
    }

    private void groupButton() {
        ButtonGroup bg1 = new ButtonGroup();
        bg1.add(btLdap);
        bg1.add(btKerbegos);
        bg1.add(btSpnego);
        bg1.add(btSaml2);

    }

    private static DefaultMutableTreeNode add(
            final DefaultMutableTreeNode parent, final String text,
            final boolean checked) {
        final CheckBoxNode data = new CheckBoxNode(text, checked);
        final DefaultMutableTreeNode node = new DefaultMutableTreeNode(data);
        parent.add(node);
        return node;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MyWizard();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:2)

//panelFirst.setLayout(null);
//panelSecond.setLayout(null);
//panelThird.setLayout(null);

不要将布局设置为null !!!

Swing旨在与布局管理器一起使用。