今天尝试开发一个小闹钟,遇到很多问题,首先就是,怎么把组建加入到jFrame 当中,尝试了直接加载,会跑出异常,所以需要把当前的对象通过getContentPane();拿到内容面板;
public class Clock extends JFrame implements Runnable{
JLabel day,hour,minute,second,current,close;
JButton confirm,open;
JTextField music,Day,Hour,Minute,Second;
int h=0,f=0,m=0,r=0;
boolean Fo,foo;
public AudioClip sound1;
int shizhong,fenzhong,miaozhong,riqi;
public Clock(){
Container c=getContentPane();
……………………
……………………
}
这是因为当前类继承了JFrame,所以能够调用getContentPane();这个方法,这个方法属于JFrame类的;
当然还有setContentPane
JFrame 提供了这两个方法:getContentPane 和 setContentPane 就是用于获取和设置其 Content Pane 的。
对JFrame添加组件有两种方式:
1)用getContentPane()方法获得JFrame的内容面板,再对其加入组件:frame.getContentPane().add(childComponent)
2)建立一个Jpanel或JDesktopPane之类的中间容器,把组件添加到容器中,用setContentPane()方法把该容器置为JFrame的内容面板:
JPanel contentPane=new JPanel();
……//把其它组件添加到Jpanel中;
frame.setContentPane(contentPane);
//把contentPane对象设置成为frame的内容面板