Graphics2d绘制一个矩形。

huangapple go评论92阅读模式
英文:

Graphics2d making a box

问题

import java.awt.*;
import javax.swing.*;

public class Frame extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawRect(150, 150, 20, 20);
    }

    public static void createWindow() {
        JFrame frame = new JFrame("Simple GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        createWindow();
    }
}
英文:

I am new to java, I wanted to make a program that draws a box on screen, everything is correct except for the paintComponent(); which is not working

<!-- language: lang-java -->

import java.awt.*; 
import javax.swing.*;
import java.awt.Graphics2D.*;
public class Frame extends JPanel
{
    @Override //this section creates the box
    public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
     super.paintComponent(g);
     
     Graphics2D g2d = (Graphics2D) g;
     
     g2d.drawRect(150,150,20,20);
    
    }
    public static void createWindow() //this section creates the frame
    {
        JFrame frame = new JFrame(&quot;Simple GUI&quot;);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //set closing bebavior
        frame.setSize(400, 400);                                 //set the size of jframe
        frame.setLocationRelativeTo(null);                       //center the jframe 
        frame.setVisible(true);                                  //show the frame
    }
    //main method  
    public static void main(String[] args) 
    {
        createWindow();//launch your creaWindow method  
        paintComponent();
    }
}       

答案1

得分: 1

你必须复习Java的基础知识。你在这段代码中的错误是,你必须创建一个该类(Frame)的对象,并将它添加到你创建的JFrame中。以下是正确的代码 @Hh000。

import java.awt.*;
import javax.swing.*;

public class Frame extends JPanel {

    @Override // 这部分创建了一个框
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.drawRect(150, 150, 20, 20);
    }

    // 主方法
    public static void main(String[] args) {
        JFrame frame = new JFrame("Simple GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭行为
        Frame frameObject = new Frame(); // 创建一个类对象
        frame.add(frameObject); // 将对象添加到JFrame中
        frame.setSize(400, 400); // 设置JFrame的大小
        frame.setLocationRelativeTo(null); // 将JFrame居中
        frame.setVisible(true);
    }
}
英文:

You have to review the basics of Java. Your mistake in this is that you have to create an object of this class Frame and add it to the JFrame which you have created. Below is the correct code @Hh000.

import java.awt.*; 
import javax.swing.*;
import java.awt.Graphics2D.*;
public class Frame extends JPanel{

    @Override //this section creates the box
    public void paintComponent(Graphics g){
     super.paintComponent(g);
	 g.setColor(Color.BLACK);
     g.drawRect(150,150,20,20);
    }
	
    //main method  
    public static void main(String[] args){ 
        JFrame frame = new JFrame(&quot;Simple GUI&quot;);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //set closing bebavior
	    Frame frameObject = new Frame();			           //Making a class object
	    frame.add(frameObject);					               //Adding the object into the JFrame
        frame.setSize(400, 400);                               //set the size of jframe
        frame.setLocationRelativeTo(null);                     //center the jframe 
        frame.setVisible(true);
    }
}

huangapple
  • 本文由 发表于 2020年10月21日 12:45:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64456831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定