permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes using image and color as a background on JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "using image and color as a background on JFrame" Watch "using image and color as a background on JFrame" New topic
Author

using image and color as a background on JFrame

oghenekaro EFEKODO
Ranch Hand

Joined: Oct 10, 2005
Posts: 56
need help changing the background of my programe JFrame.I used "setBackground(Color.pink)" but it only flash and not change.what do i do.Also,i want to use image as background,how i do it?.

my code:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class FoodDailog extends javax.swing.JFrame {
private Carbohydrate cc;
private javax.swing.JSeparator labelseparator;
public FoodDailog(){
//Container c=getContentPane();
setBackground(Color.pink);
//setOpaque(true);
setLayout(null);


javax.swing.JLabel label=new javax.swing.JLabel("Please Make A Suitable Chioce Below");
label.setBounds(120,10,260,30);
add(label);

labelseparator=new javax.swing.JSeparator();
labelseparator.setBounds(120,40,220,50);
add(labelseparator);
//to access a local variable from within an inner class we declear if to be final
final javax.swing.JButton carbohydrateButton=new javax.swing.JButton("Carbohydrate Food");
carbohydrateButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
carbohydrateButton.setPreferredSize(new Dimension(150,70));
carbohydrateButton.setBounds(120,50,200,50);
carbohydrateButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==carbohydrateButton)
cc=new Carbohydrate();
cc.setVisible(true);
cc.setLocation(90,80);

}
}
);
add(carbohydrateButton);

javax.swing.JButton proteinButton=new javax.swing.JButton("Protein Food");
proteinButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
proteinButton.setPreferredSize(new Dimension(150,70));
proteinButton.setBounds(120,100,200,50);
add(proteinButton);

javax.swing.JButton mineralsButton=new javax.swing.JButton("Minerals Food");
mineralsButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
mineralsButton.setPreferredSize(new Dimension(150,70));
mineralsButton.setBounds(120,150,200,50);
add(mineralsButton);

javax.swing.JButton vitaminsButton=new javax.swing.JButton("Vitamins Food");
vitaminsButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
vitaminsButton.setPreferredSize(new Dimension(150,70));
vitaminsButton.setBounds(120,200,200,50);
add(vitaminsButton);

javax.swing.JButton fatsButton=new javax.swing.JButton("Fats And Oil Food");
fatsButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
fatsButton.setPreferredSize(new Dimension(150,70));
fatsButton.setBounds(120,250,200,50);
add(fatsButton);

javax.swing.JButton waterButton=new javax.swing.JButton("Water Intake");
waterButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
waterButton.setPreferredSize(new Dimension(150,70));
waterButton.setBounds(120,300,200,50);
add(waterButton);

javax.swing.JButton closeButton=new javax.swing.JButton(new ImageIcon("close.gif"));
closeButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
closeButton.setPreferredSize(new Dimension(70,65));
closeButton.setToolTipText("Close");
closeButton.setBounds(450,400,30,30);
closeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
);
add(closeButton);

//Icon icon=new ImageIcon("flower.gif");
//javax.swing.JFrame.add(icon);

setUndecorated(true);
setSize(500,450);
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocation(100,100);
}


public static void main(String k[]){
new FoodDailog();
}
}
class Carbohydrate extends javax.swing.JFrame{
private DefaultListModel carbohydrateFood;
private javax.swing.JList list;

public Carbohydrate(){

carbohydrateFood=new DefaultListModel();
carbohydrateFood.addElement("Rice");
carbohydrateFood.addElement("Maize");

list=new javax.swing.JList(carbohydrateFood);

//allow user to select only one food at a time
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

//create JButton for adding food
javax.swing.JButton addButton=new javax.swing.JButton("Add Food");
addButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//prompt user for new food name
String name=JOptionPane.showInputDialog(Carbohydrate.this,"Enter");

//add new food to model
carbohydrateFood.addElement(name);
}
}
);
//create JButton for removing selected food
javax.swing.JButton removeButton=new javax.swing.JButton("Remove Food");
removeButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//remove selected food from model
carbohydrateFood.removeElement(list.getSelectedValue());

}
}
);

//lay out GUI components
javax.swing.JPanel inputPanel=new javax.swing.JPanel();
inputPanel.setPreferredSize(new Dimension(50,50));
inputPanel.add(addButton);
inputPanel.add(removeButton);

//Container container=getContentPane();
add(list,BorderLayout.CENTER);
add(inputPanel,BorderLayout.SOUTH);

setSize(300,300);
}


}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
unable to test 1.5 at present, but for 1.4

setBackground(Color.pink);
needs to be
getContentPane().setBackground(Color.pink);

for a background image on the frame you will have to override the frame's paint().
this is generally not a good idea - far better to use a JPanel, overriding
paintComponent(), and setting the panel as the contentPane

here's the BackgroundImagePanel FAQ

http://faq.javaranch.com/view?BackgroundImageOnJPanel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using image and color as a background on JFrame
 
Similar Threads
Scrollbar not move down in JTable
linking splash screen to my code
GUI control
First time using an applet in a website
Is this really where actionlistener should go?