- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class GridBagLayoutDemo
- {
- public GridBagLayoutDemo()
- {
- JFrame j=new JFrame("我的GridBagLayoutDemo面板");
- GridBagLayout g=new GridBagLayout();
- j.getContentPane().setLayout(g);
- GridBagConstraints c;
- int gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
- double weightx,weighty;
- Insets inset;
- JButton b=new JButton("你好呀!");
- gridx=0;
- gridy=0;
- gridwidth=1;
- gridheight=1;
- weightx=10;
- weighty=1;
- anchor=GridBagConstraints.CENTER;
- fill=GridBagConstraints.HORIZONTAL;
- inset=new Insets(0,0,0,0);
- ipadx=0;
- ipady=0;
- c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,fill,inset,ipadx,ipady);
- g.setConstaints(b,c);//为何这有错呢?
- j.getContentPane().add(b);
- j.pack();
- j.setVisible(true);
- j.addWindowListener(new WindowAdapter()
- {
- public void windClosing(WindowEvent e)
- {
- System.exit(0);
- }
- }
- );
- }
- public static void main(String[] args)
- {
- new GridBagLayout();
- }
- }
- <p> </p>
复制代码 设置约束,这里错在哪里?
|