我在Java中遇到一个巨大的问题,即创建一个GUI,它从用户那里获取输入并根据该输入创建一个圆圈。我正在从用户那里获取输入并保存getUserX,getUserY,getRadius和circleColor中的值,但是我不知道如何将这些变量传递给一个由paint组件用来创建一个圆的数组。 / p>
如何正确获取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]));
}
}
答案 0 :(得分:0)
与许多其他语言不同,命令不能在空间中浮动"在Java。
您需要提高该数据的范围,并将 circleValues 数组填充移动到将要执行的代码段。