【问题标题】:How to make two colors in one window?如何在一个窗口中制作两种颜色?
【发布时间】:2020-06-22 10:10:50
【问题描述】:

我也想这样做:

代码如下:

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,但没有用。

我为其他示例尝试了一些代码,颜色为白色。我不知道为什么窗口都是这样的灰色:

【问题讨论】:

  • 每个容器(通常是JPanel)都可以有不同的背景颜色,因此请将它们组合起来。
  • 在更广泛的说明:frm.setLayout(null); 1) 如果需要,为框架设置内容窗格,但不要弄乱现有的内容窗格(这是我的建议)。在框架上调用的某些方法会自动传递到默认内容窗格,而其他方法则不会。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 ..
  • .. white space 的布局内边距和边框。
  • “我试过frame.setBackground,但没有用。” 嗯……“在框架上调用某些方法会自动传递给默认内容窗格,其他人不会。” 我想你刚刚发现了一个不会。该方法可能会传递到底层框架,该框架正被内容窗格覆盖

标签: java swing colors


【解决方案1】:

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

frm.setLayout(null);

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

在我看来,BorderLayout 适合您的 GUI。一个JPanel 是NORTH 组件,它显示问题,即Which Animal can fly?,单选按钮是CENTER 组件,文本You have selected: 是南面板。

然后每个JPanel 可以有自己的背景颜色。我在 Windows 10 上使用 JDK 13,默认背景颜色为灰色。因此,在下面的代码中,我为 NORTH 和 SOUTH 面板设置了背景颜色,并将 CENTER 面板保留为默认背景颜色。

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】:

    首先在您的 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);
    

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

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-21
      • 2021-03-02
      • 1970-01-01
      • 2013-10-07
      • 2020-06-06
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      相关资源
      最近更新 更多