繁体   English   中英

需要了解有关N乘N平方的更多信息(n =用户输入)

[英]Need to learn more about an N by N square (n = user input)

我需要根据用户输入绘制一个正方形。 用户输入后,将弹出一个框架,并且按钮显示“单击我”。 我需要单击以生成正方形。

单击按钮时如何使按钮生成正方形?

UPDATE

我的代码如下!

import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.*;

//All of these imports are required, in the case of BorderLayout, and JFrame, etc.

public class Gooey implements ActionListener // implements the ActionListener (the button click)
{

  private static int n;   // make the n variable as in the lab, used for height and width of square
  private static JFrame frame; //make the frame non-accessible from any other classes, simply because
                                // we don't want a bunch of frames running with the same stuff!

  public static void main(String[] args) 
  {
    frame = new JFrame(); //create the frame!  DUH!
    JButton button = new JButton("DrawSquare!"); //make the button!
    button.addActionListener(new Gooey()); //adds the actionListener to it can respond to a button click
    JLabel label = new JLabel("Draw Your Square!"); //Make the label set to DrawSquare

    JPanel panel = new JPanel();
    panel.add(button); //add the button to the panel!
    panel.add(label); // add the label to the panel!
    frame.getContentPane().add(panel, BorderLayout.NORTH); //set the panel of the frame to the "north"

    Scanner user_input = new Scanner(System.in);
    System.out.println("Enter The Size Of Your Square. Make sure it's not too big!!");
    n = user_input.nextInt();  //set 'n' to equal the width and height of the drawSquare method

    int FRAME_WIDTH = (n + 50); //make sure the width fits the square
    int FRAME_HEIGHT = (n + 100); // make sure the height fits the square

    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); //set the frame width and height, to fit square.
    frame.setTitle("A nice "
                     + n
                     + " by "
                     + n
                     + " Square!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //make sure the frame exits and resets when closed.
    frame.setVisible(true); //make it visible on the foreground!
  }

  @Override //recommended.  Keeps from making program all wacky!
  public void actionPerformed(ActionEvent arg0) {
    JPanel p = new JPanel() //obviously, a new JPanel!
    {
      protected void paintComponent(Graphics g) //start making the square itself, and color it !!!
      {
        super.paintComponent(g);
        int height = n;
        int width = n;
        g.setColor(Color.blue);
        g.drawRect(10, 10, height, width);
      };

    };
    frame.getContentPane().add(p, BorderLayout.CENTER);  //center the Square inside the frame.
    frame.getContentPane().revalidate(); //Recalculate the layout. 
  }
}

到此结束!

1)阅读有关自定义绘画的更多信息。 例如,使用JPanel paintComponent(Graphics g)方法进行绘画。

2)您的FrameViewer尚未paint(Graphics g)方法,因为您无法覆盖它。

3)您的FrameViewer实现了ActionListener但是您不会重写actionPerformed()因为会遇到编译错误。

4)您的按钮什么也不做,您忘记在其中添加一个ActionListener

我修复了您的代码,对其进行了检查:

public class Form implements ActionListener {

    private static int n;
    private static JFrame frame;

    public static void main(String[] args) {
        frame = new JFrame();
        JButton button = new JButton("Click Me!");
        button.addActionListener(new Form());
        JLabel label = new JLabel("DrawSquare");

        JPanel panel = new JPanel();
        panel.add(button);
        panel.add(label);
        frame.getContentPane().add(panel,BorderLayout.NORTH);

        Scanner user_input = new Scanner(System.in);
        System.out.println("Enter The Size Of Your Head!  Or Square.   Whichever!");
        n = user_input.nextInt();

        int FRAME_WIDTH = (n + 600);
        int FRAME_HEIGHT = (n + 400);

        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setTitle("A nice "
                + n
                + " by "
                + n
                + " Square! Just click the button and watch the instantanious magic!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        JPanel p = new JPanel(){
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                int height = n;
                int width = n;
                g.setColor(Color.blue);
                g.drawRect(10, 10, height, width);
            };

        };
        frame.getContentPane().add(p,BorderLayout.CENTER);
        frame.getContentPane().revalidate();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM