如何将paintComponent()添加到JPanel [英] How to add a paintComponent() to a JPanel

查看:81
本文介绍了如何将paintComponent()添加到JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个课程,Class2和Class4.我希望使用p4.add(c2o)和f.add(p4)方法将Class4中的Class2中的绘画添加到Class4中的JPanel上.添加GUI没问题,但是我根本无法添加图形.

I have 2 classes, Class2 and Class4. I wish to add the paint in Class2 from Class4 onto a JPanel in Class4 using the p4.add(c2o) and f.add(p4) methods. I have no problems adding GUI but I simply couldn't add graphics.

Class4:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Class4 {


    public void mainMethod(int event){
        JFrame f = new JFrame("Love Test");
        if(event == 0){

            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(500,200);
            f.setLayout(null);

            JPanel p = new JPanel(new BorderLayout());
            p.setBounds(150, 0, 350, 75);
            Class3 c3o = new Class3();
            p.add(c3o);
            f.add(p);

            JPanel p2 = new JPanel();
            Class7 c7o = new Class7();
            p2.add(c7o);
            p2.setBounds(0, 75, 500, 40);
            f.add(p2);

            JPanel p3 = new JPanel();
            p3.setBounds(0, 0, 150, 75);
            Class5 c5o = new Class5();
            p3.add(c5o);
            f.add(p3);

            f.setVisible(true);

        }

        if(event == 5){

            JPanel p4 = new JPanel();
            Class2 c2o = new Class2();
            p4.add(c2o);
            p4.setBounds(0,115,500,55);
            f.add(p4);
        }

    }


}

Class2:

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.util.Random;
import java.awt.*;



public class Class2 extends JPanel{

    public void paint(Graphics g){
        super.paint(g);
        this.setBackground(Color.WHITE);
        g.setColor(new Color(0,0,0));
        String str = "";
        String str2 = "";
        Random rlove = new Random();
        int love = rlove.nextInt(101);
        Class3 c3o = new Class3();
        str = c3o.boy.getText() + " and " + c3o.girl.getText() + "'s amount of love is " + love + "%.";
        if(love >= 0 && love < 10){         
            str2 = "Stop thinking about that person anymore!";
        }
        if(love >= 10 && love < 20){
            str2 = "1/100 percent chance...";
        }
        if(love >= 20 && love < 30){
            str2 = "Little hope...";
        }
        if(love >= 30 && love < 40){
            str2 = "Not even a kiss, stop talking about French kisses! (wise words of Rubesh)";
        }
        if(love >= 40 && love < 50){
            str2 = "Near passing";
        }
        if(love >= 50 && love < 60){
            str2 = "Just Pass";
        }
        if(love >= 60 && love < 70){
            str2 = "Some effort needed.";
        }
        if(love >= 70 && love < 80){
            str2 = "Grade A. A good chance.";
        }
        if(love >= 80 && love < 90){
            str2 = "Good pair. Very good relationship!";
        }
        if(love >= 90 && love < 100){
            str2 = "Perfect pair. Lost for words.";
        }
        if(love == 100){
            str2 = "OH MY GOD!!!!!";
        }
        g.drawString(str,10,10);
        g.drawString(str2,10,30);
    }


}

如何将paintComponent添加到JPanel p4?谢谢!

How do I add the paintComponent to JPanel p4? Thanks!

推荐答案

  1. 不要使用 JPanel#paint ,建议使用 JPanel#paintComponent ,请检查
  1. Don't use JPanel#paint, it is recommended that use JPanel#paintComponent, check out Performing Custom Painting
  2. Make use of appropriate layout managers. The problem you having comes from two issues. The first is, you panel has no preferred size. Override the getPreferredSize method of Class2 and return an appropriate size of the layout manager.

...

JPanel p4 = new JPanel(); // Default layout manager is FlowLayout
Class2 c2o = new Class2();
// c2o has not size (0x0 by default)...
p4.add(c2o);
p4.setBounds(0,115,500,55);
f.add(p4);

这篇关于如何将paintComponent()添加到JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