Beginner: Show Image in Label when Results Show Up
Below I am trying to set up so that when the results show up an Image appears in Lb4.
Code :
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Scanner;
import java.util.Random;
public class HighLow extends Applet implements ActionListener
{
//declare variables
Panel nPanel, sPanel, tcPanel, bcPanel, cPanel;
Button one, clear;
Label top,lb1, lb2, lb3, lb4, bottom;
TextField txt;
int number, cnumber,count;
final int MAX = 25;
public void init()
{
//create random number
Random generator = new Random();
cnumber = generator.nextInt(MAX) + 1;
//set north panel
nPanel = new Panel();
nPanel.setBackground(Color.blue);
top = new Label("High/Low Game ~What Number am I thinking of?~");
//set south panel
sPanel = new Panel();
sPanel.setBackground(Color.red);
bottom = new Label("Pick a number between 1 and 25");
//set center, topcenter, bottomcenter panel
cPanel = new Panel();
tcPanel = new Panel();
bcPanel = new Panel();
//add submit buttonm labels and textfield
one = new Button("Submit");
one.addActionListener(this);
clear = new Button("Clear");
clear.addActionListener(this);
one.setPreferredSize(new Dimension(100,100));
clear.setPreferredSize(new Dimension(20,20));
lb1 = new Label();
lb2 = new Label();
lb3 = new Label(" ");
txt = new TextField();
lb4 = new Label(" ");
//set topcenter GridLayout
tcPanel.setLayout (new GridLayout(3,1));
tcPanel.add(lb3);
tcPanel.add(txt);
tcPanel.add(lb2);
//set bottomcenter GridLayout
bcPanel.setLayout (new GridLayout(3,1));
bcPanel.add(lb1);
bcPanel.add(one);
bcPanel.add(lb4);
//combine tc and bc panels
cPanel.setLayout(new BorderLayout(7,1));
//cPanel.add(clear);
cPanel.add(tcPanel, BorderLayout.WEST);
cPanel.add(bcPanel, BorderLayout.EAST);
//set layout and label for top
nPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
nPanel.add(top);
//set layout and label for bottom
sPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
sPanel.add(bottom);
//set layout for the Applet
this.setLayout(new BorderLayout());
add(nPanel, BorderLayout.NORTH);
add(cPanel, BorderLayout.CENTER);
add(sPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
number = Integer.parseInt(txt.getText());
count++;
if (number > cnumber)
{
lb3.setText("Number is too high");
lb3.setBackground(Color.red);
lb4.setText("");
}
else if (number < cnumber)
{
lb2.setText("Number is too low");
lb2.setBackground(Color.yellow);
lb3.setText("");
}
else if (number == cnumber)
{
bottom.setText("Guessed it in " + count + " tries!!");
bottom.setBackground(Color.green);
sPanel.setBackground(Color.green);
top.setText("CONGRATULATIONS!");
top.setBackground(Color.green);
nPanel.setBackground(Color.green);
//bottom.setText("It only took you " + count + " guesses!!!");
}
}
}
So I would need to add it to this statement because its when the results show. Im not sure of the code I would use get get the image to show up in the lower right Panel.
Code :
else if (number == cnumber)
{
bottom.setText("Guessed it in " + count + " tries!!");
bottom.setBackground(Color.green);
sPanel.setBackground(Color.green);
top.setText("CONGRATULATIONS!");
top.setBackground(Color.green);
nPanel.setBackground(Color.green);
lb4.getImage("winner.jpg);/////////////////////////////something like this will not work I need some code. Please help
Re: Beginner: Show Image in Label when Results Show Up
So if I understand the question correctly, you wish to set the icon of a JLabel. Firstly, see the API for JLabel. It defines the setIcon method which can be used to set the icon. Depending upon where the image is, you can then pass an ImageIcon defined by the file to this method.
Re: Beginner: Show Image in Label when Results Show Up
I need it so when the results show, so does the image. You know what im saying
Re: Beginner: Show Image in Label when Results Show Up
Sorry I guess Im still not understaning how to incorporate this in my code.
What I have am I close?
Code :
else if (number == cnumber)
{
bottom.setText("Guessed it in " + count + " tries!!");
bottom.setBackground(Color.green);
sPanel.setBackground(Color.green);
top.setText("CONGRATULATIONS!");
top.setBackground(Color.green);
nPanel.setBackground(Color.green);
ImageIcon icon = new ImageIcon("winner.gif");////////////added
JLabel img = new JLabel(icon);//////////////////////////////added
}
}
}