Java – 单击按钮后绘制圆圈

我试图用java中的按钮画一个圆圈。 我将System.out.println()放在action方法中以确保我的代码正常工作。 println显示但在任何地方都没有圆绘图。 有什么建议? 谢谢

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class CircleViewer { public static void main(String[] args) { CircleComponent circle = new CircleComponent(); JButton button = new JButton("Draw"); final JPanel panel = new JPanel(); panel.add(button); JFrame frame = new JFrame(); class addActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { CircleComponent component = new CircleComponent(); String x = JOptionPane.showInputDialog("X Coordinate", "Enter an x coordinate"); int xCoord = Integer.parseInt(x); String y = JOptionPane.showInputDialog("Y Coordinate", "Enter ay coordinate"); int yCoord = Integer.parseInt(y); String width = JOptionPane.showInputDialog("Radius", "Enter the length of the radius"); int radius = Integer.parseInt(width); component.setLocation(xCoord,yCoord); component.getWidth(radius); panel.add(component); System.out.println("CLICKED!"); } } frame.add(panel); ActionListener action = new addActionListener(); button.addActionListener(action); frame.setSize(500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import javax.swing.JButton; import javax.swing.JFrame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JPanel; import javax.swing.JOptionPane; /** This component lets the user draw a circle by clicking a button. */ public class CircleComponent extends JPanel { private int x; private int y; private int width; Ellipse2D.Double circle; public CircleComponent() { circle = new Ellipse2D.Double(x, y, width, width); } public Dimension getPreferredSize() { return new Dimension(500,500); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.draw(circle); } public int getWidth(int aWidth) { width = aWidth; return width; } } 

您需要覆盖自定义组件的getPreferredSize()方法。 默认情况下,大小为(0,0),因此无需绘制任何内容。

revalidate()应该在面板上完成,而不是组件。

你的getX()和getY()方法毫无意义。 如果要在组件上定位组件,则应使用组件的setLocation(…)方法。

编辑:

还有很多问题。 我会尝试解释它们,然后给出一个更好(仍然不是很好)的例子,说明如何做到这一点。

 circle = new Ellipse2D.Double(x, y, width, width); 
  1. 创建圆形时,所有参数的值都为0,因此无需绘制任何内容。 您不能稍后更改变量x,y和宽度的值,并期望圆圈反映这些值。

  2. 组件的大小是错误的。 你不能只做500 x 500的任意大小。首选大小应该是圆的大小。

  3. 您不能只将组件添加到面板,因为默认情况下面板使用FlowLayout。 这意味着将忽略setLocation()方法。 我将代码更改为使用null布局。 这意味着现在将使用您指定的位置,并且还必须指定组件的大小。

  4. 我改变了你的代码,使用ActonListener的“匿名内部类”。 这比在另一个类的中间定义一个类更常见。

这是一个简单的例子:

 import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.awt.*; public class CircleComponent extends JPanel { Ellipse2D.Double circle; public CircleComponent(int radius) { circle = new Ellipse2D.Double(0, 0, radius, radius); setOpaque(false); } public Dimension getPreferredSize() { Rectangle bounds = circle.getBounds(); return new Dimension(bounds.width, bounds.height); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setColor( getForeground() ); g2.fill(circle); } /* public int getWidth(int aWidth) { width = aWidth; return width; } */ public static void main(String[] args) { // Create a panel using a null layout so we can add components at random positions final JPanel center = new JPanel(); center.setLayout(null); JButton button = new JButton("Draw"); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { String x = JOptionPane.showInputDialog("X Coordinate", "Enter an x coordinate"); int xCoord = Integer.parseInt(x); String y = JOptionPane.showInputDialog("Y Coordinate", "Enter ay coordinate"); int yCoord = Integer.parseInt(y); String width = JOptionPane.showInputDialog("Radius", "Enter the length of the radius"); int radius = Integer.parseInt(width); CircleComponent component = new CircleComponent(radius); component.setLocation(xCoord,yCoord); component.setSize(component.getPreferredSize()); center.add(component); center.repaint(); } }); JFrame frame = new JFrame(); frame.add(center, BorderLayout.CENTER); frame.add(button, BorderLayout.NORTH); frame.setSize(500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }