布局管理器

小呆阿术 2014-12-07 08:42:26
请问这个应该怎么布局啊,给出代码实例
...全文
177 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
深山老叔 2014-12-12
  • 打赏
  • 举报
回复
楼主可以看一下jgoodies 的formlayout ,非常灵活的布局管理器
anshiny 2014-12-11
  • 打赏
  • 举报
回复
import javax.swing.JPanel;
import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Test extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel jPane = null;
	private JPanel jP = null;
	private JPanel jPB = null;
	private JLabel jLbl1 = null;
	private JLabel jLbl2 = null;
	private JLabel jLbl3 = null;
	private JLabel jLbl4 = null;
	private JLabel jLbl5 = null;
	private JLabel jLbl6 = null;
	private JLabel jLbl7 = null;
	private JLabel jLbl8 = null;
	
	private JButton jBtn1 = null;
	private JButton jBtn2 = null;
	private JButton jBtn3 = null;
	private JButton jBtn4 = null;
	
	private JComboBox jComboBox = null;
	private JTextField jTF1 = null;
	private JTextField jTF2 = null;
	private JTextField jTF3 = null;
	private JTextField jTF4 = null;
	private JTextField jTF5 = null;
	private JTextField jTF6 = null;
	private JTextField jTF7 = null;
	

	public Test() {
		super();
		initialize();
	}

	private void initialize() {
		jLbl1 = new JLabel("PUBLISHERS INFOMATION");
		jLbl2 = new JLabel("Publishers ID:");
		jLbl3 = new JLabel("Publishers Name:");
		jLbl4 = new JLabel("Phone Number:");
		jLbl5 = new JLabel("Address:");
		jLbl6 = new JLabel("City:");
		jLbl7 = new JLabel("State:");
		jLbl8 = new JLabel("Zip:");
		jTF1 = new JTextField(20);
		jTF2 = new JTextField(20);
		jTF3 = new JTextField(20);
		jTF4 = new JTextField(20);
		jTF5 = new JTextField(20);
		jTF6 = new JTextField(20);
		jTF7 = new JTextField(20);
		jComboBox = new JComboBox();
		jComboBox.addItem(new String("20001"));
		jP = new JPanel();
		jP.setLayout(new GridLayout(7, 3, 5, 5));
		jP.add(jLbl2);
		jP.add(jComboBox);
		jP.add(jTF1);
		jP.add(jLbl3);
		jP.add(new JLabel(""));
		jP.add(jTF2);
		jP.add(jLbl4);
		jP.add(new JLabel(""));
		jP.add(jTF3);
		jP.add(jLbl5);
		jP.add(new JLabel(""));
		jP.add(jTF4);
		jP.add(jLbl6);
		jP.add(new JLabel(""));
		jP.add(jTF5);
		jP.add(jLbl7);
		jP.add(new JLabel(""));
		jP.add(jTF6);
		jP.add(jLbl8);
		jP.add(new JLabel(""));
		jP.add(jTF7);
		jBtn1 = new JButton("Insert");
		jBtn2 = new JButton("Update");
		jBtn3 = new JButton("Clear");
		jBtn4 = new JButton("Exit");
		jPB = new JPanel();
		jPB.add(jBtn1);
		jPB.add(jBtn2);
		jPB.add(jBtn3);
		jPB.add(jBtn4);
		jPane = new JPanel();
		jPane.setBorder(new EmptyBorder(50, 50, 50, 50));
		jPane.setLayout(new BorderLayout());
		jPane.add(jP);
		jPane.add(jPB, BorderLayout.SOUTH);
		this.add(jPane);
		this.setTitle("PUBLISHERS");
		this.setSize(200, 100);		
		this.pack();		
		this.setVisible(true);
	}

	public static void main(String[] args){
		Test t = new Test();
	}
}
anshiny 2014-12-11
  • 打赏
  • 举报
