0

我试图通过单击 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                  
}
4

4 回答 4

2

如果您只是在处理某些事件中更改窗口的卡片,以下可能会有所帮助。

Oracle 教程示例

于 2013-05-14T09:53:59.907 回答
1

我不确定这是否可行,但是:

我看到的是您正在尝试“刷新/重新绘制/验证” jPanelSliding值。相反,您应该修改的是您尝试单击的面板。jPanelSliding 可能会更新,但面板(在 main 上声明)没有被实现。

那么,为什么不尝试对 main 中声明的面板进行“验证”调用呢?

你能尝试一下并告诉我们它是否有效吗?

于 2013-05-14T10:39:01.380 回答
0

我也在处理同样的问题。
经过一些测试,重新测试和很多失败后,我的伪算法是这样的:

parentPanel:包含我们要删除的面板
childPanel:我们要切换的面板
parentPanelLayout:parentPanel 的布局
editParentLayout():使用不同的 childPanel 和 NEW 构建 parentPanel parentPanelLayout 每次

parentPanel.remove(childPanel); 
editParentLayout();
parentPanel.revalidate();
parentPanel.repaint();
于 2013-10-26T07:55:24.200 回答
0

找到了我的问题的答案,由于某种原因,我必须在创建面板后为其命名,否则永远找不到。谢谢您的帮助。

     JPanel panel = new JPanel();
**panel.setName("abc");**
        panel.setLayout(new BorderLayout());
于 2013-05-14T11:03:33.007 回答