WriteaJavaprogramtodrawacirclewithinputradius.Defineacomponentsized 200x200fordrawing,andaddittotheframe.Thenaddapaneltotheframe, containingalabel,atextfield,andabutton.Whenthebuttonisclicked,drawthe circleatthecenteroftheself-definedcomponentwithpinkcolor.Showmessage dialogiftheradiusexceeds100,ortheinputradiuscannotbeconvertedtoadouble value.
时间: 2024-03-17 13:47:12 浏览: 20
Sure, here's an example Java program that meets your requirements:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class CircleDrawer extends JFrame {
private JTextField radiusField;
private JButton drawButton;
private JPanel panel;
public CircleDrawer() {
super("Circle Drawer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
// Create components
radiusField = new JTextField(10);
drawButton = new JButton("Draw Circle");
panel = new JPanel();
// Add components to panel
panel.add(new JLabel("Radius: "));
panel.add(radiusField);
panel.add(drawButton);
// Add panel to frame
add(panel, "North");
// Add drawing component to frame
add(new CircleComponent(), "Center");
}
public static void main(String[] args) {
CircleDrawer drawer = new CircleDrawer();
drawer.setVisible(true);
}
private class CircleComponent extends JComponent {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int radius = parseRadius();
if (radius > 0) {
int x = getWidth() / 2 - radius;
int y = getHeight() / 2 - radius;
g.setColor(Color.PINK);
g.fillOval(x, y, radius * 2, radius * 2);
}
}
}
private int parseRadius() {
try {
int radius = Integer.parseInt(radiusField.getText());
if (radius > 100) {
JOptionPane.showMessageDialog(this, "Radius cannot exceed 100");
return -1;
}
return radius;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Invalid radius: " + radiusField.getText());
return -1;
}
}
}
```
This program creates a `JFrame` with a `JPanel` containing a `JLabel`, a `JTextField`, and a `JButton`. When the button is clicked, it attempts to parse the radius from the text field and draws a circle in the center of the frame with the specified radius. If the radius is greater than 100 or cannot be parsed as an integer, it shows a message dialog with an error message. The circle is drawn using a `CircleComponent` that extends `JComponent` and overrides the `paintComponent` method to draw a circle with the specified radius and color.
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)