作了一个关于GridBagLayout 布局的小程序,排列四个图片按钮,怎么显示的是普通的按钮呢??图片显示不出来??import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class GridBagLay {
JButton bt_1,bt_2,bt_3,bt_4;
void buildConstraints(GridBagConstraints gbc,int gx,int gy,int gw,int gh,double wx,double wy,int anc,int fil,Insets in,int ipx,int ipy){
gbc.gridx=gx;
gbc.gridy=gy;
gbc.gridwidth=gw;
gbc.gridheight=gh;
gbc.weightx=wx;
gbc.weighty=wy;
gbc.anchor=anc;
gbc.fill=fil;
gbc.insets=in;
gbc.ipadx=ipx;
gbc.ipady=ipy;
}
public GridBagLay(){
JFrame frame=new JFrame("GridBagLayout()!!");
Container con=frame.getContentPane();
GridBagConstraints gbcs=new GridBagConstraints();
GridBagLayout gbl=new GridBagLayout();
con.setLayout(gbl);
Insets inc=new Insets(0,0,0,0);
Icon ic1=new ImageIcon("img/001.gif");
Icon ic2=new ImageIcon("img/002.gif");
Icon ic3=new ImageIcon("img/003.gif");
Icon ic4=new ImageIcon("img/004.gif");
bt_1=new JButton("but1",ic1);
bt_2=new JButton("but2",ic2);
bt_3=new JButton("but3",ic3);
bt_4=new JButton("but4",ic4);
bt_1.setToolTipText("Button1");
bt_2.setToolTipText("Button2");
bt_3.setToolTipText("Button3");
bt_4.setToolTipText("Button4");
/* bt_1=new JButton("button1");
bt_2=new JButton("button2");
bt_3=new JButton("button3");
bt_4=new JButton("button4");*/
con.add(bt_1);
con.add(bt_2);
con.add(bt_3);
con.add(bt_4);
buildConstraints(gbcs,0,0,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,inc,0,0);
gbl.setConstraints(bt_1,gbcs);
buildConstraints(gbcs,1,0,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,inc,0,0);
gbl.setConstraints(bt_2,gbcs);
buildConstraints(gbcs,0,1,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,inc,0,0);
gbl.setConstraints(bt_3,gbcs);
buildConstraints(gbcs,1,1,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,inc,0,0);
gbl.setConstraints(bt_4,gbcs);
frame.setVisible(true);
frame.setSize(300,300);
frame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
}
);
}
public static void main(String args[]){
new GridBagLay();
}
}
本人新手,请各位高手帮帮忙看看!!
import java.awt.event.*;
import javax.swing.*;public class GridBagLay {
JButton bt_1,bt_2,bt_3,bt_4;
void buildConstraints(GridBagConstraints gbc,int gx,int gy,int gw,int gh,double wx,double wy,int anc,int fil,Insets in,int ipx,int ipy){
gbc.gridx=gx;
gbc.gridy=gy;
gbc.gridwidth=gw;
gbc.gridheight=gh;
gbc.weightx=wx;
gbc.weighty=wy;
gbc.anchor=anc;
gbc.fill=fil;
gbc.insets=in;
gbc.ipadx=ipx;
gbc.ipady=ipy;
}
public GridBagLay(){
JFrame frame=new JFrame("GridBagLayout()!!");
Container con=frame.getContentPane();
GridBagConstraints gbcs=new GridBagConstraints();
GridBagLayout gbl=new GridBagLayout();
con.setLayout(gbl);
Insets inc=new Insets(0,0,0,0);
Icon ic1=new ImageIcon("img/001.gif");
Icon ic2=new ImageIcon("img/002.gif");
Icon ic3=new ImageIcon("img/003.gif");
Icon ic4=new ImageIcon("img/004.gif");
bt_1=new JButton("but1",ic1);
bt_2=new JButton("but2",ic2);
bt_3=new JButton("but3",ic3);
bt_4=new JButton("but4",ic4);
bt_1.setToolTipText("Button1");
bt_2.setToolTipText("Button2");
bt_3.setToolTipText("Button3");
bt_4.setToolTipText("Button4");
/* bt_1=new JButton("button1");
bt_2=new JButton("button2");
bt_3=new JButton("button3");
bt_4=new JButton("button4");*/
con.add(bt_1);
con.add(bt_2);
con.add(bt_3);
con.add(bt_4);
buildConstraints(gbcs,0,0,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,inc,0,0);
gbl.setConstraints(bt_1,gbcs);
buildConstraints(gbcs,1,0,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,inc,0,0);
gbl.setConstraints(bt_2,gbcs);
buildConstraints(gbcs,0,1,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,inc,0,0);
gbl.setConstraints(bt_3,gbcs);
buildConstraints(gbcs,1,1,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,inc,0,0);
gbl.setConstraints(bt_4,gbcs);
frame.setVisible(true);
frame.setSize(300,300);
frame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
}
);
}
public static void main(String args[]){
new GridBagLay();
}
}
本人新手,请各位高手帮帮忙看看!!
解决方案 »
- json 转化成java对象要怎么操作?
- 怎样核对两个txt档之间的相同字词呢?
- JTabbedpane 验证不通过时,不切换
- new Double(double) 与 Double.valueof(double) 区别与联系
- 一个问题
- 高分求一个java 中的例子 关于鼠标的拖放问题 解题者50 分送上
- 菜鸟的问题,我应该如何实现搜索一个网页上指定字符串
- 从jdk1.4.2转为jdk1.5的问题:java.lang.NoClassDefFoundError: IllegalName
- 小问题!@!!!!
- 如何使用字节流代替FileItem上传zip等文件
- 在线等库的问题
- windows2003 服务器 mysql使用率100% (java项目)
URL url = getClass().getClassLoader().getResource(path);
Icon ic1=new ImageIcon(url);
path是指你的package路径,如图片在src/com/image/001.gif。
path就是com/image/001.gif.
试下这样能访问不。