0

我在我的 CS 项目上使用 Swing 已经有一段时间了,这听起来可能是多余的,但我无法在我的 JPanel 中弹出我的 JButton。还有另外两个按钮遵循相同的代码,它们会显示出来,但我创建的这个按钮只是拒绝显示。该按钮是“查看问题”按钮。OK 和 CANCEL 等类似按钮工作正常。任何帮助将非常感激。编码:

public class CatNodePicker {

private final JDialog dialog;
private String selectedNodeCode;
private XMLTreeNode selectedNode;
private JTree cat;

public CatNodePicker(Container container, JTree cat) {
    this.cat = cat;
    selectedNodeCode = null;
    selectedNode = null;
    dialog =new JDialog(findParentFrame(container), "Pick a LEAF node", true);
    JPanel buttonPanel = new JPanel();
    final JButton okButton = new JButton("OK");
    okButton.setEnabled(false);
    okButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            dialog.dispose();
        }
    });
    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            selectedNodeCode = null;
            selectedNode = null;
            dialog.dispose();
        }
    });

    JButton viewQuestion = new JButton("View Question");
    viewQuestion.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            getQuestionLabel();
        }
    });

    buttonPanel.add(okButton);
    buttonPanel.add(viewQuestion);
    buttonPanel.add(cancelButton);

    cat.expandRow(1);
    JScrollPane jsp = new JScrollPane(cat);

    cat.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            TreePath path=e.getNewLeadSelectionPath();
            if(path==null) {
                selectedNodeCode = null;
                selectedNode = null;
            }

            if (path.getLastPathComponent().equals(path.getPathComponent(1))) {
                selectedNodeCode = null;
                selectedNode = null;
            }
            else {
                XMLTreeNode n=((XMLTreeNode)path.getLastPathComponent());
                //System.out.println(n.toString());
                if(n.isLeaf() || n.isFilter() || n.isLayer()) {
                    selectedNodeCode = n.getCode();
                    selectedNode = n;
                    okButton.setEnabled(true);
                }
                else
                    okButton.setEnabled(false);
            }

        }
    });

    dialog.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            selectedNodeCode = null;
            selectedNode = null;
            dialog.dispose();
        }
    });  

    dialog.getContentPane().add(jsp, BorderLayout.CENTER);
    dialog.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    dialog.setSize(new Dimension(600,800));
    dialog.setLocationRelativeTo(null);
    dialog.pack();

}

public JComponent getQuestionLabel(){

    JLabel questionText= new JLabel("Question here");
    return questionText;

}

}

简而言之,我想要在 OK 和 CANCEL 按钮之间有一个“查看问题”按钮。请帮忙。太感谢了 :)

4

1 回答 1

0

虽然我不希望它有很大帮助,但我正在发布这个。它说明了您正在做的事情在简单案例中有效,并且要回答您的问题,我们需要弄清楚您在哪里偏离了简单案例(以及正确的 Swing 用法)。

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;


public class Dialoger
{
    public static void main(String[] args)
    {
        Dialoger dialoger = new Dialoger();
        dialoger.go();
    }

    public void go()
    {
        JDialog jd = new JDialog();
        JButton one = new JButton("one");
        JButton two = new JButton("two");
        JButton three = new JButton("three");
        JPanel panel = new JPanel();
        panel.add(one);
        panel.add(two);
        panel.add(three);
        jd.add(panel, BorderLayout.SOUTH);
        jd.pack();
        jd.setVisible(true);
    }

}
于 2013-03-25T17:42:12.060 回答