我已经在我的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按钮之间有一个“查看问题”按钮。请帮忙。非常感谢你:))
答案 0 :(得分: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);
}
}