有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在一个窗口中制作两种颜色?

我想这样做:

the image

代码如下:

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

class QuizGUI {
    public static void main(String args[]) {
        JFrame frm = new JFrame("Simple Quiz");
        frm.setLayout(null);
        JLabel lbl1 = new JLabel("Which Animal can fly?");
        JLabel lbl2 = new JLabel("You have selected: ");
        JLabel lblOutput = new JLabel();
        JRadioButton rCat = new JRadioButton("Cat");
        JRadioButton rBird = new JRadioButton("Bird");
        JRadioButton rFish = new JRadioButton("Fish");
        ButtonGroup bg = new ButtonGroup();

        bg.add(rCat);
        bg.add(rBird);
        bg.add(rFish);

        lbl1.setBounds(0, 0, 200, 20);
        rCat.setBounds(0, 20, 100, 20);
        rBird.setBounds(0, 40, 100, 20);
        rFish.setBounds(0, 60, 100, 20);
        lbl2.setBounds(0, 80, 200, 20);
        lblOutput.setBounds(0, 105, 200, 20);

        frm.add(lbl1);
        frm.add(rCat);
        frm.add(rBird);
        frm.add(rFish);
        frm.add(lbl2);
        frm.add(lblOutput);

        rCat.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if (rCat.isSelected()) {
                    lblOutput.setText("Cat can't fly, Try again.");
                }
            }
        });

        rBird.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if (rBird.isSelected()) {
                    lblOutput.setText("Bird can fly, Excellent.");
                }
            }
        });

        rFish.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if (rFish.isSelected()) {
                    lblOutput.setText("Cat can't fly, Try again.");
                }
            }
        });

        frm.setVisible(true);
        frm.setSize(350, 200);

        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

问题是,我想要像窗口一样的图像的颜色,背景是白色,选择的背景是灰色

我试过了frame.setBackground,但没用

我尝试了另一个例子的一些代码,颜色是白色的。我不知道为什么窗户都是这样的灰色:

img


共 (2) 个答案

  1. # 1 楼答案

    根据您在问题中发布的代码:

    frm.setLayout(null);
    

    这不是个好主意。我建议总是使用layout managerJFrame是一个top-level container。它有一个内容窗格,默认情况下是JPanel。内容窗格的默认布局管理器是BorderLayout。您可以参考JFrame的源代码来确认这一点

    在我看来BorderLayout适合您的GUI。一个JPanel是北方部分,它显示了一个问题,即哪种动物会飞,单选按钮是中心组件,您选择的文本是南面板

    每个JPanel可以有自己的背景色。我在Windows 10上使用JDK 13,默认背景色为灰色。因此,在下面的代码中,我设置了南北面板的背景色,并保留了中间面板的默认背景色

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.WindowConstants;
    
    public class QuizGUI implements ActionListener, Runnable {
        private static final String BIRD = "Bird";
        private static final String CAT = "Cat";
        private static final String FISH = "Fish";
    
        private JFrame frame;
        private JLabel resultLabel;
    
        @Override // java.awt.event.ActionListener
        public void actionPerformed(ActionEvent event) {
            String actionCommand = event.getActionCommand();
            switch (actionCommand) {
                case BIRD:
                    resultLabel.setText(BIRD + " can fly. Excellent.");
                    break;
                case CAT:
                    resultLabel.setText(CAT + " can't fly. Try again.");
                    break;
                case FISH:
                    resultLabel.setText(FISH + " can't fly. Try again.");
                    break;
                default:
                    resultLabel.setText(actionCommand + " is not handled.");
            }
        }
    
        @Override // java.lang.Runnable
        public void run() {
            createAndShowGui();
        }
    
        private void createAndShowGui() {
            frame = new JFrame("Simple Quiz");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.add(createQuestionPanel(), BorderLayout.PAGE_START);
            frame.add(createChoicesPanel(), BorderLayout.CENTER);
            frame.add(createOutcomePanel(), BorderLayout.PAGE_END);
            frame.setSize(350, 200);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private void createRadioButton(String text, ButtonGroup bg, JPanel panel) {
            JRadioButton radioButton = new JRadioButton(text);
            radioButton.addActionListener(this);
            bg.add(radioButton);
            panel.add(radioButton);
        }
    
        private JPanel createChoicesPanel() {
            JPanel choicesPanel = new JPanel(new GridLayout(0, 1));
            ButtonGroup bg = new ButtonGroup();
            createRadioButton(CAT, bg, choicesPanel);
            createRadioButton(BIRD, bg, choicesPanel);
            createRadioButton(FISH, bg, choicesPanel);
            return choicesPanel;
        }
    
        private JPanel createOutcomePanel() {
            JPanel outcomePanel = new JPanel(new GridLayout(0, 1, 0, 5));
            outcomePanel.setBackground(Color.WHITE);
            JLabel promptLabel = new JLabel("You have selected:");
            setBoldFont(promptLabel);
            outcomePanel.add(promptLabel);
            resultLabel = new JLabel("    ");
            outcomePanel.add(resultLabel);
            return outcomePanel;
        }
    
        private JPanel createQuestionPanel() {
            JPanel questionPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
            questionPanel.setBackground(Color.WHITE);
            JLabel questionLabel = new JLabel("Which Animal can fly?");
            setBoldFont(questionLabel);
            questionPanel.add(questionLabel);
            return questionPanel;
        }
    
        private void setBoldFont(JLabel label) {
            Font boldFont = label.getFont().deriveFont(Font.BOLD);
            label.setFont(boldFont);
        }
    
        public static void main(String[] args) {
            String slaf = UIManager.getSystemLookAndFeelClassName();
            try {
                UIManager.setLookAndFeel(slaf);
            }
            catch (ClassNotFoundException |
                   IllegalAccessException |
                   InstantiationException |
                   UnsupportedLookAndFeelException x) {
                System.out.println("WARNING (ignored): Failed to set [system] look-and-feel");
                x.printStackTrace();
            }
            EventQueue.invokeLater(new QuizGUI());
        }
    }
    
  2. # 2 楼答案

    首先在QuizGUI类中创建一个名为contentPane的私有JPanel。然后在main方法中键入:

    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    frm.setContentPane(contentPane);
    contentPane.setLayout(null);
    

    然后,用contentPane.add()更改所有frm.add()

    我希望这有帮助