0

現在、Spring Utilities クラスが必要な小さなアプリケーションを作成しています。このクラスはコンテンツ ペインであるため「JPanel」を拡張しますが、コンテナを介してレイアウトのタイプを取得する必要がありますが、このクラス/フレームのみが使用されていますEventQueue Exception を返すスプリング レイアウト (クラス = 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);
    }

}

どんな助けでも素晴らしいでしょう、そして前もって感謝します。

4

1 に答える 1

1

最初にコンテナにコンポーネントを追加する必要があります。次にSpringUtilities.makeCompactGrid()、既存のコンポーネントをレイアウトしようとしているときにメソッドを適用します。したがって、すべてが実行された後、コンストラクターmakeCompactGrid()の最後に移動するだけです。Addnew()add()

補足として、SpringLayout は非常に柔軟ですが、非常に低レベルでもあり、通常は GUI ビルドで使用され、手作業でコーディングするのは面倒です。詳細については、 SpringLayoutの使用方法を参照してください。

SpringLayout は、他のレイアウト マネージャーの多くの機能をエミュレートできる非常に柔軟なレイアウト マネージャーです。ただし、SpringLayout は非常に低レベルであるため、手動で Spring Layout Manager をコーディングしようとするのではなく、GUI ビルダーでのみ使用する必要があります。

于 2016-08-04T18:39:41.170 に答える