Java 我有一个项目,要求我创建按钮,用户输入圆的半径和位置,然后用户单击按钮绘制圆
该程序要求用户输入圆的数据。包括以下内容:x的位置、y的位置以及圆的宽度和高度 所以当我测试这个程序时,我输入了位置和大小;然后我按了绘图按钮。圆圈没有出现 这是我的密码Java 我有一个项目,要求我创建按钮,用户输入圆的半径和位置,然后用户单击按钮绘制圆,java,swing,Java,Swing,该程序要求用户输入圆的数据。包括以下内容:x的位置、y的位置以及圆的宽度和高度 所以当我测试这个程序时,我输入了位置和大小;然后我按了绘图按钮。圆圈没有出现 这是我的密码 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Draw extends JFrame { private JButton draw; private JTextField posOfX; p
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Draw extends JFrame
{
private JButton draw;
private JTextField posOfX;
private JTextField posOfY;
private JTextField Jwidth;
private JTextField Jheight;
private ActionListener listener;
private JLabel JLx;
private JPanel drawingPanel;
private JLabel JLy;
private JLabel JLwidth;
private JLabel JLheight;
private JComponent component;
public int x =100 ;
public int y =100 ;
public int width = 100;
public int height = 100 ;
private JPanel panel;
public Draw()
{
listener = new actionPerform();
component = new drawCircle();
panel = new JPanel();
draw = new JButton ("Draw");
draw.addActionListener(listener);
posOfX = new JTextField( 15);
posOfY = new JTextField(15);
Jwidth = new JTextField(15);
Jheight = new JTextField(15);
JLx = new JLabel("X");
JLy = new JLabel("Y");
JLwidth = new JLabel("Width");
JLheight = new JLabel("Height");
panel.add(JLx);
panel.add(posOfX);
panel.add(JLy);
panel.add(posOfY);
panel.add(JLwidth);
panel.add(Jwidth);
panel.add(JLheight);
panel.add(Jheight);
panel.add(draw);
panel.add(component);
add(drawingPanel,BorderLayout.SOUTH);
add(panel,BorderLayout.NORTH);
}
class drawCircle extends JComponent
{
public void paintComponent(Graphics g)
{
g.drawOval(x,y,width,height);
}
}
class actionPerform implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
if(e.getSource() == draw)
{
width = width + Integer.parseInt(Jwidth.getText());
height = height + Integer.parseInt(Jheight.getText());
x = Integer.parseInt(posOfX.getText()) + width;
y = Integer.parseInt(posOfY.getText())+ height;
Jwidth.setText("");
Jheight.setText("");
posOfX.setText("");
posOfY.setText("");
}
}
catch (Exception except)
{
Jwidth.setText("");
Jheight.setText("");
posOfX.setText("");
posOfY.setText("");
JOptionPane.showMessageDialog(null,"You should enter numbers only","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
}
import java.awt.*;
import javax.swing.*;
public class DrawViewer
{
public static void main(String []args)
{
Draw d = new Draw();
d.setVisible(true);
d.setTitle("Draw circle");
d.setSize(1000,1000);
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
在没有布局管理器的情况下创建新的
JComponent
对象时,其首选大小的宽度最初为0,高度为0,因此根本不可见。要解决此问题,您的drawCircle
类应该重写getPreferredSize
方法,以便添加它的组件知道它的大小。例如:
@Override
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
但是,强烈建议您使用某种类型的a,这样您就不会出现各种问题,例如组件的大小错误。您应该学会使用a。您应该重写DrawCirlce类的
getPreferredSize()
方法,根据绘制代码返回组件的大小。只有画这幅画的元件才知道元件的实际尺寸。--哦,天哪,它仍然不起作用。你能告诉我它是如何基于我的代码的吗?请@Johnny如果构造函数调用仍然不可见,您可以尝试更改其值——例如,尝试500500。