GroupLayoutを使用してJavaでフォームを作成する最も簡単な方法は何ですか?フォームとは、前にラベルが付いたテキストフィールドがあるものを意味します。このようなもの:
4 に答える
グループレイアウトを使用すると、次のことができます。
package foo;
import javax.swing.*;
import java.awt.*;
public class ChangeIpSettingsDialog extends JDialog
{
public ChangeIpSettingsDialog( Frame owner )
{
super( owner, true );
setContentPane( createContent() );
}
private Container createContent()
{
JPanel result = new JPanel();
result.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
// Create the layout
GroupLayout layout = new GroupLayout( result );
result.setLayout( layout );
layout.setAutoCreateGaps( true );
// Create the components we will put in the form
JLabel ipAddressLabel = new JLabel( "IP Address:" );
JTextField ipAddressTextField = new JTextField( 20 );
JLabel subnetLabel = new JLabel( "Subnet:" );
JTextField subnetTextField = new JTextField( 20 );
JLabel gatewayLabel = new JLabel( "Gateway:" );
JTextField gatewayTextField = new JTextField( 20 );
// Horizontally, we want to align the labels and the text fields
// along the left (LEADING) edge
layout.setHorizontalGroup( layout.createSequentialGroup()
.addGroup( layout.createParallelGroup( GroupLayout.Alignment.LEADING )
.addComponent( ipAddressLabel )
.addComponent( subnetLabel )
.addComponent( gatewayLabel ) )
.addGroup( layout.createParallelGroup( GroupLayout.Alignment.LEADING )
.addComponent( ipAddressTextField )
.addComponent( subnetTextField )
.addComponent( gatewayTextField ) )
);
// Vertically, we want to align each label with his textfield
// on the baseline of the components
layout.setVerticalGroup( layout.createSequentialGroup()
.addGroup( layout.createParallelGroup( GroupLayout.Alignment.BASELINE )
.addComponent( ipAddressLabel )
.addComponent( ipAddressTextField ) )
.addGroup( layout.createParallelGroup( GroupLayout.Alignment.BASELINE )
.addComponent( subnetLabel )
.addComponent( subnetTextField ) )
.addGroup( layout.createParallelGroup( GroupLayout.Alignment.BASELINE )
.addComponent( gatewayLabel )
.addComponent( gatewayTextField ) )
);
return result;
}
public static void main( String[] args )
{
ChangeIpSettingsDialog dialog = new ChangeIpSettingsDialog( null );
dialog.pack();
dialog.setVisible( true );
}
}
または、を捨てて、主に...フォームのレイアウトとして設計されたGroupLayout
を使用しFormLayout
ます:-)
NetBeansに同梱されているMatisseと呼ばれるGUIエディタを使用するだけです。これは私が今まで見た中で最も素晴らしいGUIエディターです。それは非常にうまく機能し、あなたがデザインするすべてのウィンドウはサイズ変更可能にすることができます。
このエディターは、GroupLayoutを使用してコードを生成します。
MatisseのクローンもEclipseプラグインとして利用できますが、無料ではないと思います。ここでそれを見てください(免責事項:私はこれまでこのプラグインを使用したことがないので、元のマティスと同じ品質かどうかはわかりません)
http://marketplace.eclipse.org/content/swing-gui-designer
これが素敵なスクリーンショットです:
デモされたレイアウトを次の方法で実現する方法の例GridBagLayout
:
class Main extends JFrame implements Runnable {
JLabel lblIpAddress = new JLabel();
JLabel lblSubnet = new JLabel();
JLabel lblGateway = new JLabel();
JTextField txtIpAddress = new JTextField();
JTextField txtSubnet = new JTextField();
JTextField txtGateway = new JTextField();
public void run() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = this.getContentPane();
lblIpAddress.setText("IP Address");
lblIpAddress.setLabelFor(txtIpAddress);
lblSubnet.setText("Subnet");
lblSubnet.setLabelFor(txtSubnet);
lblGateway.setText("Gateway");
lblGateway.setLabelFor(txtGateway);
GridBagLayout layout = new GridBagLayout();
content.setLayout(layout);
content.add(lblIpAddress, newLabelConstraints());
content.add(txtIpAddress, newTextFieldConstraints());
content.add(lblSubnet, newLabelConstraints());
content.add(txtSubnet, newTextFieldConstraints());
content.add(lblGateway, newLabelConstraints());
content.add(txtGateway, newTextFieldConstraints());
// Add a spacer to push all the form rows to the top of the window.
GridBagConstraints spacer = new GridBagConstraints();
spacer.fill=BOTH;
spacer.gridwidth=REMAINDER;
content.add(new JPanel(), spacer);
// make sure you can't "cut off" the controls when making the window smaller
this.pack();
this.setMinimumSize(this.getSize());
this.setVisible(true);
}
private GridBagConstraints newConstraints() {
GridBagConstraints c = new GridBagConstraints();
// a little breathing room
c.insets = new Insets(2, 2, 2, 2);
return c;
}
private GridBagConstraints newLabelConstraints() {
GridBagConstraints c = newConstraints();
// right-align labels
c.anchor = BASELINE_TRAILING;
// do not grow labels
c.weightx=0.0;
return c;
}
private GridBagConstraints newTextFieldConstraints() {
GridBagConstraints c = newConstraints();
c.anchor = BASELINE;
// grow text fields horizontally
c.weightx=1.0;
c.fill=HORIZONTAL;
// text fields end a row
c.gridwidth=REMAINDER;
return c;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main());
}
}
主な欠点は、言いたい場合は、ボタンが他のボタンと整列しない、右揃えのボタンの行(例:「OK」と「キャンセル」)を下部に追加することです。ネストされたJPanelを使用します。(または、フォームにボタンごとに個別の列を持たせ、テキストフィールドをこれらすべての列と追加のスペーサー列にまたがらせるようなことを行います。これはかなり直感に反し、読みやすさの利点を打ち消します。3番目のMiGLayoutは-パーティグリッドベースのレイアウトマネージャーは、グリッドセルのマージ/スパン、およびマージされたセルの分割を可能にするため、この状況を適切に処理できます。)