Java swing自定义背景

标签 java swing background

我有一个 JFrame,上面有一些面板,每个面板包含不同的项目。如何设置框架背景?我的意思是,如果我只设置背景,不添加项目,背景就是我想要的,但如果我添加项目,背景是清晰的。同样,如果我首先设置一个面板的背景,然后将一些对象插入其中,则该项目不会出现在框架上,框架将使用我选择的背景着色。您能否向我展示为已有项目的面板/框架设置背景颜色的最简单方法?谢谢。我想设置自定义背景颜色。如果唯一的方法是设置背景图像,我会这样做......

编辑:我想要单一背景颜色,而不是更多。

最佳答案

我已经为您制作了一个示例程序,如果您想要除此之外的其他内容,请告诉我。我已经完成了这两种方法,您可以通过按 JButton 在 JPanel 上没有任何项目的情况下为背景设置新颜色,或者您可以先将项目添加到 JPanel,然后更改背景颜色,这工作正常。似乎您的问题对于预期发生的情况以及实际发生的情况有点不清楚。除此之外,如果您还想要其他东西,请告诉我。

此外,当您将项目添加到已显示的 JPanel 时,请务必在之后 revalidate() 和 repaint() 您的 JPanel,以使更改生效。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddItemOrColor extends JFrame
{
    private JPanel contentPane;
    private JButton modifyItemButton;
    private JButton modifyColorButton;
    private ActionListener action;
    private int count = 0;
    private Color[] color = { 
                                Color.RED, Color.BLUE, Color.GRAY,
                                Color.WHITE, Color.CYAN, Color.PINK,                                                                                                                                     
                                Color.DARK_GRAY, Color.ORANGE, Color.MAGENTA
                            };

    public AddItemOrColor()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        contentPane = new JPanel(new FlowLayout(FlowLayout.LEFT, 3, 3));
        //contentPane.setMargin(new Insets(10, 10, 10, 10));
        contentPane.setBackground(Color.BLUE);

        modifyItemButton = new JButton("MODIFY CONTENT");
        modifyColorButton = new JButton("MODIFY COLOR");

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                JButton button = (JButton) ae.getSource();

                if (count == 9)
                    count = 0;

                if (button == modifyItemButton)
                {
                    contentPane.add(new JLabel("LABEL " + count));                  
                }
                else if (button == modifyColorButton)
                {
                    contentPane.setBackground(color[count]);
                }

                contentPane.revalidate();
                contentPane.repaint();
                count++;
            }
        };

        modifyItemButton.addActionListener(action);
        modifyColorButton.addActionListener(action);

        add(modifyColorButton, BorderLayout.PAGE_START);
        add(contentPane, BorderLayout.CENTER);
        add(modifyItemButton, BorderLayout.PAGE_END);

        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new AddItemOrColor();
            }
        });
    }
}

关于Java swing自定义背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9573078/

相关文章:

css 在列表项(导航栏)内填充图像

java - hibernate中的Socket超时和Query超时有什么区别?

java - 更新 API Manager 2.0 和 IDS 5.2 的自定义声明 jar 文件

java - 窗口底部的 JButton

java - 单击按钮更改面板大小

Android 应用程序在动画渐变背景上崩溃

css - IE9 :hover Glitch

java - 外部程序的退出值错误

java - SEVERE : Running with Java class version 53. 0,但 52.0 是必需的错误,Jenkins 通过命令提示符显示为 "java -jar jenkins.war"

java - 改进我的 JLabel 闪烁