求教:Java中GridBagLayout布局的使用
小生正在学习Java图形化界面编程,想使用AWT中的GridBagLayout布局类型实现如图所示的布局画面。图中的黑色粗分割线是表示要把界面分为几大块,每块的内容也在图...
小生正在学习Java图形化界面编程,想使用AWT中的GridBagLayout布局类型实现如图所示的布局画面。图中的黑色粗分割线是表示要把界面分为几大块,每块的内容也在图中标出了,图中也用灰色细线划分出了细网格,以显示各大快的比例关系。烦请高手不吝赐教,帮帮在下。
麻烦把代码发到邮箱:jinhoudexiong@163.com
邮件中请注明大侠您的知道ID,方便给分!
谢谢! 展开
麻烦把代码发到邮箱:jinhoudexiong@163.com
邮件中请注明大侠您的知道ID,方便给分!
谢谢! 展开
2个回答
展开全部
你这图哪用得到GridBagLayout,左边一个JPanel,用BoxLayout或Borderlayout就可以了,右边同理,话说右下角那个按钮哪有这样放的,这样已经覆盖那个文本域了。
给个GridBagLayout的代码你自己琢磨琢磨
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class GridBagLayoutTest extends JFrame {
public GridBagLayoutTest(){
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(5,5,5,5);//top,left,bottom,right 逆时针方向
JButton b = new JButton("One");
/*
* 当窗体大小改变时,将额外的空间填充到网格单元中
* weightx为水平方向,weighty为垂直方向
* 数值0.0~1.0,数值越大分配的空间越多
*/
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 2;
add(b,constraints);
b = new JButton("Two");
constraints.gridy++;
add(b,constraints);
b = new JButton("Three");
constraints.anchor = GridBagConstraints.NORTH;//将组件放在某个方向上
constraints.gridx = 2;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 2;
add(b,constraints);
b = new JButton("Four");
constraints.anchor = GridBagConstraints.CENTER;
constraints.gridx = 3;
constraints.gridheight = 1;
constraints.gridwidth = 2;
add(b,constraints);
b = new JButton("Five");
constraints.gridy = 1;
constraints.gridwidth = 2;
add(b,constraints);
JTextField text = new JTextField(40);
constraints.gridx = 0;
constraints.gridy = 2;
//使用REMAINDER可以使该组件无限延长到边缘
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 1;
constraints.weighty = 0.0;
/*
* 如果组件所在的单元格有剩余空间,则组件将自身填充满该单元格
* 当weightx和weighty有分配值时anchor才有效
* 可能的值有
* NONE 默认
* VERTICAL
* HORIZONTAL
* BOTH
*/
constraints.fill = GridBagConstraints.HORIZONTAL;
add(text,constraints);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
//new GridBagLayoutTest();
new A();
}
}
/*
* anchor属性,将组件放在占单元格的某个固定方向上
* 当fill在水平有效时,WEST和EAST对于anchor无效
* 当fill在垂直有效时,NORTH和SOUTH对于anchor无效
* 当weightx和weighty有分配值时anchor才有效
*/
@SuppressWarnings("serial")
class A extends JFrame {
public A(){
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5,5,5,5);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.NORTH;
JButton b = new JButton("N");
add(b,c);
b = new JButton("E");
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.EAST;
add(b,c);
b = new JButton("S");
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.SOUTH;
add(b,c);
b = new JButton("W");
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
add(b,c);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
给个GridBagLayout的代码你自己琢磨琢磨
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class GridBagLayoutTest extends JFrame {
public GridBagLayoutTest(){
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(5,5,5,5);//top,left,bottom,right 逆时针方向
JButton b = new JButton("One");
/*
* 当窗体大小改变时,将额外的空间填充到网格单元中
* weightx为水平方向,weighty为垂直方向
* 数值0.0~1.0,数值越大分配的空间越多
*/
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 2;
add(b,constraints);
b = new JButton("Two");
constraints.gridy++;
add(b,constraints);
b = new JButton("Three");
constraints.anchor = GridBagConstraints.NORTH;//将组件放在某个方向上
constraints.gridx = 2;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 2;
add(b,constraints);
b = new JButton("Four");
constraints.anchor = GridBagConstraints.CENTER;
constraints.gridx = 3;
constraints.gridheight = 1;
constraints.gridwidth = 2;
add(b,constraints);
b = new JButton("Five");
constraints.gridy = 1;
constraints.gridwidth = 2;
add(b,constraints);
JTextField text = new JTextField(40);
constraints.gridx = 0;
constraints.gridy = 2;
//使用REMAINDER可以使该组件无限延长到边缘
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 1;
constraints.weighty = 0.0;
/*
* 如果组件所在的单元格有剩余空间,则组件将自身填充满该单元格
* 当weightx和weighty有分配值时anchor才有效
* 可能的值有
* NONE 默认
* VERTICAL
* HORIZONTAL
* BOTH
*/
constraints.fill = GridBagConstraints.HORIZONTAL;
add(text,constraints);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
//new GridBagLayoutTest();
new A();
}
}
/*
* anchor属性,将组件放在占单元格的某个固定方向上
* 当fill在水平有效时,WEST和EAST对于anchor无效
* 当fill在垂直有效时,NORTH和SOUTH对于anchor无效
* 当weightx和weighty有分配值时anchor才有效
*/
@SuppressWarnings("serial")
class A extends JFrame {
public A(){
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5,5,5,5);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.NORTH;
JButton b = new JButton("N");
add(b,c);
b = new JButton("E");
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.EAST;
add(b,c);
b = new JButton("S");
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.SOUTH;
add(b,c);
b = new JButton("W");
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
add(b,c);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class NewClass extends javax.swing.JFrame {
public NewClass() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea3 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jScrollPane4 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
getContentPane().setLayout(new java.awt.GridBagLayout());
jScrollPane1.setName("jScrollPane1"); // NOI18N
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
public int getSize() {
return strings.length;
}
public Object getElementAt(int i) {
return strings[i];
}
});
jList1.setName("jList1"); // NOI18N
jScrollPane1.setViewportView(jList1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 73;
gridBagConstraints.ipady = 190;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane1, gridBagConstraints);
jScrollPane2.setName("jScrollPane2"); // NOI18N
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jTextArea2.setName("jTextArea2"); // NOI18N
jScrollPane2.setViewportView(jTextArea2);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 251;
gridBagConstraints.ipady = 190;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane2, gridBagConstraints);
jScrollPane3.setName("jScrollPane3"); // NOI18N
jTextArea3.setColumns(20);
jTextArea3.setRows(5);
jTextArea3.setName("jTextArea3"); // NOI18N
jScrollPane3.setViewportView(jTextArea3);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 251;
gridBagConstraints.ipady = 83;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane3, gridBagConstraints);
jButton1.setText("jButton1");
jButton1.setName("jButton1"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
getContentPane().add(jButton1, gridBagConstraints);
jScrollPane4.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane4.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
jScrollPane4.setName("jScrollPane4"); // NOI18N
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setName("jTextArea1"); // NOI18N
jScrollPane4.setViewportView(jTextArea1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
getContentPane().add(jScrollPane4, gridBagConstraints);
jMenuBar1.setName("jMenuBar1"); // NOI18N
jMenu1.setText("File");
jMenu1.setName("jMenu1"); // NOI18N
jMenuItem1.setText("jMenuItem1");
jMenuItem1.setName("jMenuItem1"); // NOI18N
jMenu1.add(jMenuItem1);
jMenuItem2.setText("jMenuItem2");
jMenuItem2.setName("jMenuItem2"); // NOI18N
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenu2.setName("jMenu2"); // NOI18N
jMenuItem3.setText("jMenuItem3");
jMenuItem3.setName("jMenuItem3"); // NOI18N
jMenu2.add(jMenuItem3);
jRadioButtonMenuItem1.setSelected(true);
jRadioButtonMenuItem1.setText("jRadioButtonMenuItem1");
jRadioButtonMenuItem1.setName("jRadioButtonMenuItem1"); // NOI18N
jMenu2.add(jRadioButtonMenuItem1);
jCheckBoxMenuItem1.setSelected(true);
jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");
jCheckBoxMenuItem1.setName("jCheckBoxMenuItem1"); // NOI18N
jMenu2.add(jCheckBoxMenuItem1);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewClass().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
private javax.swing.JList jList1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextArea jTextArea3;
}
我是用ide工具直接弄出来的,这种东西大可用Ide,花太多时间不值得。
public NewClass() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea3 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jScrollPane4 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
getContentPane().setLayout(new java.awt.GridBagLayout());
jScrollPane1.setName("jScrollPane1"); // NOI18N
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
public int getSize() {
return strings.length;
}
public Object getElementAt(int i) {
return strings[i];
}
});
jList1.setName("jList1"); // NOI18N
jScrollPane1.setViewportView(jList1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 73;
gridBagConstraints.ipady = 190;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane1, gridBagConstraints);
jScrollPane2.setName("jScrollPane2"); // NOI18N
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jTextArea2.setName("jTextArea2"); // NOI18N
jScrollPane2.setViewportView(jTextArea2);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 251;
gridBagConstraints.ipady = 190;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane2, gridBagConstraints);
jScrollPane3.setName("jScrollPane3"); // NOI18N
jTextArea3.setColumns(20);
jTextArea3.setRows(5);
jTextArea3.setName("jTextArea3"); // NOI18N
jScrollPane3.setViewportView(jTextArea3);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.ipadx = 251;
gridBagConstraints.ipady = 83;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane3, gridBagConstraints);
jButton1.setText("jButton1");
jButton1.setName("jButton1"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
getContentPane().add(jButton1, gridBagConstraints);
jScrollPane4.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane4.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
jScrollPane4.setName("jScrollPane4"); // NOI18N
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setName("jTextArea1"); // NOI18N
jScrollPane4.setViewportView(jTextArea1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
getContentPane().add(jScrollPane4, gridBagConstraints);
jMenuBar1.setName("jMenuBar1"); // NOI18N
jMenu1.setText("File");
jMenu1.setName("jMenu1"); // NOI18N
jMenuItem1.setText("jMenuItem1");
jMenuItem1.setName("jMenuItem1"); // NOI18N
jMenu1.add(jMenuItem1);
jMenuItem2.setText("jMenuItem2");
jMenuItem2.setName("jMenuItem2"); // NOI18N
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenu2.setName("jMenu2"); // NOI18N
jMenuItem3.setText("jMenuItem3");
jMenuItem3.setName("jMenuItem3"); // NOI18N
jMenu2.add(jMenuItem3);
jRadioButtonMenuItem1.setSelected(true);
jRadioButtonMenuItem1.setText("jRadioButtonMenuItem1");
jRadioButtonMenuItem1.setName("jRadioButtonMenuItem1"); // NOI18N
jMenu2.add(jRadioButtonMenuItem1);
jCheckBoxMenuItem1.setSelected(true);
jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");
jCheckBoxMenuItem1.setName("jCheckBoxMenuItem1"); // NOI18N
jMenu2.add(jCheckBoxMenuItem1);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewClass().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
private javax.swing.JList jList1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextArea jTextArea3;
}
我是用ide工具直接弄出来的,这种东西大可用Ide,花太多时间不值得。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询