站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题如何使用API?

如何使用API?

摘要: 如何使用API?


我看到API的东西都介绍的很详尽,想请教一下

比如:
Frame f=new Frame();
这个Frame构造函数有4种
public JFrame() throws HeadlessException {
super();
frameInit();
}


public JFrame(GraphicsConfiguration gc) {
super(gc);
frameInit();
}

public JFrame(String title) throws HeadlessException {
super(title);
frameInit();
}


public JFrame(String title, GraphicsConfiguration gc) {
super(title, gc);
frameInit();
}
而我对应的是
1Frame f=new Frame();
2Frame f=new Frame(GraphicsConfiguration gc)看了ApI还是不知道这个怎么写
3 Frame f=new Frame("aaa");

我就是想请教一下那些都是列出来的如果继承他们的子类如何写构造函数,我怎么还是不知道我上面第二个例子到底怎么写啊


是根据你传进来的参数决定的,1Frame f=new Frame()没有参数传进,就应调用第一种构造函数,
2Frame f=new Frame(GraphicsConfiguration gc)调用第二种构造函数,
Frame f=new Frame("aaa");调用第二种构造函数,


不好意思上面写错了,

修改:
Frame f=new Frame("aaa");调用第三种构造函数,


如果还有Frame f=new Frame("aaa",GraphicsConfiguration gc);就调用第四种,


import java.awt.*;

import javax.swing.JPanel;

public class Untitled1 extends JPanel
{
public static void main(String arg[])
{ GraphicsConfiguration g;

JPanel p=new JPanel(g );//为何这么写不对呢



}
}



import java.awt.*;

import javax.swing.JPanel;

public class Untitled1 extends JPanel
{
public static void main(String arg[])
{

JPanel p=new JPanel(GraphicsConfiguration g );//这样写才对,



}
}



import java.awt.*;

import javax.swing.JPanel;

public class Untitled1 extends JPanel
{
public static void main(String arg[])
{

JPanel p=new JPanel(GraphicsConfiguration g );//这样写才对,



}
}

这样也不对啊??说g那里有错误


JPanel p=new JPanel(GraphicsConfiguration[] g )这样呢?



JPanel p=new JPanel(GraphicsConfiguration[] g )这样呢?

也不对啊


挖刚查了一下,JPanel的构造方法如下:

JPanel()
JPanel(boolean isDoubleBuffered)
JPanel(LayoutManager layout)
JPanel(LayoutManager layout, boolean isDoubleBuffered)

创建新对象时,参数不是GraphicsConfiguration对象啊,所以肯定还是不行的



如果用JPanel创建新对象,则参数应该是它构造函数中支持的类型的,这里不支持GraphicsConfiguration对象的,所以如果你要用JPanel的话,只能传这三种:空,boolean,LayoutManager

不是任何一个对象都可以的




晕了,我刚写的是Frame的,一下子写成了JPanel,先谢谢你哈,我按照api写了可是还是有问题
import java.awt.*;

import javax.swing.JPanel;

public class Untitled1 extends Frame
{
public static void main(String arg[])
{
Frame f=new Frame(GraphicsConfiguration g);//还是不对




}
}


还有这个
import java.awt.*;

import javax.swing.JPanel;

public class Untitled1 extends JPanel
{
public static void main(String arg[])
{
JPanel p=new JPanel(LayoutManager layout );//layout那里有问题



}
}




我知道了,这样就对了
import java.awt.*;

import javax.swing.JPanel;

public class Untitled1 extends JPanel
{static LayoutManager layout
public static void main(String arg[])
{
JPanel p=new JPanel(layout );



}
}





哦想起来了,你应该再import进LayoutManager,


↑返回目录
前一篇: 找eclipse
后一篇: 新手提问,关于jar文件打包执行的问题,谢谢~