英文:
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("Simple GUI");
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("Simple GUI");
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论