我有一个Main类,它有两个面板,左面板和右面板。左侧面板已经有一些组件和一个带有标签的右侧面板和一个位于NORTH的按钮。我想将另一个类的JPanel添加到Main类的右侧面板中。我到目前为止所做的是实例化类并将其添加到rightPanel但它似乎给我错误。这是Main类的代码:
package project;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import nodeChooser.gristDatabaseUtilities.SetupParentPopulation;
import nodeChooser.internal.NodeChooser;
import nodeChooser.publicInterfaces.Enumerations;
import nodeChooser.publicInterfaces.Precondition;
import nodeChooser.publicInterfaces.SubmitButtonListener;
import nodeChooser.publicInterfaces.UserSelection;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import utilities.Utilities;
public class Main extends JPanel implements SubmitButtonListener{
private JPanel mainPanel;
private JPanel leftPanel;
private JPanel rightPanel;
private JPanel centerPanel;
private JPanel southPanel;
private JSplitPane splitPane;
private Document cat;
private Document qt;
public Main() {
mainPanel = new JPanel(new BorderLayout());
setupJpanels();
setupMainWindow();
cat = Utilities.buildXMLDocumentFromFile("cat-woking-age.xml");
qt = Utilities.buildXMLDocumentFromFile("qt-woking-age.xml");
// this is how we plug nodeChooser into our project
NodeChooser nodeChooser = new NodeChooser(this, leftPanel, cat, qt);
}
private void setupJpanels() {
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setContinuousLayout(true);
splitPane.setDividerLocation(560);
splitPane.setOneTouchExpandable(true);
leftPanel = new JPanel();
rightPanel = new JPanel(new BorderLayout());
splitPane.setTopComponent(leftPanel);
splitPane.setBottomComponent(rightPanel);
mainPanel.add(splitPane, BorderLayout.CENTER);
}
private void setupMainWindow() {
final JFrame frame= new JFrame("GRiST Project '13");
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent winEvt) {
System.exit(0);
}
public void windowOpened(WindowEvent e) {
//frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
});
frame.getContentPane().add(mainPanel);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int width=600;
int height=400;
frame.setSize(width, height);
frame.setLocation((screenSize.width-width)/2,(screenSize.height-height)/2);
frame.setVisible(true);
}
/**
* this receives callbacks from nodeChooser
*/
@Override
public void submitClicked(final UserSelection userSelection) {
//the parameter userSelection has all the information that nodeChooser wants to pass on to us.
String query= "SELECT everything FROM mygrist_samples WHERE ";
JTextArea selectLabel= new JTextArea(query);
selectLabel.setLineWrap(true);
JPanel submitButtonPanel= new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton setSubmit= new JButton("Submit Query");
setSubmit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SetupParentPopulation parentpop= new SetupParentPopulation(userSelection);
JPanel setSplitPanel= new JPanel(new BorderLayout());
String rows= parentpop.getRowsInserted();
JLabel getInsertedRows= new JLabel(rows+" records found.", JLabel.LEFT);
setSplitPanel.add(getInsertedRows, BorderLayout.CENTER);
rightPanel.add(setSplitPanel);
**SplitMain splitMain= new SplitMain();
rightPanel.add(splitMain);**
}
});
submitButtonPanel.add(setSubmit);
rightPanel.add(selectLabel, BorderLayout.NORTH);
rightPanel.add(submitButtonPanel, BorderLayout.CENTER);
if (userSelection.getPreconditions().size() > 0) {
String getCorrespondingCodeFromDatabase;
for (int i = 0; i < userSelection.getPreconditions().size(); i++) {
getCorrespondingCodeFromDatabase = userSelection.getPreconditions().get(i).getCode();
getCorrespondingCodeFromDatabase = getCorrespondingCodeFromDatabase.replaceAll("-", "_");
Enumerations enum1= new Enumerations();
String getCorrespondingRelationship= enum1.getOperator(userSelection.getPreconditions().get(i).getRelationship());
query += getCorrespondingCodeFromDatabase + " " + getCorrespondingRelationship
+ " '" + userSelection.getPreconditions().get(i).getAnswer() + "'";
selectLabel.setText(query);
selectLabel.revalidate();
repaint();
if (i != userSelection.getPreconditions().size() - 1) {
query += " AND ";
}
}
} else {
System.out.println("No records found");
}
}
public static void main(String[] args) {
new Main();
}
}
这是SplitMain类(几乎与Main类完全相同,但有些方法已更改):
package project;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import nodeChooser.gristDatabaseUtilities.SetupSubsamplePopulation;
import nodeChooser.publicInterfaces.Enumerations;
import nodeChooser.publicInterfaces.SplitSubmitButtonListener;
import nodeChooser.publicInterfaces.UserSelectedSplit;
import org.w3c.dom.Document;
import utilities.Utilities;
import conditions.splitUtilities.SplitChooser;
public class SplitMain extends JPanel implements SplitSubmitButtonListener{
private JPanel mainPanel;
private JPanel leftPanel;
private JPanel rightPanel;
private JPanel centerPanel;
private JPanel southPanel;
private JSplitPane splitPane;
private Document cat;
private Document qt;
public SplitMain() {
rightPanel = new JPanel(new BorderLayout());
cat = Utilities.buildXMLDocumentFromFile("cat-woking-age.xml");
qt = Utilities.buildXMLDocumentFromFile("qt-woking-age.xml");
// this is how we plug splitChooser into our project
SplitChooser splitChooser = new SplitChooser(this, rightPanel, cat, qt);
}
@Override
public void submitSplitClicked(final UserSelectedSplit userSplitSelection) {
String query= "SELECT everything FROM mygrist_samples WHERE ";
JTextArea selectLabel= new JTextArea(query);
selectLabel.setLineWrap(true);
JPanel submitButtonPanel= new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton setSubmit= new JButton("Submit Query");
setSubmit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SetupSubsamplePopulation parentpop= new SetupSubsamplePopulation(userSplitSelection);
JPanel setSplitPanel= new JPanel(new BorderLayout());
String rows= parentpop.getRows();
JLabel getInsertedRows= new JLabel(rows+" records found.", JLabel.LEFT);
setSplitPanel.add(getInsertedRows, BorderLayout.NORTH);
getInsertedRows.revalidate();
getInsertedRows.repaint();
rightPanel.add(setSplitPanel, BorderLayout.CENTER);
}
});
submitButtonPanel.add(setSubmit);
rightPanel.add(selectLabel, BorderLayout.NORTH);
rightPanel.add(submitButtonPanel, BorderLayout.CENTER);
if (userSplitSelection.getSplitConditions().size() > 0) {
String getCorrespondingCodeFromDatabase;
for (int i = 0; i < userSplitSelection.getSplitConditions().size(); i++) {
getCorrespondingCodeFromDatabase = userSplitSelection.getSplitConditions().get(i).getCode();
getCorrespondingCodeFromDatabase = getCorrespondingCodeFromDatabase.replaceAll("-", "_");
Enumerations enum1= new Enumerations();
String getCorrespondingRelationship= enum1.getOperator(userSplitSelection.getSplitConditions().get(i).getRelationship());
query += getCorrespondingCodeFromDatabase + " " + getCorrespondingRelationship
+ " '" + userSplitSelection.getSplitConditions().get(i).getAnswer() + "'";
selectLabel.setText(query);
selectLabel.revalidate();
repaint();
if (i != userSplitSelection.getSplitConditions().size() - 1) {
query += " AND ";
}
}
} else {
System.out.println("No records found");
}
}
}
我如何将这个SplitMain类合并到Main类的rightPanel中?
编辑: 我得到的错误是:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at conditions.splitUtilities.SplitChooser.<init>(SplitChooser.java:133)
at project.SplitMain.<init>(SplitMain.java:49)
at project.Main$2.actionPerformed(Main.java:145)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
给我这个错误的课程是:
package conditions.splitUtilities;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import nodeChooser.internal.IconSource;
import nodeChooser.internal.SplitConditionChangeListener;
import nodeChooser.publicInterfaces.Enumerations.QuestionType;
import nodeChooser.publicInterfaces.Enumerations.Relationship;
import nodeChooser.publicInterfaces.SplitSubmitButtonListener;
import nodeChooser.publicInterfaces.UserSelectedSplit;
import nodeChooser.publicInterfaces.UserSplit;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
public class SplitChooser implements SplitConditionChangeListener {
private JPanel mainPanel;
private JPanel centerpanelWrapper;
private Document qt;
private Document cat;
private ArrayList<SplitConditionGui> splitConditions;
/**
*
* @param submitButtonListener a class that implements this interface so that we can notify them when submit button has been clicked
* @param panel the JPanel on which the node chooser would be painted
* @param cat the cat xml
* @param qt the qt xml
*/
public SplitChooser(final SplitSubmitButtonListener splitSubmitButtonListener, JPanel panel, Document cat, Document qt) {
this.qt = qt;
this.cat = cat;
splitConditions = new ArrayList<SplitConditionGui>();
centerpanelWrapper = panel;
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
addSplitCondition();
JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton addButton = new JButton("Add Precondition");
addButton.setIcon(IconSource.createImageIcon("images2/add.png"));
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addSplitCondition();
}
});
northPanel.add(addButton);
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton queryButton = new JButton("Submit");
queryButton.setIcon(IconSource.createImageIcon("images2/accept.png"));
queryButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
splitSubmitButtonListener.submitSplitClicked(new UserSelectedSplit() {
@Override
public ArrayList<UserSplit> getSplitConditions() {
ArrayList<UserSplit> splitConditionList = new ArrayList<>();
for(final SplitConditionGui splitList: splitConditions) {
if(splitList.getNodeCode() == null)
continue;
splitConditionList.add(new UserSplit() {
@Override
public boolean isLeafNode() {
return splitList.isLeaf();
}
@Override
public Node getSelectedCatNode() {
return splitList.getCatNode();
}
@Override
public Relationship getRelationship() {
return splitList.getRelationship();
}
@Override
public Node getQTNode() {
return splitList.getQtNode();
}
@Override
public QuestionType getNodeType() {
return splitList.getQuestionType(splitList.getQtNode());
}
@Override
public String getCode() {
return splitList.getNodeCode();
}
@Override
public String getAnswer() {
return splitList.getAnswer();
}
});
}
return splitConditionList;
}
});
}
});
southPanel.add(queryButton);
JScrollPane jsp = new JScrollPane(mainPanel);
jsp.setBorder(null);
//this part gives nullPointerException and I don't know why it's passing null. Seems to be working fine for other classes
centerpanelWrapper.removeAll();
centerpanelWrapper.setLayout(new BorderLayout(10,5));
centerpanelWrapper.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5), BorderFactory.createTitledBorder("Preconditions")));
centerpanelWrapper.add(jsp, BorderLayout.CENTER);
centerpanelWrapper.add(northPanel, BorderLayout.NORTH);
centerpanelWrapper.add(southPanel, BorderLayout.SOUTH);
centerpanelWrapper.revalidate();
centerpanelWrapper.repaint();
}
private void addSplitCondition() {
SplitConditionGui splitConditionGui = new SplitConditionGui(this, cat, qt);
splitConditions.add(splitConditionGui);
mainPanel.add(splitConditionGui.getJpanel());
mainPanel.revalidate();
mainPanel.repaint();
}
private void removeSplitCondition(SplitConditionGui splitConditionGui) {
splitConditions.remove(splitConditionGui);
mainPanel.remove(splitConditionGui.getJpanel());
mainPanel.revalidate();
mainPanel.repaint();
}
@Override
public void removedCondition(SplitConditionGui splitConditionGui) {
removeSplitCondition(splitConditionGui);
}
}