[英]How to make two colors in one window?
I want to do the same like this:我想做同样的事情:
Here's the code:这是代码:
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);
}
}
The problem is, I want the colors of window like image the background is white and the background for choices is gray.问题是,我想要像图像一样的窗口颜色,背景是白色,选择的背景是灰色。
I tried frame.setBackground
but doesn't work.我试过
frame.setBackground
但不起作用。
I tried some codes for another examples and the color was white.我为另一个例子尝试了一些代码,颜色是白色的。 I don't know why the window is all gray like this:
我不知道为什么窗口都是这样的灰色:
From the code you posted in your question:从您在问题中发布的代码:
frm.setLayout(null);
This is not a good idea.这不是一个好主意。 I recommend always using a layout manager .
我建议始终使用布局管理器。
JFrame
is a top-level container . JFrame
是一个顶级容器。 It has a content pane which, by default, is a JPanel
.它有一个内容窗格,默认情况下是一个
JPanel
。 The default layout manager for the content pane is BorderLayout
.内容窗格的默认布局管理器是
BorderLayout
。 You can refer to the source code for JFrame
in order to confirm this.您可以参考
JFrame
的源代码以确认这一点。
In my opinion BorderLayout
is suitable for your GUI.我认为
BorderLayout
适合您的 GUI。 One JPanel
is the NORTH component and it displays the question, namely Which Animal can fly?一个
JPanel
是 NORTH 组件,它显示了一个问题,即哪种动物可以飞? , the radio buttons are the CENTER component and the text You have selected: is the SOUTH panel. ,单选按钮是 CENTER 组件,您选择的文本:是 SOUTH 面板。
Each JPanel
can then have its own background color.然后每个
JPanel
可以有自己的背景颜色。 I am using JDK 13 on Windows 10 and the default background color is gray.我在 Windows 10 上使用 JDK 13,默认背景颜色为灰色。 Hence, in the code below, I set the background color for the NORTH and SOUTH panels and leave the CENTER panel with its default background color.
因此,在下面的代码中,我为 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());
}
}
First create a private JPanel
called contentPane
in your QuizGUI class.首先在 QuizGUI 类中创建一个名为
contentPane
的私有JPanel
。 Then in your main
method, type:然后在您的
main
方法中,键入:
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
frm.setContentPane(contentPane);
contentPane.setLayout(null);
then, change all frm.add()
with contentPane.add()
然后,使用
contentPane.add()
更改所有frm.add()
contentPane.add()
I hope this helped!我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.