所以我在java中的按钮有问题。我通过扩展 JButton 创建了我的按钮,所以我可以让我的所有按钮看起来都一样。现在,我在该按钮的构造函数中添加了一个 ActionListener,ActionListener 根据按钮的文本决定运行什么操作。(使用
String buttonString = e.getActionCommand();
然后使用 switch/case 来检查文本。)
目前,我有它,当我单击一个按钮时,它会添加一个新的 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,以及如何将切换设置为所述按钮。其余的我大概可以自己弄清楚。
我还能提供什么帮助吗?