簡體   English   中英

無法在我的JPanel上繪制任何內容

[英]Can't draw anything on my JPanel

只是想了解簡單的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上都會有一個小的白色正方形,但是paintComponent()方法沒有任何反應。 如果我將DrawBlock擴展到JComponent,將根本沒有正方形。 這可能是初學者的問題,但我無法解決。

您遇到的主要問題是DrawBlock實際上沒有大小,因此,重新繪制管理器會自動放棄對其進行繪制...

在此處輸入圖片說明

嘗試像這樣修改您的代碼...

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方法將使布局管理器知道您的組件想要的大小...

警告-代碼審查;)

我不確定您為什么要完成自己的工作,但請放到一邊...

您創建了一個從JPanelDrawPanels )擴展的類, 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)移到了最后一條語句,這將確保框架在可見之前進行布局。

我還稍微調動了布局管理器...

編輯:
嘗試刪除

mainFrame.revalidate();

這不是JFrame的公認命令。

此外,將@Override Annotation添加到paintComponent:

@Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawRect(1, 1,15,15);
    }

然后對我來說效果最好。

g.setColor(Color.WHITE)之前使用g.setColor(Color.WHITE) g.drawRect(1, 1,15,15);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM