2016-08-04 9 views
0

현재이 클래스에 대한 스프링 유틸리티 클래스가 필요한 작은 애플리케이션을 만들고 있습니다. 콘텐츠 창이므로 'JPanel'을 확장합니다. 용기를 통해 배치의 종류 만이 클래스/프레임 (클래스 = https://docs.oracle.com/javase/tutorial/uiswing/examples/layout/SpringGridProject/src/layout/SpringUtilities.java) 오류 EventQueue의 예외 리턴 스프링 레이아웃 사용된다새로운 프레임을 만들고 SpringUtilities를 사용하여 SpringLayout을 사용하여 꾸미십시오.

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) 

는 I가이

때문이라고 생각을
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); 
    } 

} 

하고, 사전에 감사합니다.

답변

1

먼저 컨테이너에 구성 요소를 추가해야합니다. 그런 다음 기존 구성 요소를 레이아웃하려 할 때 SpringUtilities.makeCompactGrid() 메서드를 적용하십시오. 따라서 add()이 실행 된 후에 을 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.