回复
import javax.swing.JPanel;
import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Test extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel jPane = null;
	private JPanel jP = null;
	private JPanel jPB = null;
	private JLabel jLbl1 = null;
	private JLabel jLbl2 = null;
	private JLabel jLbl3 = null;
	private JLabel jLbl4 = null;
	private JLabel jLbl5 = null;
	private JLabel jLbl6 = null;
	private JLabel jLbl7 = null;
	private JLabel jLbl8 = null;
	
	private JButton jBtn1 = null;
	private JButton jBtn2 = null;
	private JButton jBtn3 = null;
	private JButton jBtn4 = null;
	
	private JComboBox jComboBox = null;
	private JTextField jTF1 = null;
	private JTextField jTF2 = null;
	private JTextField jTF3 = null;
	private JTextField jTF4 = null;
	private JTextField jTF5 = null;
	private JTextField jTF6 = null;
	private JTextField jTF7 = null;
	

	public Test() {
		super();
		initialize();
	}

	private void initialize() {
		jLbl1 = new JLabel("PUBLISHERS INFOMATION");
		jLbl2 = new JLabel("Publishers ID:");
		jLbl3 = new JLabel("Publishers Name:");
		jLbl4 = new JLabel("Phone Number:");
		jLbl5 = new JLabel("Address:");
		jLbl6 = new JLabel("City:");
		jLbl7 = new JLabel("State:");
		jLbl8 = new JLabel("Zip:");
		jTF1 = new JTextField(20);
		jTF2 = new JTextField(20);
		jTF3 = new JTextField(20);
		jTF4 = new JTextField(20);
		jTF5 = new JTextField(20);
		jTF6 = new JTextField(20);
		jTF7 = new JTextField(20);
		jComboBox = new JComboBox();
		jComboBox.addItem(new String("20001"));
		jP = new JPanel();
		jP.setLayout(new GridLayout(7, 3, 5, 5));
		jP.add(jLbl2);
		jP.add(jComboBox);
		jP.add(jTF1);
		jP.add(jLbl3);
		jP.add(new JLabel(""));
		jP.add(jTF2);
		jP.add(jLbl4);
		jP.add(new JLabel(""));
		jP.add(jTF3);
		jP.add(jLbl5);
		jP.add(new JLabel(""));
		jP.add(jTF4);
		jP.add(jLbl6);
		jP.add(new JLabel(""));
		jP.add(jTF5);
		jP.add(jLbl7);
		jP.add(new JLabel(""));
		jP.add(jTF6);
		jP.add(jLbl8);
		jP.add(new JLabel(""));
		jP.add(jTF7);
		jBtn1 = new JButton("Insert");
		jBtn2 = new JButton("Update");
		jBtn3 = new JButton("Clear");
		jBtn4 = new JButton("Exit");
		jPB = new JPanel();
		jPB.add(jBtn1);
		jPB.add(jBtn2);
		jPB.add(jBtn3);
		jPB.add(jBtn4);
		jPane = new JPanel();
		jPane.setBorder(new EmptyBorder(50, 50, 50, 50));
		jPane.setLayout(new BorderLayout());
		jPane.add(jP);
		jPane.add(jPB, BorderLayout.SOUTH);
		this.add(jPane);
		this.setTitle("PUBLISHERS");
		this.setSize(200, 100);		
		this.pack();		
		this.setVisible(true);
	}

	public static void main(String[] args){
		Test t = new Test();
	}
}
anshiny 2014-12-11
  • 打赏
  • 举报
