在GUI中使用GroupLayout的新功能,但我嘗試使用GroupLayouts來佈局圖形組件,但我做錯了什麼,但不知道該怎麼做。我找不到導致此錯誤的原因。迫切需要幫助。IllegalStateException錯誤。雖然使用GroupLayout()
異常在線程 「主」 異常在線程 「AWT-EventQueue的-0」 java.lang.IllegalStateException:
at javax.swing.GroupLayout.checkComponents(Unknown Source)
at javax.swing.GroupLayout.prepare(Unknown Source)
at javax.swing.GroupLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at java.awt.Container.validateUnconditionally(Unknown Source)
at java.awt.Window.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at AlignTest.main(AlignTest.java:12)
java.lang.IllegalStateException:
這是代碼:
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;enter code here
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class AlignFrame extends JFrame {
private JTextField xTField;
private JTextField yTField;
private JLabel xLabel;
private JLabel yLabel;
private JCheckBox sToGr;
private JCheckBox showGrid;
private JButton okButton;
private JButton cancelButton;
private JButton helpButton;
public AlignFrame()
{
super("Align");
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
xLabel = new JLabel("X: ");
yLabel = new JLabel("Y: ");
xTField = new JTextField("");
yTField = new JTextField("");
sToGr= new JCheckBox("Snap to Grid");
showGrid = new JCheckBox("Show Grid");
okButton = new JButton("OK");
cancelButton = new JButton("Cancel");
helpButton = new JButton("Help");
layout.setAutoCreateContainerGaps(true);
layout.setAutoCreateGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(sToGr)
.addComponent(showGrid)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(xLabel)
.addComponent(yLabel))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(xTField)
.addComponent(yTField))
)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(okButton)
.addComponent(cancelButton)
.addComponent(helpButton))
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(sToGr)
.addComponent(showGrid)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(xLabel)
.addComponent(yLabel))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(okButton)
.addComponent(cancelButton)
.addComponent(helpButton))
);
}
}
####################################################################
############### AlignTest class that contains main #################
####################################################################
import javax.swing.JFrame;
public class AlignTest {
public static void main(String [] args)
{
AlignFrame test = new AlignFrame();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setSize(500, 500);
test.setVisible(true);
}
}
這是關於視覺編輯的奇怪觀察。另一種方法是用手刻意編寫整個用戶界面......真的嗎? –
@RobertHarvey這可能取決於太多的變量,在這裏詳細討論它(世衛組織正在使用WHICH的GUI佈局管理器在哪個程序等)。但是看着提問者的代碼,我認爲這基本上是不可維護的(問題本身至少是一個跡象)。我認爲至少有一個關於如何手動佈局GUI的想法是無害的。當你必須創建許多類似的無聊對話框時,這樣的UI設計師肯定會節省時間,但當你超越這個範圍時,他們(即他們的代碼)可能會很麻煩。 (有疑問,這只是一個意見...) – Marco13