简体   繁体   中英

Setting BackGround Colour [JAVA]

I am in the middle of an application and the background looks way to plain. I have tried setting a background using the code of my knowledge which is simply this:

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

Full:

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

Now when I launch that it opens up the window with a blue background but then once it loads the buttons/Jpanels the background goes back to plain white.

The Main Panel/JFrame Code:

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


    }

When you add the panel to the JFrame it occupies all the view. Instead of changing the color of the JFrame ContentPane, try to change the background color of the panel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM