如何让容器紧密相连

darcymei 2004-02-06 07:06:34
小弟初学java 。我想做一个像扫雷一样紧密联合在一起的一组button,应该怎么办呢?
用了刚看到的几种布局方法,都不行,有空隙,希望各位帮帮忙。
测试代码如下
//
// test1.java
// test1
//
// Created by Administrator on Thu Jan 01 1970.
// Copyright (c) 1970 __MyCompanyName__. All rights reserved.
//
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test1 {
JPanel [] m_panel=new JPanel[100];
JFrame m_frame;
/* public test1(){
m_frame =new JFrame();
Container contentPane=m_frame.getContentPane();
contentPane.setLayout(new GridLayout(10,10));
for(int i=0;i<100;i++){
m_panel[i]=new JPanel();
m_panel[i].add(new JButton());
contentPane.add(m_panel[i]);
}
m_frame.setTitle("CardLayout");
m_frame.pack();
m_frame.setVisible(true);
m_frame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}*/
public test1(){
m_frame =new JFrame();
Container contentPane=m_frame.getContentPane();
GridBagLayout gridbag=new GridBagLayout();
contentPane.setLayout(gridbag);
int gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
double weightx,weighty;
Insets inset;
GridBagConstraints temp;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++){
gridx=i;
gridy=j;
gridwidth=1;
gridheight=1;
weightx=0;
weighty=0;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=0;
temp=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
m_panel[i+j*10]=new JPanel();
m_panel[i+j*10].add(new JButton());
contentPane.add(m_panel[i+j*10],temp);
}
m_frame.setTitle("CardLayout");
//m_frame.pack();
m_frame.setSize(100,100);
m_frame.setVisible(true);
m_frame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}

public static void main (String args[]) {
// insert code here...
System.out.println("Hello World!");
new test1();
}
}
...全文
11 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复

还没有回复,快来抢沙发~

相关推荐
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2004-02-06 07:06
社区公告
暂无公告