我试图通过单击 JButton 将新卡添加到现有的 JPanel(cardLayout),然后转到该新卡,但由于新卡未注册,因此出现 Null 异常。
我试过搜索谷歌,但找不到示例,我尝试使用“保存”布局
.validate();
.refresh();
.repaint();
但没有任何效果,这可能是动态的吗?如果是这样,有人知道任何例子吗?
下面的代码中,jPanelSliding1.nextSlidPanel 只是移动到 CardLayout 中选定的 JPanel。由于未找到面板,此处创建的新面板导致空错误。谢谢:)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package partnerships;
import components.ColorScheme;
import components.Utilities;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JSlidingPanel extends javax.swing.JPanel implements PropertyChangeListener {
public PropertyChangeSupport pcs = new PropertyChangeSupport(this);
private ColorScheme scheme;
public JSlidingPanel() {
initComponents();
pcs.addPropertyChangeListener(this);
scheme = new ColorScheme();
Object[][] object = {
{pcs, "Offered", "BT details", scheme.green()},
{pcs, "Sold", "BT details", scheme.pink()},
{pcs, "Other", "BT details", scheme.purple()}
};
jPanelSliding1.add(makePanels(object));
}
private void setupGUI() {
}
private JPanel makePanels(Object[][] objects) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, objects.length));
for (Object[] properties : objects) {
panel.add(new ObjectPanel((PropertyChangeSupport) properties[0], properties[1].toString(), properties[2].toString(), (Color) properties[3]));
}
return panel;
}
private void newPanel(String str) {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JButton());
jPanelSliding1.add(panel);
jPanelSliding1.refresh();
jPanelSliding1.validate();
jPanelSliding1.repaint();
validate();
repaint();
Utilities.getTopFrame().pack();
jPanelSliding1.NextSlidPanel(10, panel);
jPanelSliding1.refresh();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (this.isVisible() == true) {
// makePanels();
newPanel(evt.getPropertyName());
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JSlidingPanel app = new JSlidingPanel();
panel.add(app);
frame.add(panel);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
});
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanelSliding1 = new apps.JPanelSliding();
setPreferredSize(new java.awt.Dimension(900, 600));
setLayout(new java.awt.BorderLayout());
jPanelSliding1.setLayout(new java.awt.CardLayout());
add(jPanelSliding1, java.awt.BorderLayout.CENTER);
}// </editor-fold>
// Variables declaration - do not modify
private apps.JPanelSliding jPanelSliding1;
// End of variables declaration
}