import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ButPanel extends JPanel{
private JButton but;
public ButPanel(){
but = new JButton("but");
this.add(but); }
}//类Practice
public class Practice extends JFrame{
private ButPanel butPanel;
public Practice(){
super("practice");
Container c = getContentPane();
butPanel = new ButPanel();
c.add(butPanel,BorderLayout.CENTER);
setSize(300,300);
show();
}
public static void main(String args[]){
Practice app = new Practice();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}这样写就可以直接运行,看不见按钮是因为初始化JPanel子类的时候,没有把button对象加进去
import java.awt.event.*;
import javax.swing.*;
class ButPanel extends JPanel{
private JButton but;
public ButPanel(){
but = new JButton("but");
this.add(but); }
}//类Practice
public class Practice extends JFrame{
private ButPanel butPanel;
public Practice(){
super("practice");
Container c = getContentPane();
butPanel = new ButPanel();
c.add(butPanel,BorderLayout.CENTER);
setSize(300,300);
show();
}
public static void main(String args[]){
Practice app = new Practice();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}这样写就可以直接运行,看不见按钮是因为初始化JPanel子类的时候,没有把button对象加进去
因为少了把对象加入去。this.add(but);