Swing 中控件添加问题, 求解.

ioio88 2005-03-17 02:46:02
// Swing 中控件添加问题, 求解.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class bb
{
public static void main(String[] args)
{
Test t = new Test("内部类");
t.setSize(200, 200);
t.setVisible(true);
}
}

class Test extends JFrame
{
JPanel panel;
JButton button;

public Test(String str)
{
super(str);
panel = (JPanel)this.getContentPane(); // 这句语句到底是什么意思
// panel并没有被实例化, 它是怎样被添加到Frame上的. 这里的this指的是什么? 是Frame没错吧. 为什么要加 (JPanel)

button = new JButton("well");
// JButton button = new JButton("well"); 为什么会出现运行时异常
panel.add("North", button);
button.addActionListener(new Handle());
}

private class Handle implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
button.setBackground(Color.red); // 外壳类的数据成员可直接访问
panel.setBackground(Color.cyan);
}
}
}


/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class bb
{
public static void main(String[] args)
{
Test t = new Test("匿名类");
t.setSize(200, 200);
t.setVisible(true);
}
}

class Test extends JFrame
{
JPanel panel;
JButton button;

public Test(String str)
{
super(str);
panel = (JPanel)this.getContentPane();

button = new JButton("well");
// JButton button = new JButton("well"); // 为什么这么竟然出现了编译错误
panel.add("North", button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button.setBackground(Color.red);
panel.setBackground(Color.cyan);
}
});
}
}
*/
...全文
119 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ioio88 2005-03-19
  • 打赏
  • 举报
回复
有朋友能帮我解答一下吗, 等好久了
wenchaohu213 2005-03-17
  • 打赏
  • 举报
回复
学习ing

偶也碰到过swing控件的失败

关注。。。。

友情UP一下

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