我需要从用户输入中绘制一个正方形。用户输入后,弹出一个框架,按钮显示“click me”。我需要点击才能生成正方形。
点击它后,如何让按钮生成正方形?
更新
我的代码如下!
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.
}
}
这已经结束了!
答案 0 :(得分:1)
1)阅读有关custom Paintings的更多信息。例如,使用paintComponent(Graphics g)
方法JPanel
进行绘画。
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();
}
}