レイアウトでJava swingでUIを作成しようとしています。ラジオボタンと2つのテキストフィールドとボタンが必要です。ラジオボタンの場合は、フローレイアウトを使用し、テキストフィールドとボタンの場合はグループレイアウトを使用しています。しかし、私が得ている出力は散らばっており、テキストフィールドは伸びています。私はこれらのすべてのコンポーネントをウィンドウの中央に置いておきたい。以下は私のコードです。GroupLayoutとFlowLayoutを併用する:OutPutがセンターに表示されない
package DecodeTool1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingLayoutDemo {
private JFrame mainFrame;
private JLabel file_1;
private JLabel file_2;
private JPanel RPanel;
private JPanel TextPanel;
private JPanel panel;
public SwingLayoutDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();
swingLayoutDemo.showGroupLayoutDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 2,20,10));
file_1 = new JLabel("",JLabel.CENTER);
file_2 = new JLabel("",JLabel.CENTER);
file_2.setSize(100,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
panel = new JPanel();
TextPanel = new JPanel();
mainFrame.add(file_1);
mainFrame.add(file_2);
mainFrame.setVisible(true);
}
private void showGroupLayoutDemo(){
RPanel = new JPanel();
RPanel.setSize(50,50);
RPanel.setBounds(150, 0, 50, 50);
TextPanel = new JPanel();
TextPanel.setSize(50,50);
JRadioButton r1=new JRadioButton("Airport");
JRadioButton r2 = new JRadioButton("Apex");
JRadioButton r3 = new JRadioButton("IN");
ButtonGroup bg=new ButtonGroup();
FlowLayout layoutFL = new FlowLayout();
layoutFL.setAlignment(FlowLayout.CENTER);
RPanel.setLayout(layoutFL);
layoutFL.setHgap(15);
RPanel.add(bg);
RPanel.add(comboApex);
RPanel.add(comboINAV);
file_1.setText("File1");
file_2.setText("File2");
JTextField text1 = new JTextField(10);
JTextField text2 = new JTextField(10);
text1.setSize(15, 5);
text2.setSize(15,5);
GroupLayout layout = new GroupLayout(TextPanel);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
JButton btn1 = new JButton("Browse");
JButton btn2 = new JButton("Browse");
layout.setHorizontalGroup(layout.createSequentialGroup()
// .addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(file_1)
.addComponent(file_2))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(text1)
.addComponent(text2))
.addGroup(layout.createParallelGroup(
GroupLayout.Alignment.CENTER)
.addComponent(btn1))
.addComponent(btn2)
.addGroup(layout.createSequentialGroup()
.addComponent(btn3)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(file_1)
.addComponent(text1)
.addComponent(btn1))
.addComponent(btn3)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(file_2)
.addComponent(text2)
.addComponent(btn2)));
TextPanel.setLayout(layout);
mainFrame.add(RPanel);
mainFrame.add(TextPanel);
mainFrame.add(panel);
mainFrame.pack();
mainFrame.setVisible(true);
}
}
1)は、コード行とブロックのインデントの論理的かつ一貫した形式を使用。インデントは、コードの流れをより簡単にするためのものです。 2)ソースコード内の空白の空白行が1つ必要です。 '{'の前または '}'の前の空白行も通常は冗長です。 3)ASCIIアート、またはGUIの*意図された*レイアウトの簡単な図面を最小サイズで提供し、サイズ変更可能な場合は幅と高さを増やします。 –
変数名は大文字で始まる必要があります。 – camickr