gpt4 book ai didi

java - 按下按钮时画一个圆圈

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:13 25 4
gpt4 key购买 nike

我对 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 - 按下按钮时画一个圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53709079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com