need help in running a splash screen along with my code and also the class Protein is not display the text in the JLabel.thanks for ever listening hers.the codes is as follows:
splash screen code:
import java.awt.*;
import javax.swing.*;
public class SplashScreen extends JWindow {
//private static FoodDailog dailog;
private int duration;
public SplashScreen(int d) {
duration = d;
}
// A simple little method to show a title screen in the center
// of the screen for the amount of time given in the constructor
public void showSplash() {
JPanel content = (JPanel)getContentPane();
content.setBackground(Color.white);
// Set the window's bounds, centering the window
int width = 450;
int height =215;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-width)/2;
int y = (screen.height-height)/2;
setBounds(x,y,width,height);
// Build the splash screen
JLabel label = new JLabel(new ImageIcon("flower.gif"));
JLabel copyrt = new JLabel
("I Love You", JLabel.CENTER);
copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
content.add(label, BorderLayout.CENTER);
content.add(copyrt, BorderLayout.SOUTH);
Color oraRed = new Color(156, 20, 20, 255);
content.setBorder(BorderFactory.createLineBorder(oraRed, 10));
// Display it
setVisible(true);
// Wait a little while, maybe while loading resources
try { Thread.sleep(duration); } catch (Exception e) {}
setVisible(false);
}
public void showSplashAndExit() {
showSplash();
System.exit(0);
}
public static void main(String[] args) {
// Throw a nice little title page up on the screen first
SplashScreen splash = new SplashScreen(10000);
// Normally, we'd call splash.showSplash() and get on with the program.
// But, since this is only a test...
splash.showSplashAndExit();
//splash
}
}
my main code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FoodDailog extends javax.swing.JFrame {
//private static SplashScreen splash;
private Carbohydrate cc;
private Protein pp;
private javax.swing.JSeparator labelseparator;
/*public void SplashScreen(JFrame parent){
dispose();
}*/
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);
final 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);
proteinButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==proteinButton)
pp=new Protein();
pp.setVisible(true);
pp.setLocation(95,60);
pp.setSize(300,300);
}
}
);
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[]){
//splash=new SplashScreen(1000);
//splash.showSplashAndExit();
try{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}catch(Exception e){
}
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);
}
}
class Protein extends JFrame implements ItemListener{
Choice itemChoice;
JLabel price;
String[] items={"Chicken","Fish","Beans","Beef"};
double[] prices={250,200,150,200};
public Protein(String title){
super(title);
}
public Protein(){
setLayout(new FlowLayout());
add(new JLabel("Select Food"));
itemChoice=new Choice();
for(int i=0;i<items.length;i++){
itemChoice.add(items[i]);
}
add(itemChoice);
itemChoice.addItemListener(this);
price=new JLabel("Select One Of The "+itemChoice.getItemCount()+"Listed Food to see its Price");
price.setFont(new Font("SanSerif",Font.BOLD+Font.ITALIC,16));
price.setForeground(Color.red);
price.setAlignmentX(CENTER_ALIGNMENT);
add(price,"South");
}
public void itemStateChanged(ItemEvent e){
price.setText(itemChoice.getSelectedItem()+"are"+prices[itemChoice.getSelectedIndex()]+"per Naira");
}
}
splash screen code:
import java.awt.*;
import javax.swing.*;
public class SplashScreen extends JWindow {
//private static FoodDailog dailog;
private int duration;
public SplashScreen(int d) {
duration = d;
}
// A simple little method to show a title screen in the center
// of the screen for the amount of time given in the constructor
public void showSplash() {
JPanel content = (JPanel)getContentPane();
content.setBackground(Color.white);
// Set the window's bounds, centering the window
int width = 450;
int height =215;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-width)/2;
int y = (screen.height-height)/2;
setBounds(x,y,width,height);
// Build the splash screen
JLabel label = new JLabel(new ImageIcon("flower.gif"));
JLabel copyrt = new JLabel
("I Love You", JLabel.CENTER);
copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
content.add(label, BorderLayout.CENTER);
content.add(copyrt, BorderLayout.SOUTH);
Color oraRed = new Color(156, 20, 20, 255);
content.setBorder(BorderFactory.createLineBorder(oraRed, 10));
// Display it
setVisible(true);
// Wait a little while, maybe while loading resources
try { Thread.sleep(duration); } catch (Exception e) {}
setVisible(false);
}
public void showSplashAndExit() {
showSplash();
System.exit(0);
}
public static void main(String[] args) {
// Throw a nice little title page up on the screen first
SplashScreen splash = new SplashScreen(10000);
// Normally, we'd call splash.showSplash() and get on with the program.
// But, since this is only a test...
splash.showSplashAndExit();
//splash
}
}
my main code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FoodDailog extends javax.swing.JFrame {
//private static SplashScreen splash;
private Carbohydrate cc;
private Protein pp;
private javax.swing.JSeparator labelseparator;
/*public void SplashScreen(JFrame parent){
dispose();
}*/
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);
final 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);
proteinButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==proteinButton)
pp=new Protein();
pp.setVisible(true);
pp.setLocation(95,60);
pp.setSize(300,300);
}
}
);
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[]){
//splash=new SplashScreen(1000);
//splash.showSplashAndExit();
try{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}catch(Exception e){
}
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);
}
}
class Protein extends JFrame implements ItemListener{
Choice itemChoice;
JLabel price;
String[] items={"Chicken","Fish","Beans","Beef"};
double[] prices={250,200,150,200};
public Protein(String title){
super(title);
}
public Protein(){
setLayout(new FlowLayout());
add(new JLabel("Select Food"));
itemChoice=new Choice();
for(int i=0;i<items.length;i++){
itemChoice.add(items[i]);
}
add(itemChoice);
itemChoice.addItemListener(this);
price=new JLabel("Select One Of The "+itemChoice.getItemCount()+"Listed Food to see its Price");
price.setFont(new Font("SanSerif",Font.BOLD+Font.ITALIC,16));
price.setForeground(Color.red);
price.setAlignmentX(CENTER_ALIGNMENT);
add(price,"South");
}
public void itemStateChanged(ItemEvent e){
price.setText(itemChoice.getSelectedItem()+"are"+prices[itemChoice.getSelectedIndex()]+"per Naira");
}
}