1 Replies - 1320 Views - Last Post: 13 May 2010 - 03:29 AM Rate Topic: -----

#1 Nameo0  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 75
  • Joined: 30-November 09

getting an image to displlay in a jframe with menu bar

Posted 12 May 2010 - 06:00 PM

the code builds and runs, but only one problem
the image: arena
will not appear a in the jframe

import java.text.*;
import javax.swing.*;
import java.lang.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

class rps extends JFrame implements ActionListener {
	
	private static final int FRAME_WIDTH = 1000;
	private static final int FRAME_HEIGHT = 500;
	private static final int FRAME_X_ORIGIN = 150;
	private static final int FRAME_Y_ORIGIN = 250;
	private static final int RPSMAX = 3;
	private static final int RPSMIN = 1;
	
	private JLabel	response;
	private JLabel 	image;
	private JMenu	fileMenu;
	private JMenu	editMenu;
	
	int rock = 1;
	int paper = 2;
	int scissors = 3;
	int cpu, player, draw = 0;
	int playerChoose, cpuChoose = 0;
	String winner;
	
	public static void main (String[] args){
		rps frame = new rps();
		frame.setVisible(true);
	}
	
	public rps(){
		
		Container contentPane;
		
		setTitle ("Rock, Paper, Scissors, and maybe more");
		setSize (FRAME_WIDTH, FRAME_HEIGHT);
		setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
		
		contentPane = getContentPane();
		contentPane.setLayout(new FlowLayout());
		
		image = new JLabel(new ImageIcon(" arena.jpg "));
		image.setSize( 1000,500 );
		contentPane.add(image);
		
		createFileMenu();
		createEditMenu();
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar (menuBar);
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		
		response = new JLabel ("Hello, this is your menu tester.");
		response.setSize(250, 50);
		contentPane.add(response);
		
		setDefaultCloseOperation (EXIT_ON_CLOSE);		
	}
	
	public void actionPerformed(ActionEvent event) {
		
		String menuName;
		
		menuName = event.getActionCommand();
		
		if (menuName.equals("QUIT")){
			System.exit(0);
		}
		else {
			response.setText("Menu Item ' " + menuName + " ' is selected.");
		}
	}
	
	private void createFileMenu(){
		JMenuItem item;
		
		fileMenu = new JMenu("File");
		
		item = new JMenuItem ("New Game");
		item.addActionListener(this);
		fileMenu.add(item);
		
		item = new JMenuItem ("View Stats");
		item.addActionListener(this);
		fileMenu.add(item);	
		
		item = new JMenuItem ("QUIT");
		item.addActionListener(this);
		fileMenu.add(item);				
	}
	
	private void createEditMenu(){
		
		JMenuItem item;
		
		editMenu = new JMenu("Edit");
		
		item = new JMenuItem ("Change Game Type");
		item.addActionListener(this);
		editMenu.add(item);
	}
	
	private void rpsCpuChoose(){
		
		cpuChoose = (int) (Math.floor(Math.random()*(RPSMAX - RPSMIN + 1)) + RPSMIN);
		if (cpuChoose == 1){
			cpuChoose = rock;
		}
		else if (cpuChoose == 2){
			cpuChoose = paper;
		}
		else if (cpuChoose == 3){
			cpuChoose = scissors;
		}
	}
	
	private	void scorer(){
		
		if (playerChoose > cpuChoose){
			player++;
			winner = "Player";
		}
		else if (playerChoose < cpuChoose){
			cpu++;
			winner = "Computer";
		}
		else if (playerChoose == cpuChoose){
			draw++;
			winner = "This round is a draw";
		}
	}
}



i don't know if it is possible, but is it possible to set an image as a background, because if it is, that is what i am trying to do with the image

Is This A Good Question/Topic? 0
  • +

Replies To: getting an image to displlay in a jframe with menu bar

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2619
  • View blog
  • Posts: 11,068
  • Joined: 20-September 08

Re: getting an image to displlay in a jframe with menu bar

Posted 13 May 2010 - 03:29 AM

Quote

image = new JLabel(new ImageIcon(" arena.jpg "));



You have spaces around the file name. You need

image = new JLabel(new ImageIcon("arena.jpg"));


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1