• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

using image and color as a background on JFrame

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}


}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic