简体   繁体   English

JPanel带有3个复选框,可在JFrame的中心设置背景色

[英]JPanel with 3 check boxes to set background color in the centre of JFrame

I'm designing a JPanel with three check boxes labelled “red”, “green”, and “blue” that add a red, green, or blue component to the background color of a JPanel in the centre of the GUI. 我正在设计一个带有三个标记为“红色”,“绿色”和“蓝色”的复选框的JPanel,它们在GUI中心的JPanel的背景色中添加了红色,绿色或蓝色成分。 Initially, the JPanel has the “blue” JCheckBox ticked and so the JPanel is colored in blue. 最初,JPanel勾选了“蓝色” JCheckBox,因此,JPanel用蓝色着色。 As different check boxes are ticked, the appropriate color combination appears on the JPanel which can display a total of eight color combinations. 勾选不同的复选框后,JPanel上将显示适当的颜色组合,该颜色组合可显示总共八种颜色组合。

It should be like the pictures below: 它应该像下面的图片:

输出量

The codes for the program is: 该程序的代码是:

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

public class ColorDemo extends JFrame implements ItemListener
{
    int r=0,g=0,b=0;
    JCheckBox red,green,blue;
    JPanel P = new JPanel();
    JPanel cpanel = new JPanel();
    Container pane = getContentPane();


    ColorDemo(String cd){
        super(cd);
        red = new JCheckBox("red");
        red.addItemListener(this);

        green = new JCheckBox("green");
        green.addItemListener(this);

        blue = new JCheckBox("blue");
        blue.setSelected(true);
        blue.addItemListener(this);        

        cpanel.add(red);
        cpanel.add(green);
        cpanel.add(blue);

        getContentPane().add(cpanel,"North");
        setSize(400,400);
        setVisible(true);        

        getContentPane().add(P);
        P.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        setVisible(true);

    }

    public static void main(String[] args)
    {
        ColorDemo cd = new ColorDemo("Color Check Box");
        cd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void itemStateChanged(ItemEvent ie){

        if(ie.getItem() == red)
        if(red.isSelected()) r=255; else r=0;
            if(ie.getItem() == green)
        if(green.isSelected()) g=255; else g=0;
            if(ie.getItem() == blue)
        if(blue.isSelected()) b=255; else b=0;

        P.setBackground(new Color(r,g,b));            

    }

}

Now the issue I'm having is: 现在我遇到的问题是:

*** It's showing the color as background, not boxed in center. ***它将颜色显示为背景,而不是居中显示。 See the pic: 看图片:

我的代码输出

*** Also as per the requirement, the blue checkbox is initially checked but not showing the color on the JFrame. ***同样根据要求,最初选中了蓝色复选框,但未在JFrame上显示颜色。

I'm bit confused (as I'm very much new to Programming), Any suggestions would be highly appreciated. 我有点困惑(因为我对编程非常陌生),任何建议将不胜感激。 Thanks. 谢谢。

Directly before your setVisible(true) change the code to this: setVisible(true)之前,将代码更改为此:

 panel = new JPanel();
    getContentPane().add(panel, BorderLayout.CENTER);
    panel.setLayout(new CardLayout(100, 100));
    panel.add(P, "name");
    P.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    P.setBackground(new Color(0,0,255));

This will stop the color from being on the whole frame. 这样可以防止颜色出现在整个画面上。 This also makes the default color blue instead of the original grey default. 这也使默认颜色为蓝色,而不是原始的灰色默认值。

What I did was create a JPanel with your panel inside it with a card layout with a gap of 100 for horizontal and vertical. 我所做的就是创建一个JPanel,其中的面板内部有一个卡布局,水平和垂直间距为100。

Try using a Layout Manager 尝试使用布局管理器

https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html

And you might perhaps need to add an empty border depending on the layout you use. 您可能需要根据使用的布局添加一个空边框。

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

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