gpt4 book ai didi

java - 创建一个新框架并使用 SpringUtilities 使用 SpringLayout 对其进行装饰

转载 作者:行者123 更新时间:2023-12-01 09:37:17 26 4
gpt4 key购买 nike

我目前正在制作一个小型应用程序,我需要 Spring Utilities 类,该类扩展“JPanel”,因为它是一个内容 Pane ,但是我需要通过容器获取布局类型,但只有该类/框架使用 spring 布局,它返回 EventQueue 异常(类 = https://docs.oracle.com/javase/tutorial/uiswing/examples/layout/SpringGridProject/src/layout/SpringUtilities.java )错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 0
at java.awt.Container.getComponent(Unknown Source)
at dinges.Utilities.SpringUtilities.getConstraintsForCell(SpringUtilities.java:153)
at dinges.Utilities.SpringUtilities.makeCompactGrid(SpringUtilities.java:190)
at dinges.Containers.Addnew.<init>(Addnew.java:38)
at dinges.Containers.Listeners.AddListener.mousePressed(AddListener.java:27)
at java.awt.AWTEventMulticaster.mousePressed(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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

我认为是这个原因

SpringUtilities.makeCompactGrid(this, 3, 2, 6, 6, 6, 6);

因为“this”不能使用,但我不确定该使用什么。

代码:

    package dinges.Containers;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import dinges.Utilities.SpringUtilities;

@SuppressWarnings("serial")
public class Addnew extends JPanel {

/**
* > Add a text input for the following:
* > Id, Name, last name, current balance, and the state. But this has to be in order of the new Account.
* > we're just going to be using JTextFields, a JButton for saving and JLabels for writing what it is
*
**/

public Addnew() {
// frame size is WIDTH = 280 , HEIGHT = 480
SpringLayout layout = new SpringLayout();
setLayout(layout);

JButton save = new JButton("Save data");
JTextField name = new JTextField(15);
JTextField lastname = new JTextField(15);
JComboBox<String> accounttype = new JComboBox<String>();
JLabel label1 = new JLabel("First name: ", JLabel.TRAILING);
JLabel label2 = new JLabel("Last name: ", JLabel.TRAILING);
JLabel label3 = new JLabel("Account type: ", JLabel.TRAILING);
label1.setLabelFor(name);
label2.setLabelFor(lastname);
label3.setLabelFor(accounttype);

SpringUtilities.makeCompactGrid(this, 3, 2, 6, 6, 6, 6);




add(label1);
add(label2);
add(label3);
add(save);
add(name);
add(lastname);
add(accounttype);
}

}

任何帮助都会很棒,提前致谢。

最佳答案

您需要先将组件添加到容器中。然后应用 SpringUtilities.makeCompactGrid() 方法,因为它尝试布局现有组件。因此,只需在执行所有 add() 后,将 makeCompactGrid() 移动到 Addnew() 构造函数的末尾即可。

顺便说一句,尽管 SpringLayout 非常灵活,但它的级别也非常低,通常由 GUI 构建使用,并且手动编码可能很麻烦。请参阅How to Use SpringLayout了解更多详情:

SpringLayout is a very flexible layout manager that can emulate many of the features of other layout managers. SpringLayout is, however, very low-level and as such you really should only use it with a GUI builder, rather than attempting to code a spring layout manager by hand.

关于java - 创建一个新框架并使用 SpringUtilities 使用 SpringLayout 对其进行装饰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38773360/

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