单击按钮时重新排列组件(按钮还添加了JXCollapsiblePane)

时间:2013-07-03 14:01:31

标签: java swing jbutton swingx

所以我的java中的按钮有问题。我通过扩展JButton来创建我的按钮,所以我可以让所有按钮看起来都一样。现在,我向该按钮的构造函数添加了一个ActionListener,ActionListener根据按钮的文本决定要运行的操作。 (使用

String buttonString = e.getActionCommand(); 

然后使用开关/案例来检查文本。)

目前,我有它,当我点击一个按钮时,它添加了一个新的JXCollapsiblePane,但它被添加到面板的底部。如何将其添加到按钮下方?

基于我的代码的SSCCE,大致相同:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.jdesktop.swingx.JXCollapsiblePane;

public class ActualCode{
    public static JPanel newPanel;
    public static JFrame frame;

    public static void createGUI(){
        JButton newButton= new JButton("TextButton");
        newButton.addActionListener(new ButtonClickActionK());

        JPanel newPanel = new JPanel();
        newPanel.setVisible(true);
        newPanel.setOpaque(false);
        newPanel.setLayout(new BorderLayout());
        newPanel.add(newButton, BorderLayout.NORTH);

        JFrame frame = new JFrame("Command Line Test");
        frame.setLayout(new BorderLayout());
        frame.add(newPanel, BorderLayout.CENTER);

        frame.setSize(new Dimension(720, 480));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }



    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();

            } // public void run() Closing
        }); // SwingUtilities Closing
    }

}

 class ButtonClickActionK extends ActualCode implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();

        switch (str) {


        case "TextButton":
            //TODO Make a JXCollapsiblePane slide out
            System.out.println("Slide out panel not made yet!");
            JXCollapsiblePane testp = new JXCollapsiblePane();
            newPanel.add(testp);
            break;

        default:
            JOptionPane.showMessageDialog(frame, "Erm, this is embarrasing! Something went (horribly) wrong...");
        }
    }
}

堆栈跟踪错误:

Slide out panel not made yet!
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.warlords.warlordsorganizer.ButtonClickActionK.actionPerformed(ActualCode.java:64)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

我遇到的主要问题是如何直接在按钮下添加JXCollapsiblePane,以及如何将切换设置为所述按钮。其余的我可以自己弄清楚。

我可以提供其他任何帮助吗?

1 个答案:

答案 0 :(得分:2)

这是一个片段

  • 正在添加可折叠而无需对面板的引用:只需添加到ActionEvent源的父级
  • 没有扩展任何不相关的内容(对其ActionListener-ness)

请注意,JXCollapsiblePane的父容器必须使用一个总是尊重可折叠的preferredSize的LayoutManager,否则你会在崩溃/展开时获得不可预测的效果

class ButtonClickActionK implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();
        Component source = (Component) e.getSource();

        switch (str) {


        case "TextButton":
            Container parent = source.getParent();
            JXCollapsiblePane testp = new JXCollapsiblePane();
            testp.add(new JLabel("dummy, just to see we are open"));
            parent.add(testp);
            parent.revalidate();
            break;

        default:
            JOptionPane.showMessageDialog(source, 
               "Erm, this is embarrasing! Something went (horribly) wrong...");
        }
    }
}

// usage
JButton newButton= new JButton("TextButton");
newButton.addActionListener(new ButtonClickActionK());
// note: _not_ using a class field
JComponent newPanel = new JPanel(new VerticalLayout());
newPanel.add(newButton);