【发布时间】:2019-05-11 13:18:31
【问题描述】:
我是 Java 新手,想在单击按钮时画一个圆圈。
到目前为止,我得到了圆圈并显示了按钮,但我似乎无法使按钮工作。
圆圈没有出现。 当我添加另一个 setVisible(true);进入 actionPerformed 按钮将起作用并显示我的圆圈,但另一个按钮将出现在窗口顶部。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class circle extends JFrame{
public circle (){
super("Making a Circle");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout bord = new BorderLayout();
//creating a button
JButton draw = new JButton("draw");
draw.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
circleFrame cf = new circleFrame();
add(cf, BorderLayout.CENTER);
}
});
add(draw, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] arguments){
circle main = new circle();
}
}
class circleFrame extends JPanel{
public circleFrame(){
}
public void paintComponent(Graphics comp){
Graphics2D comp2D = (Graphics2D) comp;
comp2D.setColor(Color.black);
comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Ellipse2D.Float circle = new Ellipse2D.Float(10F, 10F, 100F, 100F);
comp2D.fill(circle);
}
}
【问题讨论】:
-
类名应以大写字符开头。
标签: java swing button paintcomponent