#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    BackGround Color Java


    Hello. I am doing a project atm using JPanel. I am trying to set a backgroundcolor. I have tried a couple of stuff, nothing seems to be working. If someone could plz help.

    Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.event.*;
    import javax.swing.JPanel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class SkoleFrame extends JFrame
    {
        private JLabel lblNavn;
        private JLabel lblAdresse;
        private JLabel lblUddannelser;
        private JLabel lblAntalStuderende;
        private JLabel lblStoreUddannelser;
        private JTextField txfNavn;
        private JTextField txfAdresse;
        private JTextField txfUddannelser;
        private JTextField txfAntalStuderende;
        private JTextField txfSnitAntalStud;
        private JTextField txfAntalItUddannelser;
        private JTextField txfStoersteUddannelse;
        private JTextField txfStoreUddannelser;
        private JButton btnVis;
        private JButton btnNaeste;
        private JButton btnOpdater;
        private JButton btnSlet;
        private JButton btnOpretSkole;
        private JButton btnSum;
        private JButton btnSnitAntalStud;
        private JButton btnAntalItUddannelser;
        private JButton btnStoreUdd;
        private JButton btnStoersteUddannelse;
        private JButton btnOpretUddannelse;
        private Controller controller;
    
        
        public SkoleFrame()
        {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLayout(null);
            this.setTitle("Skole GUI");
            this.setLocation(300, 200);
            this.setSize(550, 450);
    
            lblNavn = new JLabel("Skolens navn:");
            lblNavn.setSize(100,25);
            lblNavn.setLocation(20, 20);
            this.add(lblNavn);
            
            lblAdresse = new JLabel("Skolens adresse:");
            lblAdresse.setSize(100,25);
            lblAdresse.setLocation(20, 70);
            this.add(lblAdresse);
            
            lblUddannelser = new JLabel("Uddannelser:");
            lblUddannelser.setSize(100,25);
            lblUddannelser.setLocation(20, 120);
            this.add(lblUddannelser);
            
            lblAntalStuderende = new JLabel("Antal Studerende:");
            lblAntalStuderende.setSize(150,25);
            lblAntalStuderende.setLocation(20, 170);
            this.add(lblAntalStuderende);
            
            txfNavn = new JTextField();
            this.add(txfNavn);
            txfNavn.setLocation(175, 20);
            txfNavn.setSize(200, 25);
            
            txfAdresse = new JTextField();
            this.add(txfAdresse);
            txfAdresse.setLocation(175, 70);
            txfAdresse.setSize(200, 25);
            
            txfUddannelser = new JTextField();
            this.add(txfUddannelser);
            txfUddannelser.setLocation(175, 120);
            txfUddannelser.setSize(200, 25);
            
            txfAntalStuderende = new JTextField();
            this.add(txfAntalStuderende);
            txfAntalStuderende.setLocation(175, 170);
            txfAntalStuderende.setSize(200, 25);
            
            txfSnitAntalStud = new JTextField();
            this.add(txfSnitAntalStud);
            txfSnitAntalStud.setLocation(175,220);
            txfSnitAntalStud.setSize(200,25);
    
            txfAntalItUddannelser = new JTextField();
            this.add(txfAntalItUddannelser);
            txfAntalItUddannelser.setLocation(175,270);
            txfAntalItUddannelser.setSize(200,25);
            
            txfStoersteUddannelse = new JTextField();
            this.add(txfStoersteUddannelse);
            txfStoersteUddannelse.setLocation(175,320);
            txfStoersteUddannelse.setSize(200,25);
            
            controller = new Controller();
            
            btnVis = new JButton("Vis");
            this.add(btnVis);
            btnVis.setLocation(150, 370);
            btnVis.setSize(100, 25);
            btnVis.addActionListener(controller);
            
            btnNaeste = new JButton("Næste");
            this.add(btnNaeste);
            btnNaeste.setLocation(400, 175);
            btnNaeste.setSize(100,25);
            btnNaeste.addActionListener(controller);
            
            btnOpdater = new JButton("Opdater");
            this.add(btnOpdater);
            btnOpdater.setLocation(250, 370);
            btnOpdater.setSize(100, 25);
            btnOpdater.addActionListener(controller);
            
            btnSlet = new JButton("Slet");
            this.add(btnSlet);
            btnSlet.setLocation(350, 370);
            btnSlet.setSize(100, 25);
            btnSlet.addActionListener(controller);
            
            btnOpretSkole = new JButton("Opret Skole");
            this.add(btnOpretSkole);
            btnOpretSkole.setLocation(400, 20);
            btnOpretSkole.setSize(100,100);
            btnOpretSkole.addActionListener(controller);
            
            btnOpretUddannelse = new JButton ("Opret Uddannelse");
            this.add(btnOpretUddannelse);
            btnOpretUddannelse.setLocation(400, 135);
            btnOpretUddannelse.setSize(100,25);
            btnOpretUddannelse.addActionListener(controller);
            
            btnSum = new JButton("Sum stud.");
            this.add(btnSum);
            btnSum.setLocation(50, 370);
            btnSum.setSize(100,25);
            btnSum.addActionListener(controller);
            
            btnSnitAntalStud = new JButton ("Gennemsnit Studerende");
            this.add(btnSnitAntalStud);
            btnSnitAntalStud.setLocation(20,220);
            btnSnitAntalStud.setSize(150,25);
            btnSnitAntalStud.addActionListener(controller);
            
            btnAntalItUddannelser = new JButton ("Antal IT Uddannelser");
            this.add(btnAntalItUddannelser);
            btnAntalItUddannelser.setLocation(20,270);
            btnAntalItUddannelser.setSize(150,25);
            btnAntalItUddannelser.addActionListener(controller);
            btnSum.addActionListener(controller);
    
            btnStoersteUddannelse = new JButton ("Største Uddannelse");
            this.add(btnStoersteUddannelse);
            btnStoersteUddannelse.setLocation(20,320);
            btnStoersteUddannelse.setSize(150,25);
            btnStoersteUddannelse.addActionListener(controller);
            
    
            
            this.setVisible(true);
        }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The posted code has compiler errors. Missing class definition

    Where are there any colors used in this code?
  4. #3
  5. No Profile Picture
    Choose Life...
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Location
    Scotland
    Posts
    461
    Rep Power
    513
    Your question isn't very specific...what are you trying to set the background color of?

    Quick Google and I found this site which demonstrates how to set the background color of a JPanel (assumed this is what you want to set the background color for).

    http://java2everyone.blogspot.co.uk/...und-color.html
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0
    From what I've understood, you're trying to set the background color of a JPanel component.
    Put this line in SkoleFrame's constructor:
    Code:
    setBackground(Color.WHITE);

IMN logo majestic logo threadwatch logo seochat tools logo