回复
import javax.swing.JPanel;
import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Test extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel jPane = null;
	private JPanel jP = null;
	private JPanel jPB = null;
	private JLabel jLbl1 = null;
	private JLabel jLbl2 = null;
	private JLabel jLbl3 = null;
	private JLabel jLbl4 = null;
	private JLabel jLbl5 = null;
	private JLabel jLbl6 = null;
	private JLabel jLbl7 = null;
	private JLabel jLbl8 = null;
	
	private JButton jBtn1 = null;
	private JButton jBtn2 = null;
	private JButton jBtn3 = null;
	private JButton jBtn4 = null;
	
	private JComboBox jComboBox = null;
	private JTextField jTF1 = null;
	private JTextField jTF2 = null;
	private JTextField jTF3 = null;
	private JTextField jTF4 = null;
	private JTextField jTF5 = null;
	private JTextField jTF6 = null;
	private JTextField jTF7 = null;
	

	public Test() {
		super();
		initialize();
	}

	private void initialize() {
		jLbl1 = new JLabel("PUBLISHERS INFOMATION");
		jLbl2 = new JLabel("Publishers ID:");
		jLbl3 = new JLabel("Publishers Name:");
		jLbl4 = new JLabel("Phone Number:");
		jLbl5 = new JLabel("Address:");
		jLbl6 = new JLabel("City:");
		jLbl7 = new JLabel("State:");
		jLbl8 = new JLabel("Zip:");
		jTF1 = new JTextField(20);
		jTF2 = new JTextField(20);
		jTF3 = new JTextField(20);
		jTF4 = new JTextField(20);
		jTF5 = new JTextField(20);
		jTF6 = new JTextField(20);
		jTF7 = new JTextField(20);
		jComboBox = new JComboBox();
		jComboBox.addItem(new String("20001"));
		jP = new JPanel();
		jP.setLayout(new GridLayout(7, 3, 5, 5));
		jP.add(jLbl2);
		jP.add(jComboBox);
		jP.add(jTF1);
		jP.add(jLbl3);
		jP.add(new JLabel(""));
		jP.add(jTF2);
		jP.add(jLbl4);
		jP.add(new JLabel(""));
		jP.add(jTF3);
		jP.add(jLbl5);
		jP.add(new JLabel(""));
		jP.add(jTF4);
		jP.add(jLbl6);
		jP.add(new JLabel(""));
		jP.add(jTF5);
		jP.add(jLbl7);
		jP.add(new JLabel(""));
		jP.add(jTF6);
		jP.add(jLbl8);
		jP.add(new JLabel(""));
		jP.add(jTF7);
		jBtn1 = new JButton("Insert");
		jBtn2 = new JButton("Update");
		jBtn3 = new JButton("Clear");
		jBtn4 = new JButton("Exit");
		jPB = new JPanel();
		jPB.add(jBtn1);
		jPB.add(jBtn2);
		jPB.add(jBtn3);
		jPB.add(jBtn4);
		jPane = new JPanel();
		jPane.setBorder(new EmptyBorder(50, 50, 50, 50));
		jPane.setLayout(new BorderLayout());
		jPane.add(jP);
		jPane.add(jPB, BorderLayout.SOUTH);
		this.add(jPane);
		this.setTitle("PUBLISHERS");
		this.setSize(200, 100);		
		this.pack();		
		this.setVisible(true);
	}

	public static void main(String[] args){
		Test t = new Test();
	}
}
lliiqiang 2014-12-08
  • 打赏
  • 举报
回复
组合布局即可。 先用BoxLayout,然后每一个又是横着排列.
sayhiyang 2014-12-08
  • 打赏
  • 举报
