简体   繁体   English

如何为 JPanel 创建渐变? 另外,如何在 Java 中的 class 中调用 class?

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

I am making a Java GUI and I have searched the internet for 2 hours on how make a gradient for a JPanel.我正在制作 Java GUI,我已经在互联网上搜索了 2 个小时,了解如何为 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.我尝试了很多版本,但我也不完全知道如何在 class 中运行 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 JPanel 的背景仍然是白色的

  1. The instance of TestPanel is never added to anything TestPanel的实例永远不会添加到任何东西
  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 null布局将阻止组件的大小和定位,因此即使您执行了上一步,您也不会看到任何内容
  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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM