gpt4 book ai didi

java - 需要了解有关 N × N 正方形的更多信息(n = 用户输入)

转载 作者:行者123 更新时间:2023-12-01 13:47:56 26 4
gpt4 key购买 nike

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

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

更新

我的代码如下!

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)了解更多关于custom Paintings的信息。例如,使用 JPanelpaintComponent(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();
}
}

关于java - 需要了解有关 N × N 正方形的更多信息(n = 用户输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20207792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com