问题
I'm sorry for not giving much information; I'm new to Java. Please can you help me with this code? I want to align labels and textfields next to one other and all the buttons in a single row. How can i do this?
Here's how I'm creating a GroupLayout:
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
Here's how I'm creating a horizontal group:
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup()
.addGap(40, 40, 40)
.addComponent(jLabel2)
.addComponent(txtitem, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(txtprice, javax.swing.GroupLayout.DEFAULT_SIZE, 100,javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(txtquantity, javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addComponent(txtreorder, javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnupdate)//,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)//,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
.addComponent(btndelete,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
.addComponent(btnload,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
.addComponent(btnadd)//,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
.addComponent(txtreorder)
.addComponent(jLabel5))
)
)
);
Here's how I'm creating a VerticalGroup:
layout.setVerticalGroup(
layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addComponent(txtitem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(txtprice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(txtquantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
// .addComponent(btnadd))
.addGap(40, 40, 40)
.addComponent(btnadd,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnupdate,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btndelete,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnload,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
.addContainerGap()
)
)
);
回答1:
You are never forced to use a single panel for all components.
I want to align labels and textfields next to one other
Maybe use a panel with eight a SpringLayout or GridBagLayout. Read the section from the Swing tutorial on How to Use Layout Managers for more information and examples.
and all the buttons in a single row.
Maybe use a panel with a FlowLayout
.
Then add the first panel to the CENTER of your dialog and the buttons panel to the SOUTH.
回答2:
Something like this:
label1.setBounds(2,2,50,25);
textField1.setBounds(10,2,50,25);
ETC... for each item you want to programmatically place.
You'll want to play around with the (x,y,w,h) coordinates to fit your needs.
来源:https://stackoverflow.com/questions/22843902/i-want-to-align-labels-and-textfields-next-to-one-other-and-all-the-buttons-in-a