How do you create a gradient for JPanel? Also, how do you call a class within a class in Java?

不羁岁月 提交于 2019-12-04 07:14:43

问题


I am making a Java GUI and I have searched the internet for 2 hours on how make a gradient for a JPanel. The code below is that I have, but when run the gradient does not show. What is wrong?

I've tried many other posts from similar questions on this throughout the Internet but they don't work. I've tried numerous versions, but I also don't exactly know how to run a class within a class. Can someone help me please?

class TestPanel extends JPanel{
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        int w = getWidth();
        int h = getHeight();
        Color color1 = Color.BLUE;
        Color color2 = Color.GREEN;
        GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
    }
} //this is nested within the main class

//some code

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
        CreateGUI cg = new CreateGUI();
        cg.create();    //previous method (not mentioned here)           
            CreateGUI.TestPanel tp = cg.new TestPanel(); //problem
            JPanel panel = new JPanel();
            f.add(panel);
            f.setSize(800, 600);
            f.setLocationRelativeTo(null);
        f.getContentPane().setLayout(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
    });
}

I expect there to be a gradient but there is none; the background of the JPanel is still white


回答1:


  1. The instance of TestPanel is never added to anything
  2. null layouts will prevent the component from been sized and positioned, so you won't see anything even if your did the previous step
  3. You should, unless you're adding child components to it, provide a sizing hint, so that the layout managers have something to work with.

For example:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    class TestPanel extends JPanel {

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            int w = getWidth();
            int h = getHeight();
            Color color1 = Color.BLUE;
            Color color2 = Color.GREEN;
            GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
            g2d.setPaint(gp);
            g2d.fillRect(0, 0, w, h);
            g2d.dispose();
        }
    }
}


来源:https://stackoverflow.com/questions/58795134/how-do-you-create-a-gradient-for-jpanel-also-how-do-you-call-a-class-within-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!