设置背景颜色[JAVA]

时间:2013-11-24 13:22:38

标签: java swing

我正处于应用程序的中间,背景看起来很简单。 我尝试使用我的知识代码设置背景,这就是:

 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);


    }

1 个答案:

答案 0 :(得分:2)

将面板添加到JFrame时,它会占用所有视图。不要改变JFrame ContentPane的颜色,而是尝试更改面板的背景颜色。