setOpaque为true,但JPanel不会改变背景颜色

时间:2017-05-04 21:21:44

标签: java user-interface jpanel

我正在制作带GUI的餐厅菜单程序。

我创建了3个JPanel并将Background(Color.white)设置为其中一个。

但是java不会显示那种背景颜色。

出了什么问题?

这是我的代码

请不要注意食物'价格和cardLayout。没有完成编码。

public class MainDishPanel extends JPanel {

JCheckBox box1 = new JCheckBox("Hamburger 5 dollars"),
          box2 = new JCheckBox("Pizza 5 dollars"),
          box3 = new JCheckBox("French Hot dog 5 dollars");
MainDishPanel(){
   setOpaque(true);
   setBackground(Color.white);
   setLayout(new GridLayout(3,1));
  // setBorder();

  add(box1);add(box2);add(box3);
    setVisible(true);
}
}

使用main方法的类:

 import java.awt.BorderLayout;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
public class RestaurantMenu extends JFrame implements ActionListener{



/* private JPanel maindishPanel= new JPanel(),
                   dessertPanel = new JPanel(),
                   drinkPanel = new JPanel(),*/
    private JPanel leftPanel = new JPanel(),
                   rightPanel = new JPanel();
    private MainDishPanel maindish = new MainDishPanel();
    private DessertPanel dessert = new DessertPanel();
    private DrinkPanel drink = new DrinkPanel();
    private LinkedList<Double>price = new LinkedList<>();
    private JButton left = new JButton("previous"),
                    right = new JButton("next");
    public int page =1;
     private JLabel pageLabel = new JLabel(String.valueOf(page)),
                    foodTypeLabel = new JLabel();
     CardLayout cards;
     GridBagLayout grid;
    RestaurantMenu(){
        setTitle("Main Dish");
        GridBagConstraints gbc = new GridBagConstraints();
        add(foodTypeLabel, BorderLayout.NORTH);
        add(pageLabel, BorderLayout.SOUTH);
        add(leftPanel, BorderLayout.WEST);
        add(rightPanel, BorderLayout.EAST);
        add(maindish, BorderLayout.CENTER);

        grid = new GridBagLayout();
        leftPanel.setLayout(grid); leftPanel.add(left);
        rightPanel.setLayout(grid); rightPanel.add(right);
        right.addActionListener(this);
        left.addActionListener(this);
        //cards = new CardLayout();
        /*cards = (CardLayout)maindishPanel.getLayout();
        cards.show(maindishPanel,"Main Dish");*/
        setVisible(true);
        setSize(500,500);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==right)
            cards.next(this);
        if(e.getSource()==left)
            cards.previous(this);
    }
    public static void main (String []args){
        RestaurantMenu rm = new RestaurantMenu();

    }
}

我没有收到任何错误。背景颜色刚好被改为白色。

RestaurantMenu课程可能看起来像意大利面条代码或奇怪的东西

因为我和这堂课有很多关系。

1 个答案:

答案 0 :(得分:0)

在您的MainDishPanel中,您需要将JCheckBox的背景颜色设置为白色,或者您可以在JCheckBox上将setOpaque设置为false

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

public class MainDishPanel extends JPanel {

    JCheckBox box1 = new JCheckBox("Hamburger 5 dollars"), box2 = new JCheckBox("Pizza 5 dollars"),
        box3 = new JCheckBox("French Hot dog 5 dollars");

    MainDishPanel() {
        setOpaque(true);
        setBackground(Color.WHITE);
        setLayout(new GridLayout(3, 1));

        box1.setBackground(Color.WHITE); // or box1.setOpaque(false);
        box2.setBackground(Color.WHITE);
        box3.setBackground(Color.WHITE);

        add(box1);
        add(box2);
        add(box3);
        setVisible(true);
    }
}