更改 JPanel 的颜色和尺寸不起作用

Changing colour and dimensions of JPanel not working

我创建了一个 JPanel,其中包含网格结构中 ItemTest class 的 9 个实例。

ItemTest class 包含自己的 JPanel、一个复选框和一些文本。它用于表示可以购买的不同项目。我希望能够更改属于 ItemTest class.

的 JPanel 组件的大小和颜色

setSize 方法和setForeground 方法在这里似乎不起作用。 this.setSize(100,100)this.setForeground(new java.awt.Color(80,80,90)) 对 JPanel 没有影响。

import javax.swing.JPanel;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ItemTest extends JPanel{

    static JPanel secondPanel = new JPanel();

    private static final long serialVersionUID = 8013287075740780359L;

    static JCheckBox selectBox;

    static GraphicsConfiguration gc;

    static JFrame frame = new JFrame(gc);

    ItemTest(String name, double cost){

        JLabel nameLabel = new JLabel();
        this.add(nameLabel);
        nameLabel.setText(name);

        selectBox = new JCheckBox("$"+cost);

        this.setForeground(new java.awt.Color(80, 80, 90));
        this.setSize(100, 100);
        this.add(selectBox);
    }

    public static void main(String[] args) {

        frame.setVisible(true);
        frame.setTitle("Program");
        frame.setSize(1000,800);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().setBackground(new java.awt.Color(100,100,110));

        secondPanel.setBounds(345,40,640,700);
        secondPanel.setBackground(new java.awt.Color(90,90,100));
        secondPanel.setLayout(new GridLayout(3,3,50,50));
        secondPanel.setVisible(true);
        secondPanel.getComponents();
        frame.add(secondPanel);

        for (int i = 0; i < 9; i ++) {
            secondPanel.add(new ItemTest("T-Shirt",50));
            System.out.println("test");
        }
    }
}

编辑:

阅读了一些改进我的代码的建议后,我稍微更改了 class。

import javax.swing.JPanel;

import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ItemTest extends JPanel{

    private static final long serialVersionUID = 8013287075740780359L;

    static GraphicsConfiguration gc;
    static JFrame frame = new JFrame(gc);

    ItemTest(String name, double cost){

        JLabel nameLabel = new JLabel();
        this.add(nameLabel);
        nameLabel.setText(name);


        JCheckBox selectBox = new JCheckBox("$"+cost);
        this.add(selectBox);

        //Changes colour of the text, but I want that to remain black.  I'm looking to change the colour of the actuall JPanel.
        selectBox.setForeground(Color.red);
        nameLabel.setForeground(Color.red);
    }

    public static void main(String[] args) {

        frame.setTitle("Program");
        frame.setSize(1000,800);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout( new GridBagLayout() );
        frame.setLocationRelativeTo(null);
        frame.getContentPane().setBackground(new java.awt.Color(100,100,110));
        frame.setVisible(true);

        JPanel secondPanel = new  JPanel();
        //secondPanel.setBounds(345,40,640,700);
        secondPanel.setBackground(new java.awt.Color(90,90,100));
        secondPanel.setLayout(new GridLayout(3,3,50,50));
        secondPanel.setVisible(true);
        secondPanel.getComponents();
        frame.add(secondPanel);

        for (int i = 0; i < 9; i ++) {
            secondPanel.add(new ItemTest("T-Shirt",50));
        }
    }
}

更改 setBounds 方法删除了我喜欢的 JPanel 大小,因此现在所有项目都位于 window 的中心,而不是我以前的设计,它们包含在jpanel 偏到一边。

使用

selectBox.setForeground(Color.red);
nameLabel.setForeground(Color.red);

将文本颜色设置为红色。我想更改JPanel的背景颜色,并保持文本颜色为黑色。

我希望将物品放在面板上的一边,就像我的原始代码中的尺寸一样,尺寸为 50x50。

static JCheckBox selectBox; 

不要使用静态变量 Swing 组件应该是在 ItemTest 中定义的实例变量 class。

this.setForeground(new java.awt.Color(80, 80, 90));

在 JPanel 上设置前景没有任何作用,因为 JPanel 上没有自定义绘画。

您需要在 JLabel 和 JCheckbox 上设置前景。

this.setSize(100, 100);

不要使用 setSize(...)。正如您所注意到的,它将被忽略。

Swing 旨在与布局管理器一起使用。 JPanel 的默认布局是 FlowLayout。 JLabel 和 JCheckBox 都将以其首选大小显示。

如果您想在面板上增加 space,则可以使用 EmptyBorder。阅读有关 How to Use Borders 的 Swing 教程部分,了解更多信息和工作示例。

frame.setLayout(null);
secondPanel.setBounds(345,40,640,700);

不要使用空布局和 setBounds(...)。

而是使用类似的东西:

frame.setLayout( new GridBagLayout() );

面板的大小将由您添加到面板的组件自动确定。

编辑:

I'm looking to change the colour of the actuall JPanel

然后使用 setBackground(...) 而不是 setForeground(...) 更改面板的背景。

, so now all of the items are in the centre of the window, instead of my previous design where they were contained in a jpanel off to the side.

是的,这是 GridBagLayout 的默认行为。如果您不喜欢那样,则可以在框架上使用不同的布局管理器。阅读 Layout Managers 上的教程。最简单的可能是 FlowLayout。可以指定对齐方式为左对齐。

with dimensions say 50x50.

不要使用随机数。您不知道标签和复选框的文本是否可以以 50 像素显示。我已经告诉过你如何使面板尺寸更大。阅读有关边框的教程!!!

static GraphicsConfiguration gc;
static JFrame frame = new JFrame(gc);

您仍在使用静态变量。摆脱他们,他们不需要。

frame.setVisible(true);  

这应该是所有组件都添加到框架后的最后一条语句。实际上代码应该是:

frame.pack();
frame.setVisible( true );

然后所有组件将以其首选大小很好地显示。

阅读教程 教程中的每个部分都有您可以下载和使用的工作示例。本教程将向您展示如何更好地构建代码。