gpt4 book ai didi

java - 设置背景颜色 [JAVA]

转载 作者:行者123 更新时间:2023-11-30 04:08:37 24 4
gpt4 key购买 nike

我正在使用一个应用程序,背景看起来很普通。我尝试使用我所知的代码设置背景,这很简单:

 frame.getContentPane().setBackground(Color.BLUE);

完整:

JFrame frame = new JFrame("HC Navigator");
frame.setVisible(true);
frame.setSize(250, 200);
frame.getContentPane().setBackground(Color.BLUE); // Colour Set Here
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

现在,当我启动它时,它会打开带有蓝色背景的窗口,但一旦加载按钮/Jpanels,背景就会恢复为纯白色。

主面板/JFrame 代码:

    package me.jamplifier;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.DefaultCaret;

public class Launcher extends JFrame {



// Main Class Area (Default/Main Windows)
public Launcher(){

// Main Window
JFrame frame = new JFrame("HC Navigator");
frame.setSize(250, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Text Labels
JLabel label = new JLabel("Beta 1.5 ");
JLabel label1 = new JLabel("Welcome to HurricaneCraft");
JPanel panel = new JPanel();
frame.add(panel);
panel.add(label);
panel.add(label1);

Font font = new Font("Verdana", Font.BOLD, 12);
label1.setFont(font);
label1.setForeground(Color.RED);
label.setFont(font);
label.setForeground(Color.GRAY);

// Buttons
JButton site = new JButton("Site");
site.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://www.hurricanecraft.com");
}
});

JButton forums = new JButton("Forums");
forums.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://www.hurricanecraft.com/forums/");
}
});
JButton apps = new JButton("Member Apps");
apps.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://hurricanecraft.freeforums.org/member-registration-f29.html");
}
});
JButton community = new JButton("Community");
community.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
createFrame2();
}
});
JButton admin = new JButton("Admin Area");
admin.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
createFrame();
}
});
JButton update = new JButton("Update Log");
update.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
createFrame1();
}
});
panel.add(site);
panel.add(forums);
panel.add(community);
panel.add(apps);
panel.add(admin);
panel.add(update);


}

最佳答案

当您将面板添加到 JFrame 时,它​​会占据所有 View 。不要更改 JFrame ContentPane 的颜色,而是尝试更改面板的背景颜色。

关于java - 设置背景颜色 [JAVA],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20174960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com