我在我的 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 按钮之间有一个“查看问题”按钮。请帮忙。太感谢了 :)