java中setContentPane()的问题
用GridBagLayout排一个JLabel代码如下:importjavax.swing.*;importjava.awt.*;importjava.awt.event...
用GridBagLayout排一个JLabel代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Psword extends JFrame
{
public void buildGrid(GridBagConstraints constraints,int gx,int gy,int gw,int gh,int
wx,int wy,int ac,int fl)
{
constraints.gridx=gx;
constraints.gridy=gy;
constraints.gridwidth=gw;
constraints.gridheight=gh;
constraints.weightx=wx;
constraints.weighty=wy;
constraints.anchor=ac;
constraints.fill=fl;
}
public Psword()
{
super("password test!");
setSize(290,110);
JPanel pane=new JPanel();
GridBagLayout grid = new GridBagLayout();
GridBagConstraints con=new GridBagConstraints();
pane.setLayout(grid);
JLabel l1=new JLabel("用户名:",JLabel.LEFT);
//问题 1:下面的方法后面两个参数可以这样写么?
buildGrid(con,0,0,1,1,10,40,GridBagConstraints.EAST,GridBagConstraints.NONE);
grid.setConstraints(l1,con);
pane.add(l1);
// 问题2: 编译说下面这句有错。。。不懂。
setContentPane(pane);
}
public static void main(String args[])
{
Psword p = new Psword();
Ewindow e=new Ewindow();
p.addWindowListener(e);
p.show();
}
}
class Ewindow extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
问题如上注释处。
各位高手,本人是菜鸟,请多多指教啊。 展开
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Psword extends JFrame
{
public void buildGrid(GridBagConstraints constraints,int gx,int gy,int gw,int gh,int
wx,int wy,int ac,int fl)
{
constraints.gridx=gx;
constraints.gridy=gy;
constraints.gridwidth=gw;
constraints.gridheight=gh;
constraints.weightx=wx;
constraints.weighty=wy;
constraints.anchor=ac;
constraints.fill=fl;
}
public Psword()
{
super("password test!");
setSize(290,110);
JPanel pane=new JPanel();
GridBagLayout grid = new GridBagLayout();
GridBagConstraints con=new GridBagConstraints();
pane.setLayout(grid);
JLabel l1=new JLabel("用户名:",JLabel.LEFT);
//问题 1:下面的方法后面两个参数可以这样写么?
buildGrid(con,0,0,1,1,10,40,GridBagConstraints.EAST,GridBagConstraints.NONE);
grid.setConstraints(l1,con);
pane.add(l1);
// 问题2: 编译说下面这句有错。。。不懂。
setContentPane(pane);
}
public static void main(String args[])
{
Psword p = new Psword();
Ewindow e=new Ewindow();
p.addWindowListener(e);
p.show();
}
}
class Ewindow extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
问题如上注释处。
各位高手,本人是菜鸟,请多多指教啊。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询