java - 无法在我的 JPanel 上绘制任何内容

标签 java swing jpanel paintcomponent preferredsize

只是想了解简单的 Java 事物。无法让这个东西发挥作用。警告:传入大量错误代码。

import java.awt.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.LineBorder;


class DrawFrame  {
    public DrawFrame(){
        DrawPanels panelFrame=new DrawPanels();
        JFrame mainFrame=new JFrame();
        mainFrame.setLayout(new GridLayout(1,3));
        mainFrame.setVisible(true);
        mainFrame.setSize(480, 800);
        mainFrame.setTitle("Title");
        mainFrame.setResizable(false);
        mainFrame.add(panelFrame.panel1);
        mainFrame.add(panelFrame.panel2);
        mainFrame.add(panelFrame.panel3);
        //panelFrame.panel1.getGraphics();
        panelFrame.panel1.add(new DrawBlock());
        panelFrame.panel2.add(new DrawBlock());
        panelFrame.panel3.add(new DrawBlock());
        mainFrame.revalidate();
        mainFrame.repaint();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
class DrawPanels extends JPanel{
     JPanel panel1=new JPanel();
     JPanel panel2=new JPanel();
     JPanel panel3=new JPanel();
    public DrawPanels(){
        panel1.setBackground(Color.ORANGE);
        panel2.setBackground(Color.BLACK);
        panel3.setBackground(Color.RED);
        panel1.setVisible(true);
        panel2.setVisible(true);
        panel3.setVisible(true);
        panel1.setBorder(new LineBorder(Color.BLACK));
        panel2.setBorder(new LineBorder(Color.BLACK));
        panel3.setBorder(new LineBorder(Color.BLACK));

    }
}

class DrawBlock extends JPanel{

    private static final long serialVersionUID = 1L;
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawRect(1, 1,15,15);
    }
}


public class MainClass {

    /**
     * @param args
     */
    public static void main(String[] args) {


        DrawFrame Windos=new DrawFrame();
    }}

如果我的类DrawBlock扩展了JPanel,每个JPanel处都会有一个白色的小方 block ,但是,paintComponent()方法没有任何反应。如果我将 DrawBlock 扩展到 JComponent,则根本不会有正方形。这可能是初学者的问题,但我无法解决。

最佳答案

您遇到的主要问题是 DrawBlock 实际上没有大小,因此,重绘管理器会自动放弃对其进行绘制...

enter image description here

尝试修改您的代码...

public class DrawBlock extends JPanel {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(18, 18);
    }

    @Override
    public void paintComponent(Graphics g) {
        System.out.println("...");
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.drawRect(1, 1, 15, 15);
    }
}

添加 getPreferredSize 方法将使布局管理器知道您的组件想要的大小...

警告 - 代码审查;)

我不确定你为什么要做你所做的事情,但让我们把它放在一边......

您创建一个从 JPanel (DrawPanels) 扩展的类,其中包含许多其他 JPanel,但不是将它们添加到框架,您从中提取面板并将面板提取到框架中......

直接将 DrawPanels 添加到框架本身会更有意义......

public static class DrawFrame {

    public DrawFrame() {
        DrawPanels panelFrame = new DrawPanels();
        JFrame mainFrame = new JFrame();
        mainFrame.setLayout(new BorderLayout());
        mainFrame.setSize(480, 800);
        mainFrame.setTitle("Title");
        mainFrame.setResizable(false);
        mainFrame.add(panelFrame);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
    }
}

public class DrawPanels extends JPanel {

    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();

    public DrawPanels() {
        setLayout(new GridLayout(1, 3));
        panel1.setBackground(Color.ORANGE);
        panel2.setBackground(Color.BLACK);
        panel3.setBackground(Color.RED);
        panel1.setBorder(new LineBorder(Color.BLACK));
        panel2.setBorder(new LineBorder(Color.BLACK));
        panel3.setBorder(new LineBorder(Color.BLACK));

        panel1.add(new DrawBlock());
        panel2.add(new DrawBlock());
        panel3.add(new DrawBlock());

        add(panel1);
        add(panel2);
        add(panel3);

    }
}

注意 - 我将 mainFrame.setVisible(true) 移至最后一个语句,这将确保框架在可见之前就已布局。

我还稍微移动了布局管理器...

关于java - 无法在我的 JPanel 上绘制任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16415420/

相关文章:

Java:同步关键字不会阻塞不同线程上的对象

java - 安卓错误: Unable to resume activity Adview on a null object reference

java - 如何为应用程序和小程序播放 mp3 文件?

java - 按下按钮时使其他类框架设置可见(假)

java - 将 JTextArea 添加到 TabbedPane

java - 如何使用SpringSource Tool Suit开发Tomcat应用?

java - Java中访问另一个类的数据成员字段

从 IDE Netbeans 运行 Java GUI 程序

java - 使用 SAX java Swing 的 XML 解析器

Java优化为游戏绘制背景图像