java - 在按钮按下时画一个圆圈
问题描述
我对 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);
}
}
解决方案
请查看,注意评论,然后运行:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Circle extends JFrame{
private boolean isDraw = false;
private CircleFrame cf;
private JButton draw;
public Circle (){ // see Java Naming Conventions https://www.geeksforgeeks.org/java-naming-conventions
super("Making a Circle");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout bord = new BorderLayout(); //you never use bord
setLayout(bord); //BTW BorderLayout is the default for JFrame content pane
//you do not want to add a new panel with every button click so it
//should NOT be triggered by the action listener
cf = new CircleFrame();
add(cf, BorderLayout.CENTER);
//creating a button
draw = new JButton("Draw");
draw.addActionListener(event -> toggleDrawErase() );
//you also need to add the button
add(draw, BorderLayout.PAGE_END);
pack();
setVisible(true);
}
private void toggleDrawErase() {
isDraw = ! isDraw;
cf.setDraw(isDraw);
draw.setText(isDraw ? "Erase" : "Draw");
repaint();
}
public static void main(String[] arguments){
new Circle();
}
}
class CircleFrame extends JPanel{
private boolean isDraw = false;
public CircleFrame(){
setPreferredSize(new Dimension(250, 300));
}
@Override
public void paintComponent(Graphics comp){
super.paintComponent(comp);
if(! isDraw() ) return;
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);
}
boolean isDraw() { return isDraw; }
void setDraw(boolean isDraw) { this.isDraw = isDraw; }
}
如有需要,请随时要求澄清。
推荐阅读
- java - 使用命令 java -cp 的 java 运行问题
- c - 当符号似乎存在时,为什么 ld、nm、objdump 在此共享对象中报告“无符号”?
- kubernetes - 部署到新实例后,certmanager 不再工作
- python - 在不访问光盘的情况下使用 webbrowser 生成和打开 HTML 字符串
- python - 使用分类值时如何在散点图中保留轴的顺序?
- python - 在熊猫(> 1.0)中,如何将具有缺失值的浮点系列的dtype转换为整数?
- socket.io - 如何使用socketio将推送通知发送到android设备,当它将返回在线时
- c# - 如果不正确,请再次提问,直到正确为止
- airflow - 气流 dag 永远不会被动态 start_date 触发
- c - C 函数变量意外更改