您可以使用 windowBuilder eclipse 插件,它有助于以您想要的方式形成布局。例如,我使用 windowbuilder 创建了一个简单的布局,它以这种方式形成代码
import java.awt.Color;import java.awt.EventQueue;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Alignment;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JProgressBar;import javax.swing.JTextField;import javax.swing.LayoutStyle.ComponentPlacement;import javax.swing.SwingConstants;import javax.swing.border.EmptyBorder;import javax.swing.event.MenuEvent;import javax.swing.event.MenuListener;public class LoginFrame2 extends JFrame implements ActionListener,MenuListener{private static final long serialVersionUID=1L;private JPanel contentPane;private JTextField userNameTextField;private JPasswordField passwordField;private JButton btnChangePassword;private JProgressBar progressBar;private JCheckBox chckbxShowPassword;private static final Color GREEN_COLOR=new Color(107,142,35);private static final Color RED_COLOR=new Color(255,0,0);private JLabel lblStatusmessage,lblProgressstatus;private JPanel footerPanel;private JLabel lblFooter;private JMenuBar menuBar;private JMenu mnConfigure;public static void main(String[]args){EventQueue.invokeLater(new Runnable(){public void run(){try{LoginFrame2 frame=new LoginFrame2();frame.setVisible(true);}catch(Exception e){e.printStackTrace();}}});}
public LoginFrame2(){setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,455,350);menuBar=new JMenuBar();setJMenuBar(menuBar);mnConfigure=new JMenu("Configure");mnConfigure.addMenuListener(this);menuBar.add(mnConfigure);contentPane=new JPanel();contentPane.setBorder(new EmptyBorder(5,5,5,5));setContentPane(contentPane);JPanel topPanel=new JPanel();JPanel centerPanel=new JPanel();JPanel bottomPanel=new JPanel();footerPanel=new JPanel();GroupLayout gl_contentPane=new GroupLayout(contentPane);gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING,gl_contentPane.createSequentialGroup().addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addComponent(bottomPanel,GroupLayout.DEFAULT_SIZE,438,Short.MAX_VALUE).addComponent(footerPanel,GroupLayout.PREFERRED_SIZE,438,Short.MAX_VALUE).addComponent(topPanel,Alignment.LEADING,GroupLayout.DEFAULT_SIZE,438,Short.MAX_VALUE).addComponent(centerPanel,Alignment.LEADING,GroupLayout.DEFAULT_SIZE,438,Short.MAX_VALUE)).addContainerGap()));gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addComponent(topPanel,GroupLayout.PREFERRED_SIZE,37,GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.RELATED).addComponent(centerPanel,GroupLayout.DEFAULT_SIZE,154,Short.MAX_VALUE).addPreferredGap(ComponentPlacement.RELATED).addComponent(bottomPanel,GroupLayout.PREFERRED_SIZE,61,GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.RELATED).addComponent(footerPanel,GroupLayout.PREFERRED_SIZE,14,GroupLayout.PREFERRED_SIZE).addGap(1)));lblFooter=new JLabel("Developer : KEA3");lblFooter.setHorizontalAlignment(SwingConstants.RIGHT);lblFooter.setFont(new Font("Tahoma",Font.PLAIN,8));GroupLayout gl_footerPanel=new GroupLayout(footerPanel);gl_footerPanel.setHorizontalGroup(gl_footerPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_footerPanel.createSequentialGroup().addGap(100).addComponent(lblFooter,GroupLayout.PREFERRED_SIZE,338,GroupLayout.PREFERRED_SIZE).addContainerGap(GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)));gl_footerPanel.setVerticalGroup(gl_footerPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_footerPanel.createSequentialGroup().addComponent(lblFooter,GroupLayout.PREFERRED_SIZE,14,GroupLayout.PREFERRED_SIZE).addContainerGap(GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)));footerPanel.setLayout(gl_footerPanel);progressBar=new JProgressBar();lblProgressstatus=new JLabel("progressStatus");lblProgressstatus.setFont(new Font("Tahoma",Font.BOLD,11));lblProgressstatus.setHorizontalAlignment(SwingConstants.CENTER);GroupLayout gl_bottomPanel=new GroupLayout(bottomPanel);gl_bottomPanel.setHorizontalGroup(gl_bottomPanel.createParallelGroup(Alignment.TRAILING).addGroup(gl_bottomPanel.createSequentialGroup().addContainerGap().addComponent(lblProgressstatus,GroupLayout.DEFAULT_SIZE,412,Short.MAX_VALUE).addContainerGap()).addComponent(progressBar,Alignment.LEADING,GroupLayout.DEFAULT_SIZE,438,Short.MAX_VALUE));gl_bottomPanel.setVerticalGroup(gl_bottomPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_bottomPanel.createSequentialGroup().addContainerGap().addComponent(progressBar,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.RELATED).addComponent(lblProgressstatus).addContainerGap(GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)));bottomPanel.setLayout(gl_bottomPanel);JLabel lblAlias=new JLabel("Alias :");lblAlias.setFont(new Font("Tahoma",Font.BOLD,11));lblAlias.setHorizontalAlignment(SwingConstants.RIGHT);JLabel lblPassword=new JLabel("Password :");lblPassword.setFont(new Font("Tahoma",Font.BOLD,11));lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);userNameTextField=new JTextField();userNameTextField.setColumns(10);passwordField=new JPasswordField();chckbxShowPassword=new JCheckBox("Show Password");chckbxShowPassword.setFont(new Font("Tahoma",Font.BOLD,11));btnChangePassword=new JButton("Change Password");GroupLayout gl_centerPanel=new GroupLayout(centerPanel);gl_centerPanel.setHorizontalGroup(gl_centerPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_centerPanel.createSequentialGroup().addGroup(gl_centerPanel.createParallelGroup(Alignment.TRAILING,false).addComponent(lblPassword,Alignment.LEADING,GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addGroup(Alignment.LEADING,gl_centerPanel.createSequentialGroup().addContainerGap().addComponent(lblAlias,GroupLayout.PREFERRED_SIZE,151,GroupLayout.PREFERRED_SIZE))).addPreferredGap(ComponentPlacement.UNRELATED).addGroup(gl_centerPanel.createParallelGroup(Alignment.LEADING).addComponent(btnChangePassword).addComponent(chckbxShowPassword).addComponent(passwordField,GroupLayout.DEFAULT_SIZE,251,Short.MAX_VALUE).addComponent(userNameTextField,GroupLayout.DEFAULT_SIZE,251,Short.MAX_VALUE)).addContainerGap()));gl_centerPanel.setVerticalGroup(gl_centerPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_centerPanel.createSequentialGroup().addGap(21).addGroup(gl_centerPanel.createParallelGroup(Alignment.BASELINE).addComponent(lblAlias).addComponent(userNameTextField,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.UNRELATED).addGroup(gl_centerPanel.createParallelGroup(Alignment.BASELINE).addComponent(lblPassword).addComponent(passwordField,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)).addGap(18).addComponent(chckbxShowPassword).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(btnChangePassword).addContainerGap(15,Short.MAX_VALUE)));btnChangePassword.addActionListener(this);centerPanel.setLayout(gl_centerPanel);lblStatusmessage=new JLabel("Status Message");lblStatusmessage.setFont(new Font("Tahoma",Font.BOLD|Font.ITALIC,13));lblStatusmessage.setHorizontalAlignment(SwingConstants.CENTER);JLabel lblHeader=new JLabel("Header\r\n");lblHeader.setFont(new Font("Tahoma",Font.BOLD,13));lblHeader.setHorizontalAlignment(SwingConstants.CENTER);GroupLayout gl_topPanel=new GroupLayout(topPanel);gl_topPanel.setHorizontalGroup(gl_topPanel.createParallelGroup(Alignment.TRAILING).addGroup(Alignment.LEADING,gl_topPanel.createSequentialGroup().addGap(10).addGroup(gl_topPanel.createParallelGroup(Alignment.LEADING).addComponent(lblStatusmessage,Alignment.TRAILING,GroupLayout.DEFAULT_SIZE,428,Short.MAX_VALUE).addComponent(lblHeader,Alignment.TRAILING,GroupLayout.DEFAULT_SIZE,428,Short.MAX_VALUE)).addContainerGap()));gl_topPanel.setVerticalGroup(gl_topPanel.createParallelGroup(Alignment.TRAILING).addGroup(Alignment.LEADING,gl_topPanel.createSequentialGroup().addComponent(lblHeader).addPreferredGap(ComponentPlacement.RELATED,7,Short.MAX_VALUE).addComponent(lblStatusmessage)));topPanel.setLayout(gl_topPanel);contentPane.setLayout(gl_contentPane);lblStatusmessage.setVisible(false);lblProgressstatus.setVisible(false);progressBar.setVisible(false);}@Override
public void actionPerformed(ActionEvent e){System.out.println("Ding"+e.getActionCommand());}@Override
public void menuCanceled(MenuEvent arg0){}@Override
public void menuDeselected(MenuEvent arg0){}@Override
public void menuSelected(MenuEvent arg0){new ConfigureDialog(this).setVisible(true);}}
你甚至可以从现有的 jframe 打开一个弹出窗口
import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Alignment;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.LayoutStyle.ComponentPlacement;import javax.swing.border.EmptyBorder;import javax.swing.filechooser.FileFilter;import javax.swing.filechooser.FileNameExtensionFilter;public class ConfigureDialog extends JDialog implements ActionListener{private static final long serialVersionUID=1L;private final JPanel contentPanel=new JPanel();private JTextField driverPathTextField;private JLabel lblDriverPath;private JButton btnBrowse;public static void main(String[]args){try{ConfigureDialog dialog=new ConfigureDialog(new JFrame());dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);dialog.setVisible(true);}catch(Exception e){e.printStackTrace();}}
public ConfigureDialog(JFrame parent){super(parent,"",true);if(parent!=null){Dimension parentSize=parent.getSize();Point p=parent.getLocation();setLocation(p.x+parentSize.width+100,p.y+parentSize.height/1);}
setBounds(100,100,479,141);getContentPane().setLayout(new BorderLayout());contentPanel.setBorder(new EmptyBorder(5,5,5,5));getContentPane().add(contentPanel,BorderLayout.CENTER);{lblDriverPath=new JLabel("Driver Path : ");}
{driverPathTextField=new JTextField(System.getProperty("web.ie.driver"));driverPathTextField.setColumns(10);}
btnBrowse=new JButton("Browse");GroupLayout gl_contentPanel=new GroupLayout(contentPanel);gl_contentPanel.setHorizontalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addContainerGap().addComponent(lblDriverPath).addPreferredGap(ComponentPlacement.RELATED).addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addComponent(btnBrowse).addComponent(driverPathTextField,GroupLayout.DEFAULT_SIZE,207,Short.MAX_VALUE)).addContainerGap()));gl_contentPanel.setVerticalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addGap(5).addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE).addComponent(driverPathTextField,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE).addComponent(lblDriverPath)).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(btnBrowse).addContainerGap(21,Short.MAX_VALUE)));contentPanel.setLayout(gl_contentPanel);{JPanel buttonPane=new JPanel();buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));getContentPane().add(buttonPane,BorderLayout.SOUTH);{JButton okButton=new JButton("OK");okButton.setActionCommand("OK");okButton.addActionListener(this);buttonPane.add(okButton);getRootPane().setDefaultButton(okButton);}
{JButton cancelButton=new JButton("Cancel");cancelButton.setActionCommand("Cancel");cancelButton.addActionListener(this);buttonPane.add(cancelButton);}}
btnBrowse.addActionListener(this);}@Override
public void actionPerformed(ActionEvent e){if("Cancel".contains(e.getActionCommand())){dispose();}else if("Browse".contains(e.getActionCommand())){JFileChooser fileopen=new JFileChooser();FileFilter filter=new FileNameExtensionFilter("exe file","exe");fileopen.addChoosableFileFilter(filter);fileopen.setAcceptAllFileFilterUsed(false);fileopen.setFileFilter(filter);fileopen.setFileSelectionMode(JFileChooser.FILES_ONLY);int ret=fileopen.showOpenDialog(this);if(ret==JFileChooser.APPROVE_OPTION){File file=fileopen.getSelectedFile();System.out.println(file);driverPathTextField.setText(file.getPath());}}else if("OK".contains(e.getActionCommand())){System.setProperty("web.ie.driver",driverPathTextField.getText());dispose();}}}
我知道这篇文章更长了......如果有人想要关于这个的信息,我的回答可能会有所帮助......;)