Java GUI编程题求解(自己已完成了一部分)
题目:用户在文本框中输入圆的半径,然后随机产生一个坐标位置,在屏幕上画圆,通过其他组件(如:JTextArea)输出圆的直径、周长和面积。另外提供一个按钮,可以清除屏幕,...
题目:用户在文本框中输入圆的半径,然后随机产生一个坐标位置,在屏幕上画圆,通过其他组件(如:JTextArea)输出圆的直径、周长和面积。另外提供一个按钮,可以清除屏幕,重新画圆。
我自己已经写好了一部分,如下面所示。不过只是一小部分,希望有高手帮我完成下!谢谢!
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class RandomCircle {
JFrame frame;
Container contentPane;
JLabel label1, label2, label3, label4;
JTextField textfield;
JPanel panel1, panel2;
public RandomCircle() {
frame = new JFrame("随机画圆");
contentPane = frame.getContentPane();
label1 = new JLabel("输入圆的半径:");
label2 = new JLabel(" ");
label3 = new JLabel(" ");
label4 = new JLabel(" ");
textfield = new JTextField("在此处输入半径");
panel1 = new JPanel();
panel1.setLayout(new GridLayout(1, 2));
panel1.add(label1);
panel1.add(textfield);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(4, 1));
panel2.add(panel1);
panel2.add(label2);
panel2.add(label3);
panel2.add(label4);
textfield.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double radious = Double.parseDouble(textfield.getText());
String str1 = "半径:" + Double.toString(2 * radious);
String str2 = "周长:" + Double.toString(2 * radious * Math.PI);
String str3 = "面积:"
+ Double.toString(Math.PI * radious * radious);
label2.setText(str1);
label3.setText(str2);
label4.setText(str3);
}
});
contentPane.setLayout(new GridLayout(1, 2));
contentPane.add(panel2);
contentPane.add(new JTextArea());
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
RandomCircle obj = new RandomCircle();
}
} 展开
我自己已经写好了一部分,如下面所示。不过只是一小部分,希望有高手帮我完成下!谢谢!
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class RandomCircle {
JFrame frame;
Container contentPane;
JLabel label1, label2, label3, label4;
JTextField textfield;
JPanel panel1, panel2;
public RandomCircle() {
frame = new JFrame("随机画圆");
contentPane = frame.getContentPane();
label1 = new JLabel("输入圆的半径:");
label2 = new JLabel(" ");
label3 = new JLabel(" ");
label4 = new JLabel(" ");
textfield = new JTextField("在此处输入半径");
panel1 = new JPanel();
panel1.setLayout(new GridLayout(1, 2));
panel1.add(label1);
panel1.add(textfield);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(4, 1));
panel2.add(panel1);
panel2.add(label2);
panel2.add(label3);
panel2.add(label4);
textfield.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double radious = Double.parseDouble(textfield.getText());
String str1 = "半径:" + Double.toString(2 * radious);
String str2 = "周长:" + Double.toString(2 * radious * Math.PI);
String str3 = "面积:"
+ Double.toString(Math.PI * radious * radious);
label2.setText(str1);
label3.setText(str2);
label4.setText(str3);
}
});
contentPane.setLayout(new GridLayout(1, 2));
contentPane.add(panel2);
contentPane.add(new JTextArea());
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
RandomCircle obj = new RandomCircle();
}
} 展开
展开全部
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author szmneo
*/
public class DrawCircle {
static JFrame frame = new JFrame();
static DrawPanel p1 = new DrawPanel();
static JLabel l1 = new JLabel("直径:");
static JLabel l2 = new JLabel("周长:");
static JLabel l3 = new JLabel("面积:");
static JTextField tx = new JTextField("输入半径",10);
public static void main(String[] args){
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JButton b1 = new JButton("OK");
JButton b2 = new JButton("Clear");
p2.add(tx);
p2.add(b1);
p2.add(b2);
p3.setLayout(new GridLayout(3,1));
p3.add(l1);
p3.add(l2);
p3.add(l3);
p4.add(p2);
p4.add(p3);
frame.setLayout(new BorderLayout());
frame.add(p1,BorderLayout.CENTER);
frame.add(p4,BorderLayout.SOUTH);
frame.setTitle("Draw Circles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setVisible(true);
b1.addActionListener(new ActionListener() {//添加绘制事件
public void actionPerformed(ActionEvent e) {
DrawAction();
}
});
b2.addActionListener(new ActionListener() {//添加清除事件
public void actionPerformed(ActionEvent e) {
ClearAction();
}
});
}
static void DrawAction(){
float r = 0;
try {
r = Float.parseFloat(tx.getText());
} catch (NumberFormatException numberFormatException) {
tx.setText("请输入数字(0-400)");
}
l1.setText("直径:" + Float.toString(2 * r));
l2.setText("周长:" + Float.toString((float) (2 * r * Math.PI)));
l3.setText("面积:"+ Float.toString((float) (Math.PI * r * r)));
p1.setR(r);
p1.repaint();
frame.repaint();
frame.setVisible(true);
}
static void ClearAction(){
p1.Clear();
p1.repaint();
frame.repaint();
frame.setVisible(true);
}
static class DrawPanel extends JPanel{
int x,y;
float r=0f;
ArrayList CircleData = new ArrayList();
void setR(float r){
this.r=r;
x = (int)(Math.random()*getWidth()-2*r)+1;
if(x<0)x=0;
y = (int)(Math.random()*getHeight()-2*r)+1;
if(y<0)y=0;
Circle circle = new Circle();
circle.setR(r);
circle.setX(x);
circle.setY(y);
CircleData.add(circle);//添加一个圆
}
void Clear(){
CircleData = new ArrayList();//清空列表
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for(int i=0;i<CircleData.size();i++){//画每个圆
int a =((Circle)CircleData.get(i)).getX();
int b =((Circle)CircleData.get(i)).getY();
int d =(int)(2*((Circle)CircleData.get(i)).getR());
g.drawArc(a, b, d, d, 0, 360);
}
}
}
}
class Circle {//定义每个圆的信息
int x,y;
float r=0f;
void setR(float r){
this.r=r;
}
void setX(int x){
this.x=x;
}
void setY(int y){
this.y=y;
}
float getR(){
return r;
}
int getX(){
return x;
}
int getY(){
return y;
}
}
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author szmneo
*/
public class DrawCircle {
static JFrame frame = new JFrame();
static DrawPanel p1 = new DrawPanel();
static JLabel l1 = new JLabel("直径:");
static JLabel l2 = new JLabel("周长:");
static JLabel l3 = new JLabel("面积:");
static JTextField tx = new JTextField("输入半径",10);
public static void main(String[] args){
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JButton b1 = new JButton("OK");
JButton b2 = new JButton("Clear");
p2.add(tx);
p2.add(b1);
p2.add(b2);
p3.setLayout(new GridLayout(3,1));
p3.add(l1);
p3.add(l2);
p3.add(l3);
p4.add(p2);
p4.add(p3);
frame.setLayout(new BorderLayout());
frame.add(p1,BorderLayout.CENTER);
frame.add(p4,BorderLayout.SOUTH);
frame.setTitle("Draw Circles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setVisible(true);
b1.addActionListener(new ActionListener() {//添加绘制事件
public void actionPerformed(ActionEvent e) {
DrawAction();
}
});
b2.addActionListener(new ActionListener() {//添加清除事件
public void actionPerformed(ActionEvent e) {
ClearAction();
}
});
}
static void DrawAction(){
float r = 0;
try {
r = Float.parseFloat(tx.getText());
} catch (NumberFormatException numberFormatException) {
tx.setText("请输入数字(0-400)");
}
l1.setText("直径:" + Float.toString(2 * r));
l2.setText("周长:" + Float.toString((float) (2 * r * Math.PI)));
l3.setText("面积:"+ Float.toString((float) (Math.PI * r * r)));
p1.setR(r);
p1.repaint();
frame.repaint();
frame.setVisible(true);
}
static void ClearAction(){
p1.Clear();
p1.repaint();
frame.repaint();
frame.setVisible(true);
}
static class DrawPanel extends JPanel{
int x,y;
float r=0f;
ArrayList CircleData = new ArrayList();
void setR(float r){
this.r=r;
x = (int)(Math.random()*getWidth()-2*r)+1;
if(x<0)x=0;
y = (int)(Math.random()*getHeight()-2*r)+1;
if(y<0)y=0;
Circle circle = new Circle();
circle.setR(r);
circle.setX(x);
circle.setY(y);
CircleData.add(circle);//添加一个圆
}
void Clear(){
CircleData = new ArrayList();//清空列表
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for(int i=0;i<CircleData.size();i++){//画每个圆
int a =((Circle)CircleData.get(i)).getX();
int b =((Circle)CircleData.get(i)).getY();
int d =(int)(2*((Circle)CircleData.get(i)).getR());
g.drawArc(a, b, d, d, 0, 360);
}
}
}
}
class Circle {//定义每个圆的信息
int x,y;
float r=0f;
void setR(float r){
this.r=r;
}
void setX(int x){
this.x=x;
}
void setY(int y){
this.y=y;
}
float getR(){
return r;
}
int getX(){
return x;
}
int getY(){
return y;
}
}
展开全部
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class RandomCircle {
JFrame frame;
Container contentPane;
JLabel label1, label2, label3, label4;
JTextField textfield;
JPanel panel1, panel2;
public RandomCircle() {
frame = new JFrame("随机画圆");
contentPane = frame.getContentPane();
label1 = new JLabel("输入圆的半径:");
label2 = new JLabel(" ");
label3 = new JLabel(" ");
label4 = new JLabel(" ");
textfield = new JTextField("在此处输入半径");
panel1 = new JPanel();
panel1.setLayout(new GridLayout(1, 2));
panel1.add(label1);
panel1.add(textfield);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(4, 1));
panel2.add(panel1);
panel2.add(label2);
panel2.add(label3);
panel2.add(label4);
final CirclePanel panel3 = new CirclePanel();
textfield.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double radious = Double.parseDouble(textfield.getText());
String str1 = "直径:" + Double.toString(2 * radious);
String str2 = "周长:" + Double.toString(2 * radious * Math.PI);
String str3 = "面积:"
+ Double.toString(Math.PI * radious * radious);
label2.setText(str1);
label3.setText(str2);
label4.setText(str3);
panel3.draw(radious);
}
});
contentPane.setLayout(new GridLayout(1, 2));
contentPane.add(panel2);
contentPane.add(panel3);
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
RandomCircle obj = new RandomCircle();
}
}
class CirclePanel extends JPanel {
int r=0;
public void draw(double radious) {
r = (int)(radious);
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
int x=(int)(Math.random()*(w-2*r));
if(x<0) x=0;
int y=(int)(Math.random()*(h-2*r));
if(y<0) y=0;
g.drawOval(x, y, 2*r, 2*r);
}
}
import java.awt.event.*;
import java.awt.*;
public class RandomCircle {
JFrame frame;
Container contentPane;
JLabel label1, label2, label3, label4;
JTextField textfield;
JPanel panel1, panel2;
public RandomCircle() {
frame = new JFrame("随机画圆");
contentPane = frame.getContentPane();
label1 = new JLabel("输入圆的半径:");
label2 = new JLabel(" ");
label3 = new JLabel(" ");
label4 = new JLabel(" ");
textfield = new JTextField("在此处输入半径");
panel1 = new JPanel();
panel1.setLayout(new GridLayout(1, 2));
panel1.add(label1);
panel1.add(textfield);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(4, 1));
panel2.add(panel1);
panel2.add(label2);
panel2.add(label3);
panel2.add(label4);
final CirclePanel panel3 = new CirclePanel();
textfield.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double radious = Double.parseDouble(textfield.getText());
String str1 = "直径:" + Double.toString(2 * radious);
String str2 = "周长:" + Double.toString(2 * radious * Math.PI);
String str3 = "面积:"
+ Double.toString(Math.PI * radious * radious);
label2.setText(str1);
label3.setText(str2);
label4.setText(str3);
panel3.draw(radious);
}
});
contentPane.setLayout(new GridLayout(1, 2));
contentPane.add(panel2);
contentPane.add(panel3);
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
RandomCircle obj = new RandomCircle();
}
}
class CirclePanel extends JPanel {
int r=0;
public void draw(double radious) {
r = (int)(radious);
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
int x=(int)(Math.random()*(w-2*r));
if(x<0) x=0;
int y=(int)(Math.random()*(h-2*r));
if(y<0) y=0;
g.drawOval(x, y, 2*r, 2*r);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询