如何将Action事件中的int放入数组中以在Java中创建一个圆?
我在Java中遇到了一个巨大的问题:创建一个从用户那里获取输入并基于该输入创建一个圆圈的GUI。我从用户那里获取输入并保存getUserX、getUserY、getRadius和circleColor中的值,但是我不知道如何将这些变量传递到一个数组中,该数组由paint组件用来创建一个圆 如何正确地获取getUserX、getUserY和getRadius变量的值并将它们放入circleValues数组中?如何从用户输入中获取circleColor并将其作为已创建圆圈的page.setColor值如何将Action事件中的int放入数组中以在Java中创建一个圆?,java,paintcomponent,Java,Paintcomponent,我在Java中遇到了一个巨大的问题:创建一个从用户那里获取输入并基于该输入创建一个圆圈的GUI。我从用户那里获取输入并保存getUserX、getUserY、getRadius和circleColor中的值,但是我不知道如何将这些变量传递到一个数组中,该数组由paint组件用来创建一个圆 如何正确地获取getUserX、getUserY和getRadius变量的值并将它们放入circleValues数组中?如何从用户输入中获取circleColor并将其作为已创建圆圈的page.setColor
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CircleMaker
{
public static void main (String[] args)
{
BuildsFrame();
}
public static void BuildsFrame()
{
JFrame frame = new JFrame ("Circle Drawer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tp = new JTabbedPane();
tp.addTab ("Intro", new IntroPanel());
tp.addTab("Your Circle!", new CirclePanel());
frame.getContentPane().add(tp);
frame.pack();
frame.setVisible(true);
}
}
class IntroPanel extends JPanel
{
public IntroPanel()
{
final JTextField xCoorTF, yCoorTF, radiusTF;
int userXcoor, userYcoor, userRadius;
JButton makeButton = new JButton("Create!");
final JColorChooser colorChooser;
setLayout (new FlowLayout());
setBackground (Color.gray);
setPreferredSize (new Dimension(700, 500));
JLabel l1 = new JLabel ("Enter your desired coordinates and radius for your circle.");
JLabel l2 = new JLabel ("Please type the X coordinate in the first box, the");
JLabel l3 = new JLabel ("Y coordinates in the second box,");
JLabel l4 = new JLabel ("and the radius of your circle");
JLabel l5 = new JLabel ("in the final box.");
xCoorTF = new JTextField(5);
yCoorTF = new JTextField(5);
radiusTF = new JTextField(5);
colorChooser = new JColorChooser();
colorChooser.setBorder(BorderFactory.createTitledBorder("Choose Circle Color:"));
makeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
int getUserX = Integer.parseInt(xCoorTF.getText());
int getUserY = Integer.parseInt(yCoorTF.getText());
int getRadius = Integer.parseInt(radiusTF.getText());
Color circleColor = colorChooser.getColor();
System.out.println(getUserX);
System.out.println(getUserY);
System.out.println(getRadius);
}
});
add (l1);
add (l2);
add (l3);
add (l4);
add (l5);
add (xCoorTF);
add (yCoorTF);
add (radiusTF);
add (colorChooser);
add (makeButton);
}
}
class CirclePanel extends JPanel
{
int[] circleValues = {50, 50, 100};
circleValues[0] = getUserX;
circleValues[1] = getUserY;
circleValues[2] = getRadius;
public CirclePanel()
{
setBackground (Color.WHITE);
}
public void paintComponent (Graphics page)
{
super.paintComponent (page);
page.setColor (Color.BLACK);
page.fillOval((circleValues[0] - 2), (circleValues[1] - 2), (circleValues[2] + 4), (circleValues[2] + 4));
page.setColor (Color.GRAY);
page.fillOval (circleValues[0], circleValues[1], (circleValues[2]), (circleValues[2]));
}
}
与许多其他语言不同,命令在Java中不能“在空间中浮动”
您需要扩大该数据的范围,并将circleValues数组填充移动到将要执行的代码段。我对编码的了解非常有限。我不知道如何确保执行这些代码行;我以为他们是被执行的,但我还在学习编码,所以我只是有点随机应变。