简体   繁体   English

尽管使用了正确的布局管理器,但JButton仍未出现在JPanel上

[英]JButton not appearing on JPanel despite using correct layout managers, etc

I have been working with Swing for a while now on my CS project and, this may sound redundant, but I am unable to pop in my JButton in my JPanel. 我已经在CS项目上使用Swing了一段时间,这听起来可能很多余,但是我无法在JPanel中弹出我的JButton。 There are two other buttons which follow the same code and they get displayed but this one I create just refuses to show up. 还有另外两个遵循相同代码的按钮,它们会显示出来,但是我创建的这个按钮只是拒绝显示。 The button is "View Question" button. 该按钮是“查看问题”按钮。 Similar buttons like the OK and CANCEL work fine. 像“确定”和“取消”之类的类似按钮也可以正常工作。 Any help would be much appreciated. 任何帮助将非常感激。 The code: 编码:

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;

}

} }

In short, I want a "View Question" button in between the OK and CANCEL buttons. 简而言之,我想在“确定”和“取消”按钮之间插入一个“查看问题”按钮。 Please help. 请帮忙。 Thank you so much :) 非常感谢 :)

I'm posting this though I don't expect it to help very much. 我发布了此内容,尽管我希望它不会有太大帮助。 It illustrates that what you are doing works in the simple case, and that to answer your question we need to figure out where you deviate from the simple case (and from correct Swing usage). 它说明了您在简单情况下所做的工作,并且为了回答您的问题,我们需要弄清楚您与简单情况(以及正确的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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM