package EnlargeCircle;
import javax.swing.*;
import java.awt.*;
public class ControlCircle1 extends JFrame {
private JButton jbtEnlarge=new JButton();
private JButton jbtShrink=new JButton();
private ControlPanel canvas=new ControlPanel();
public ControlCircle1(){
JPanel panel=new JPanel();
panel.add(jbtEnlarge);
panel.add(jbtShrink);
this.add(canvas,BorderLayout.CENTER);
this.add(panel,BorderLayout.SOUTH);
}
public static void main(String[] agrs){
JFrame frame=new JFrame();
frame.setTitle("ControlCircle");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class ControlPanel extends JPanel{
private int radius=5;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawOval(getWidth()/2-radius, getHeight()/2-radius,
2*radius, 2*radius);
}
}
运行完之后,只显示窗口,没有里面的内容,这是为什么。(本人新手)
------解决方案--------------------
我也是新手,还没学到swing 不过这里看到 this之指代的是调用该方法的对象,而不是抽口对象,所以要改成frame 但是这样编译时通不过的 因为frame还没有创建 所以我把程序改成这样就可以正常显示了。
import javax.swing.*;
import java.awt.*;
public class gggg extends JFrame {
private JButton jbtEnlarge=new JButton();
private JButton jbtShrink=new JButton();
private ControlPanel canvas=new ControlPanel();
public void init(){
JFrame frame=new JFrame();
frame.setTitle("ControlCircle");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
JPanel panel=new JPanel();
panel.add(jbtEnlarge);
panel.add(jbtShrink);
frame.add(canvas,BorderLayout.CENTER);
frame.add(panel,BorderLayout.SOUTH);
frame.setVisible(true);
}
public static void main(String[] agrs){
new gggg().init();
}
}
class ControlPanel extends JPanel{
private int radius=5;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawOval(getWidth()/2-radius, getHeight()/2-radius,
2*radius, 2*radius);
}
}