回复
GridBagLayout布局 package com.fxxc.ui; import java.awt.BorderLayout; import javax.swing.JPanel; import javax.swing.JFrame; import java.awt.GridBagLayout; import java.awt.Dimension; import javax.swing.JLabel; import java.awt.GridBagConstraints; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JTextField; import java.awt.Insets; public class Test extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JLabel jLabel = null; private JLabel jLabel1 = null; private JLabel jLabel2 = null; private JLabel jLabel3 = null; private JLabel jLabel4 = null; private JLabel jLabel5 = null; private JLabel jLabel6 = null; private JButton jButton = null; private JButton jButton1 = null; private JButton jButton2 = null; private JButton jButton3 = null; private JComboBox jComboBox = null; private JTextField jTextField = null; private JTextField jTextField1 = null; private JTextField jTextField2 = null; private JTextField jTextField3 = null; private JTextField jTextField4 = null; private JTextField jTextField5 = null; private JTextField jTextField6 = null; private JTextField jTextField7 = null; private JTextField jTextField8 = null; /** * This is the default constructor */ public Test() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(890, 375); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { GridBagConstraints gridBagConstraints81 = new GridBagConstraints(); gridBagConstraints81.fill = GridBagConstraints.VERTICAL; gridBagConstraints81.gridy = 6; gridBagConstraints81.weightx = 1.0; gridBagConstraints81.gridx = 3; GridBagConstraints gridBagConstraints71 = new GridBagConstraints(); gridBagConstraints71.fill = GridBagConstraints.VERTICAL; gridBagConstraints71.gridy = 8; gridBagConstraints71.weightx = 1.0; gridBagConstraints71.gridx = 3; GridBagConstraints gridBagConstraints61 = new GridBagConstraints(); gridBagConstraints61.fill = GridBagConstraints.VERTICAL; gridBagConstraints61.gridy = 7; gridBagConstraints61.weightx = 1.0; gridBagConstraints61.gridx = 3; GridBagConstraints gridBagConstraints51 = new GridBagConstraints(); gridBagConstraints51.fill = GridBagConstraints.VERTICAL; gridBagConstraints51.gridy = 5; gridBagConstraints51.weightx = 1.0; gridBagConstraints51.gridx = 3; GridBagConstraints gridBagConstraints41 = new GridBagConstraints(); gridBagConstraints41.fill = GridBagConstraints.VERTICAL; gridBagConstraints41.gridy = 4; gridBagConstraints41.weightx = 1.0; gridBagConstraints41.gridx = 3; GridBagConstraints gridBagConstraints31 = new GridBagConstraints(); gridBagConstraints31.fill = GridBagConstraints.VERTICAL; gridBagConstraints31.gridy = 2; gridBagConstraints31.weightx = 1.0; gridBagConstraints31.gridx = 3; GridBagConstraints gridBagConstraints21 = new GridBagConstraints(); gridBagConstraints21.fill = GridBagConstraints.VERTICAL; gridBagConstraints21.gridy = 3; gridBagConstraints21.weightx = 1.0; gridBagConstraints21.gridx = 3; GridBagConstraints gridBagConstraints13 = new GridBagConstraints(); gridBagConstraints13.fill = GridBagConstraints.VERTICAL; gridBagConstraints13.gridy = 1; gridBagConstraints13.weightx = 1.0; gridBagConstraints13.gridx = 3; GridBagConstraints gridBagConstraints12 = new GridBagConstraints(); gridBagConstraints12.fill = GridBagConstraints.VERTICAL; gridBagConstraints12.gridy = 0; gridBagConstraints12.weightx = 1.0; gridBagConstraints12.gridx = 3; GridBagConstraints gridBagConstraints11 = new GridBagConstraints(); gridBagConstraints11.fill = GridBagConstraints.VERTICAL; gridBagConstraints11.gridy = 0; gridBagConstraints11.weightx = 1.0; gridBagConstraints11.insets = new Insets(0, 0, 0, 0); gridBagConstraints11.gridx = 2; GridBagConstraints gridBagConstraints10 = new GridBagConstraints(); gridBagConstraints10.gridx = 3; gridBagConstraints10.gridy = 9; GridBagConstraints gridBagConstraints9 = new GridBagConstraints(); gridBagConstraints9.gridx = 2; gridBagConstraints9.gridy = 9; GridBagConstraints gridBagConstraints8 = new GridBagConstraints(); gridBagConstraints8.gridx = 1; gridBagConstraints8.gridy = 9; GridBagConstraints gridBagConstraints7 = new GridBagConstraints(); gridBagConstraints7.gridx = 0; gridBagConstraints7.gridy = 9; GridBagConstraints gridBagConstraints6 = new GridBagConstraints(); gridBagConstraints6.gridx = 0; gridBagConstraints6.gridy = 8; jLabel6 = new JLabel(); jLabel6.setText("Zip"); GridBagConstraints gridBagConstraints5 = new GridBagConstraints(); gridBagConstraints5.gridx = 0; gridBagConstraints5.gridy = 6; jLabel5 = new JLabel(); jLabel5.setText("State:"); GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); gridBagConstraints4.gridx = 0; gridBagConstraints4.gridy = 5; jLabel4 = new JLabel(); jLabel4.setText("City:"); GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); gridBagConstraints3.gridx = 0; gridBagConstraints3.gridy = 4; jLabel3 = new JLabel(); jLabel3.setText("Address:"); GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); gridBagConstraints2.gridx = 0; gridBagConstraints2.gridy = 2; jLabel2 = new JLabel(); jLabel2.setText("Phone Number"); GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); gridBagConstraints1.gridx = 0; gridBagConstraints1.gridy = 1; jLabel1 = new JLabel(); jLabel1.setText("Publishers Name"); GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; jLabel = new JLabel(); jLabel.setText("PublishersID:"); jContentPane = new JPanel(); jContentPane.setLayout(new GridBagLayout()); jContentPane.add(jLabel, gridBagConstraints); jContentPane.add(jLabel1, gridBagConstraints1); jContentPane.add(jLabel2, gridBagConstraints2); jContentPane.add(jLabel3, gridBagConstraints3); jContentPane.add(jLabel4, gridBagConstraints4); jContentPane.add(jLabel5, gridBagConstraints5); jContentPane.add(jLabel6, gridBagConstraints6); jContentPane.add(getJButton(), gridBagConstraints7); jContentPane.add(getJButton1(), gridBagConstraints8); jContentPane.add(getJButton2(), gridBagConstraints9); jContentPane.add(getJButton3(), gridBagConstraints10); jContentPane.add(getJComboBox(), gridBagConstraints11); jContentPane.add(getJTextField(), gridBagConstraints12); jContentPane.add(getJTextField1(), gridBagConstraints13); jContentPane.add(getJTextField2(), gridBagConstraints21); jContentPane.add(getJTextField3(), gridBagConstraints31); jContentPane.add(getJTextField4(), gridBagConstraints41); jContentPane.add(getJTextField5(), gridBagConstraints51); jContentPane.add(getJTextField6(), gridBagConstraints61); jContentPane.add(getJTextField7(), gridBagConstraints71); jContentPane.add(getJTextField8(), gridBagConstraints81); } return jContentPane; } /** * This method initializes jButton * * @return javax.swing.JButton */ private JButton getJButton() { if (jButton == null) { jButton = new JButton(); jButton.setText("Insert"); } return jButton; } /** * This method initializes jButton1 * * @return javax.swing.JButton */ private JButton getJButton1() { if (jButton1 == null) { jButton1 = new JButton(); jButton1.setText("Updae"); } return jButton1; } /** * This method initializes jButton2 * * @return javax.swing.JButton */ private JButton getJButton2() { if (jButton2 == null) { jButton2 = new JButton(); jButton2.setText("Clear"); } return jButton2; } /** * This method initializes jButton3 * * @return javax.swing.JButton */ private JButton getJButton3() { if (jButton3 == null) { jButton3 = new JButton(); jButton3.setText("Exit"); } return jButton3; } /** * This method initializes jComboBox * * @return javax.swing.JComboBox */ private JComboBox getJComboBox() { if (jComboBox == null) { jComboBox = new JComboBox(); } return jComboBox; } /** * This method initializes jTextField * * @return javax.swing.JTextField */ private JTextField getJTextField() { if (jTextField == null) { jTextField = new JTextField(15); } return jTextField; } private JTextField getJTextField1() { if (jTextField1 == null) { jTextField1 = new JTextField(15); } return jTextField1; } private JTextField getJTextField2() { if (jTextField2 == null) { jTextField2 = new JTextField(15); } return jTextField2; } private JTextField getJTextField3() { if (jTextField3 == null) { jTextField3 = new JTextField(15); } return jTextField3; } private JTextField getJTextField4() { if (jTextField4 == null) { jTextField4 = new JTextField(15); } return jTextField4; } private JTextField getJTextField5() { if (jTextField5 == null) { jTextField5 = new JTextField(15); } return jTextField5; } /** * This method initializes jTextField6 * * @return javax.swing.JTextField */ private JTextField getJTextField6() { if (jTextField6 == null) { jTextField6 = new JTextField(15); } return jTextField6; } private JTextField getJTextField7() { if (jTextField7 == null) { jTextField7 = new JTextField(15); } return jTextField7; } private JTextField getJTextField8() { if (jTextField8 == null) { jTextField8 = new JTextField(15); } return jTextField8; } }
小呆阿术 2014-12-07
  • 打赏
  • 举报
回复
引用 2 楼 MarkAsh 的回复:
觉得 采用DridLayout布局就OK
具体怎么实现呢
小呆阿术 2014-12-07
  • 打赏
  • 举报
回复
引用 1 楼 anshiny 的回复:
GridLayout + BorderLayout
能给出大致框架的代码吗,刚学不是太懂
MarkAsh 2014-12-07
  • 打赏
  • 举报
回复
觉得 采用DridLayout布局就OK
anshiny 2014-12-07
  • 打赏
  • 举报
回复
GridLayout + BorderLayout

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