Java 在 Jpanel 中绘制矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18877882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:51:16  来源:igfitidea点击:

draw rectangle in Jpanel

javaswing

提问by Hyman_of_All_Trades

I am trying to get my hands on GUI programming in java and wanted to draw a rectangle in Jpanel. Code does not give any error but I cannot get rectangle in the GUI. Can somebody please tell me what I am missing in the following code. I am sure it is pretty simple so please be gentle.

我正在尝试使用 Java 进行 GUI 编程,并想在 Jpanel 中绘制一个矩形。代码没有给出任何错误,但我无法在 GUI 中获得矩形。有人可以告诉我我在以下代码中缺少什么吗?我相信这很简单,所以请保持温和。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloWorldGUI2 {

    private static class ButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        System.exit(0);
        }
        }
    private static class RectDraw extends JPanel {
        public void paintComponent(Graphics g) {
        super.paintComponent(g);  
         g.drawRect(230,80,10,10);  
         g.setColor(Color.RED);  
         g.fillRect(230,80,10,10);  
        }
        }
    public static void main(String[] args) {
        JPanel content = new JPanel();
        RectDraw newrect= new RectDraw();
        JButton okButton= new JButton("OK");
        JButton clearButton= new JButton("Clear");
        ButtonHandler listener= new ButtonHandler();
        okButton.addActionListener(listener);
        clearButton.addActionListener(listener);
        content.add(okButton);
        content.add(clearButton);
        content.add(newrect);
        JFrame window = new JFrame("GUI Test");
        window.setContentPane(content);
        window.setSize(250,100);
        window.setLocation(100,100);
        window.setVisible(true);
        }

    }

采纳答案by Hovercraft Full Of Eels

Your newrect RectDraw's size is likely going to be quite small, probably [0, 0], since it has been added to a FlowLayout using JPanel and doesn't have a preferredSize set. Consider overriding its getPreferredSize()method and returning a suitable Dimension so that its drawing can be seen.

您的 newrect RectDraw 的大小可能会非常小,可能是 [0, 0],因为它已使用 JPanel 添加到 FlowLayout 并且没有设置 preferredSize。考虑覆盖它的getPreferredSize()方法并返回一个合适的 Dimension 以便可以看到它的绘图。

private static class RectDraw extends JPanel {
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);  
    g.drawRect(230,80,10,10);  
    g.setColor(Color.RED);  
    g.fillRect(230,80,10,10);  
  }

  public Dimension getPreferredSize() {
    return new Dimension(PREF_W, PREF_H); // appropriate constants
  }
}

Also,

还,

  • The paintComponentmethod should be protected, not public
  • Don't forget to use @Overridebefore your method overrides.
  • Aim for muchless code in the main method and more code in the "instance" world, not the static world.
  • Take care with your code formatting. Poor code formatting, especially misleading indentations, leads to silly errors.
  • paintComponent方法应该受到保护,不公开
  • 不要忘记@Override在方法覆盖之前使用。
  • 目的为主方式更少的代码和更多的代码在“实例”的世界,而不是静态的世界。
  • 请注意您的代码格式。糟糕的代码格式,尤其是误导性的缩进,会导致愚蠢的错误。

回答by camickr

I am trying to get my hands on GUI programming in java

我正在尝试使用 Java 进行 GUI 编程

Then you should start by reading the Swing tutorial.

那么您应该首先阅读Swing 教程

For this question you can start with the section on Custom Painting.

对于这个问题,您可以从自定义绘画部分开始。

Not only does the tutorial show you a working example, but it also shows you how to properly create the frame by executing the code on the EDT. Read the section on Concurrencyfor more information.

本教程不仅向您展示了一个工作示例,而且还向您展示了如何通过在 EDT 上执行代码来正确创建框架。阅读有关Concurrency更多信息的部分。