我试图通过点击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
}
答案 0 :(得分:2)
如果您只是在某些事件上处理更改窗口的卡片,以下情况可能有所帮助。
答案 1 :(得分:1)
我不确定这是否可行但是:
我看到的是您正在尝试“刷新/重新/验证” jPanelSliding 值。相反,您应修改的是您尝试点击的面板。可以更新jPanelSliding,但是面板(在main上声明)没有被实现。
那么,为什么不尝试对主要声明的面板进行“验证”调用?
你可以尝试并告诉我们它是否有效吗?
答案 2 :(得分:0)
找到我的问题的答案,由于某种原因,我必须在创建后命名面板,否则永远找不到它。谢谢您的帮助。
JPanel panel = new JPanel();
**panel.setName("abc");**
panel.setLayout(new BorderLayout());
答案 3 :(得分:0)
我也在处理同样的问题。
经过一些测试,重新测试和很多失败的我的伪算法是这样的:
parentPanel:包含我们要删除的面板
childPanel:我们要切换的面板
parentPanelLayout:parentPanel的布局
editParentLayout():每次使用不同的childPanel和NEW parentPanelLayout构建parentPanel
parentPanel.remove(childPanel);
editParentLayout();
parentPanel.revalidate();
parentPanel.repaint();