gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:55:20 26 4
gpt4 key购买 nike

所以我在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 直接添加到按钮下方,以及如何将切换设置为所述按钮。剩下的我大概可以自己弄清楚。

我还能提供什么帮助吗?

最佳答案

这是一个片段

  • 无需引用面板即可添加可折叠内容:只需添加到 ActionEvent 源的父级即可
  • 不扩展任何不相关的内容(与其 ActionListener 性质)

请注意,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);

关于java - 单击按钮时重新排列组件(按钮还添加了 JXCollapsiblePane),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17450095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com