JFrame 中的渐变背景

Sid*_*Sid 0 java swing gradient background jframe

我已经用谷歌搜索并阅读了很多内容,但没有找到适合我需要的答案,所以我会在这里问:

我想在我的 JFrame 中有一个渐变背景。目前背景是单色的。我的代码看起来像这样:

//imports

public class Game {

   //some other irrelevant instance variables

   JFrame frame = new JFrame("Game");

   public Game() {

       frame.getContentPane().setBackground(new Color(200,220,200));
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setLayout(null);
       frame.setPreferredSize(new Dimension(frameX,frameY)); //frameX and frameY are instance variables

       getMenu(); //method that adds a few JLabels to the JFrame and so on

   }
}
Run Code Online (Sandbox Code Playgroud)

我读过的方法适用于扩展 JPanel 或 JFrame 的类(然后使用 GradientPaint 或类似的东西),但正如您所看到的,我使用 JFrame 作为实例变量。有人可以帮我吗?

编辑: 图片:

在此输入图像描述

And*_*son 5

现在,显然,上面的示例图像没有指定按钮,也没有为底部的消息添加标签。但由于很明显您希望用户选择这些选项,因此我使用了按钮。底部的标签只是为了证明它们按钮(附加一个动作侦听器,以显示消息)。

使用实际按钮的优点是它们也可以通过键盘访问(按下Enter可查看第一条消息,按下Tab可导航到下一条消息......

如果游戏不需要键盘访问,您可以将它们替换为标签并添加鼠标侦听器。我会把它留给你。

该代码有很多包含“调整”一词的注释。仔细查看它们,检查 JavaDocs,根据需要调整它们。

在此输入图像描述 在此输入图像描述

在此输入图像描述 在此输入图像描述

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class GradientPaintBackground {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout(15, 15)) {

                    @Override
                    public void paintComponent(Graphics g) {
                        super.paintComponent(g);

                        Point point1 = new Point(10, 10);
                        Point point2 = new Point(
                                getWidth() - 10, 
                                getHeight() - 10);
                        final GradientPaint gp = new GradientPaint(
                                point1, Color.YELLOW,
                                point2, new Color(255, 225, 100),
                                true);

                        // we need a Graphics2D to use GradientPaint.
                        // If this is Swing, it should be one..
                        final Graphics2D g2 = (Graphics2D) g;
                        g2.setPaint(gp);
                        g.fillRect(0, 0, getWidth(), getHeight());
                    }
                };
                // adjust size to need.
                gui.setBorder(new EmptyBorder(20, 20, 20, 20));

                // Start:  Add components
                // adjust size to size of logo
                BufferedImage logo = new BufferedImage(
                        100, 40, BufferedImage.TYPE_INT_RGB);
                JLabel logoLabel = new JLabel(new ImageIcon(logo));
                gui.add(logoLabel, BorderLayout.NORTH);

                // adjust spacing to need
                JPanel menuPanel = new JPanel(new GridLayout(0, 1, 20, 20));
                menuPanel.setBorder(new EmptyBorder(5, 55, 5, 5));
                // allow the BG to show through..
                menuPanel.setOpaque(false);
                gui.add(menuPanel);
                String[] actionTexts = new String[]{
                    "Play Game", "Tutorial", "Other"
                };
                final JLabel messages = new JLabel("Ready to play?  "
                        + "Select an option");
                gui.add( messages, BorderLayout.PAGE_END );
                ActionListener al = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (e.getSource() instanceof JButton) {
                            JButton b = (JButton)e.getSource();
                            messages.setText(b.getText() + " selected!");
                        }
                    }
                };
                for (int ii = 0; ii < actionTexts.length; ii++) {
                    JButton b = new JButton(actionTexts[ii]);
                    b.setContentAreaFilled(false);
                    b.setHorizontalAlignment(SwingConstants.LEADING);
                    b.setBorder(null);
                    b.addActionListener(al);
                    menuPanel.add(b);
                }
                // End:  Add components

                JFrame f = new JFrame("Gradient Background in JFrame");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See /sf/answers/500037891/ for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                f.setMinimumSize(f.getSize());
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}
Run Code Online (Sandbox Code Playgroud)

一般提示

Java GUI 可能必须在多种平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于元件的精确放置。这就是为什么您不断地看到您所看到的问题类型的原因。把布局扔到窗外,一切都会崩溃。

要组织强大的 GUI 组件,请使用布局管理器或它们的组合1,以及空白区域的布局填充和边框2