import javax.swing.*; public class testFrame extends JFrame { private JLabel jLabel1 = null; private JLabel jLabel2 = null; private JLabel jLabel3 = null; private JLabel jLabel4 = null; private JLabel jLabel5 = null; private JComboBox dataBaseTypeBox = null; private JTextField urlTextField = null; private JTextField dataBaseTextField = null; private JTextField accountTextField = null; private JPasswordField PasswordField = null; private JButton testConnButton = null; private JPanel jContentPane = null; public testFrame() { setTitle("布局管理器"); setSize(320, 350); initialize(); setLayout(); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } /** * 初始化控件 */ private void initialize() { jLabel1 = new JLabel(); jLabel1.setText("数据库类型:"); jLabel2 = new JLabel(); jLabel2.setText("数据库URL:"); jLabel3 = new JLabel(); jLabel3.setText("数据库名:"); jLabel4 = new JLabel(); jLabel4.setText("帐户:"); jLabel5 = new JLabel(); jLabel5.setText("密码:"); dataBaseTypeBox=new JComboBox(); dataBaseTypeBox.addItem("Oracle"); dataBaseTypeBox.addItem("DB2"); urlTextField=new JTextField(); dataBaseTextField=new JTextField(); accountTextField=new JTextField(); PasswordField=new JPasswordField(); testConnButton=new JButton(); testConnButton.setText("测试连接"); jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(dataBaseTypeBox); jContentPane.add(urlTextField); jContentPane.add(dataBaseTextField); jContentPane.add(accountTextField); jContentPane.add(PasswordField); jContentPane.add(testConnButton); jContentPane.add(jLabel1); jContentPane.add(jLabel2); jContentPane.add(jLabel3); jContentPane.add(jLabel4); jContentPane.add(jLabel5); setContentPane(jContentPane); } /** * 显示窗口 */ private void setLayout() { LocationLayout locationLayout=new LocationLayout(); //窗口居中
locationLayout.setCenter(this); //使用默认的左边距25,上边距25
//第0行控件
locationLayout.setPosition(jLabel1, 0, 0,70, 25); locationLayout.setPosition(dataBaseTypeBox, 0, 1, 150, 25); //第1行控件
locationLayout.setPosition(jLabel2, 1, 0, 70, 25); locationLayout.setPosition(urlTextField, 1, 1,150, 25); //第2行控件
locationLayout.setPosition(jLabel3, 2, 0,70, 25); locationLayout.setPosition(dataBaseTextField, 2, 1, 150, 25); //第3行控件
locationLayout.setPosition(jLabel4, 3, 0, 70, 25); locationLayout.setPosition(accountTextField, 3, 1, 150, 25); //第4行控件
locationLayout.setPosition(jLabel5, 4, 0, 70, 25); locationLayout.setPosition(PasswordField, 4, 1, 150, 25); //调整左边距为120
//第5行控件
locationLayout.setLeftDistance(120); locationLayout.setPosition(testConnButton, 5, 0, 90, 25); setVisible(true); } public static void main(String[] arg) { new testFrame(); } }
|