【问题标题】:I have a project that ask me to create button,and user enter the radius and location of the circle, then user click button to draw the circle我有一个项目要求我创建按钮,用户输入圆的半径和位置,然后用户单击按钮绘制圆
【发布时间】:2015-03-27 23:14:57
【问题描述】:

这个程序要求用户输入一个圆的数据。包括:x的位置,y的位置,圆的宽高。

所以当我测试这个程序时,我输入了位置和它的大小;然后我点击了绘制按钮。圆圈没有出现。

这是我的代码!

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);
    }
}

【问题讨论】:

标签: java swing


【解决方案1】:

在没有布局管理器的情况下创建新的JComponent 对象时,其首选大小的宽度最初为 0,高度为 0,因此根本不可见。要解决此问题,您的 drawCircle 类应覆盖 getPreferredSize 方法,以便添加它的组件知道其大小。例如:

@Override
public Dimension getPreferredSize() {
    return new Dimension(width, height);
}

强烈建议您使用某种layout manager,但是这样您就不会遇到各种问题,例如组件大小错误。

【讨论】:

  • 你应该重写DrawCirlce类的getPreferredSize()方法,根据绘制代码返回组件的大小。只有进行绘制的组件才知道组件的实际尺寸应该是多少。
  • --天哪,它仍然无法正常工作。你能告诉我它是如何根据我的代码工作的吗?请!!!!!!
  • @Johnny 如果仍然不可见,您可以尝试更改构造函数调用中的值 - 例如尝试 500、500。
猜你喜欢
  • 2022-11-29
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-29
  • 2021-11-11
相关资源
最近更新 更多