Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Text Not Setting

    I am helping a friend build a game and i have hit a bit of a snag. I am taking random items from a text file and putting them into an array list. Then i am randomly grabbing the items for a max of 6 items. I have checked and everything is being taken and set correctly but for some reason the text on the JToggleButtons is not being set correctly. Here is the code.

    Java Code:
    import java.awt.BorderLayout;
    
    
    @SuppressWarnings({ "serial", "unused" })
    public class MainMenu extends JFrame {
    
    	private JPanel contentPane;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					MainMenu frame = new MainMenu();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public MainMenu() {
    		setTitle("Loot Wars");
    		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Ian\\Desktop\\lootwarsicon.jpg"));
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 700);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		JButton MainMenuButton = new JButton("Main");
    		MainMenuButton.setBounds(0, 614, 89, 50);
    		contentPane.add(MainMenuButton);
    		
    		JButton HomeButton = new JButton("Home");
    		HomeButton.setBounds(88, 614, 89, 50);
    		contentPane.add(HomeButton);
    		
    		JButton LootButton = new JButton("Loot!");
    		LootButton.setBounds(175, 614, 89, 50);
    		lootMenu lm = new lootMenu();
    		LootButton.addActionListener(lm);
    		contentPane.add(LootButton);
    		
    		JButton TownButton = new JButton("Town");
    		TownButton.setBounds(261, 614, 89, 50);
    		contentPane.add(TownButton);
    		
    		JButton ArenaButton = new JButton("Arena");
    		ArenaButton.setBounds(345, 614, 89, 50);
    		contentPane.add(ArenaButton);
    		
    		JMenuBar menuBar = new JMenuBar();
    		menuBar.setBounds(0, 0, 434, 21);
    		contentPane.add(menuBar);
    		
    		JMenu File = new JMenu("File");
    		menuBar.add(File);
    		
    		JMenuItem Open = new JMenuItem("Open");
    		File.add(Open);
    		
    		JMenu mnOptions = new JMenu("Options");
    		menuBar.add(mnOptions);
    		
    		JMenuItem mntmIdkYet = new JMenuItem("IDK yet");
    		mnOptions.add(mntmIdkYet);
    		
    		JButton SpecialButton = new JButton("Special Items");
    		SpecialButton.setBounds(10, 138, 125, 34);
    		contentPane.add(SpecialButton);
    		
    		JButton WorkBench = new JButton("Workbench");
    		WorkBench.setBounds(155, 138, 125, 34);
    		contentPane.add(WorkBench);
    		
    		JButton TrophiesButton = new JButton("Trophies");
    		TrophiesButton.setBounds(299, 138, 125, 34);
    		contentPane.add(TrophiesButton);
    		
    		Panel StatPanel = new Panel();
    		StatPanel.setBounds(10, 27, 414, 105);
    		contentPane.add(StatPanel);
    		StatPanel.setBackground(Color.BLACK);
    		StatPanel.setLayout(null);
    		
    		JLabel nameLabel = new JLabel("NameLabel");
    		nameLabel.setBounds(155, 6, 102, 22);
    		StatPanel.add(nameLabel);
    		nameLabel.setForeground(Color.WHITE);
    		nameLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
    		
    		JLabel AttackLabel = new JLabel("Attack: ????");
    		AttackLabel.setBounds(15, 32, 97, 22);
    		AttackLabel.setForeground(Color.WHITE);
    		StatPanel.add(AttackLabel);
    		AttackLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		
    		JLabel DefenseLabel = new JLabel("Defense: ???");
    		DefenseLabel.setForeground(Color.WHITE);
    		DefenseLabel.setBounds(15, 60, 102, 22);
    		StatPanel.add(DefenseLabel);
    		DefenseLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		
    		JLabel EnergyLabel = new JLabel("Energy: ???");
    		EnergyLabel.setForeground(Color.WHITE);
    		EnergyLabel.setBounds(145, 32, 93, 22);
    		StatPanel.add(EnergyLabel);
    		EnergyLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		
    		JLabel GRCLabel = new JLabel("GRC: ???");
    		GRCLabel.setForeground(Color.WHITE);
    		GRCLabel.setBounds(145, 60, 73, 22);
    		StatPanel.add(GRCLabel);
    		GRCLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		
    		JLabel TimeLabel = new JLabel("more in 12 minutes");
    		TimeLabel.setForeground(Color.WHITE);
    		TimeLabel.setBounds(250, 32, 154, 22);
    		StatPanel.add(TimeLabel);
    		TimeLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		
    		Panel DuffelPanel = new Panel();
    		DuffelPanel.setBackground(Color.BLACK);
    		DuffelPanel.setBounds(10, 212, 414, 37);
    		contentPane.add(DuffelPanel);
    		
    		JLabel DuffelLabel = new JLabel("Duffel Bag");
    		DuffelPanel.add(DuffelLabel);
    		DuffelLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		DuffelLabel.setBackground(Color.BLACK);
    		DuffelLabel.setHorizontalAlignment(SwingConstants.CENTER);
    		DuffelLabel.setForeground(new Color(255, 255, 255));
    		
    		Panel Item1 = new Panel();
    		Item1.setBackground(Color.GRAY);
    		Item1.setBounds(10, 255, 414, 34);
    		contentPane.add(Item1);
    		
    		Panel Item2 = new Panel();
    		Item2.setBackground(Color.GRAY);
    		Item2.setBounds(10, 288, 414, 37);
    		contentPane.add(Item2);
    		
    		Panel Item3 = new Panel();
    		Item3.setBackground(Color.GRAY);
    		Item3.setBounds(10, 324, 414, 37);
    		contentPane.add(Item3);
    		
    		Panel Item4 = new Panel();
    		Item4.setBackground(Color.GRAY);
    		Item4.setBounds(10, 361, 414, 37);
    		contentPane.add(Item4);
    		
    		Panel Item5 = new Panel();
    		Item5.setBackground(Color.GRAY);
    		Item5.setBounds(10, 394, 414, 37);
    		contentPane.add(Item5);
    		
    		Panel Item6 = new Panel();
    		Item6.setBackground(Color.GRAY);
    		Item6.setBounds(10, 426, 414, 37);
    		contentPane.add(Item6);
    		
    		JLabel backG = new JLabel("");
    		backG.setBounds(0, 0, 434, 643);
    		contentPane.add(backG);
    		backG.setIcon(new ImageIcon("C:\\Users\\Ian\\workspace\\LootWars\\src\\LootWarsBackground.jpg"));
    	}
    	public class lootMenu implements ActionListener {
    		public void actionPerformed(ActionEvent ezpz) {
    			setVisible(false);
    			Frame f = new Frame();
    			Loot loot = new Loot();
    			f.setVisible(true);
    			
    		}
    	}
    }
    Java Code:
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.Scanner;
    
    
    
    public class Loot {
    	int item1, item2, item3, item4, item5, item6;
    	String itemOne, itemTwo, itemThree, itemFour, itemFive, itemSix;
    	int Items[] = new int[] {};
    	ArrayList<String> itemNames = new ArrayList<String>();
    	
    	public Loot () {
    		getLootNames();
    		setLoot();
    		System.out.println(itemNames);
    	}
    	
    	
    	public void getLootNames() {
    		try {
    			Scanner itemScan = new Scanner(new File("C:\\Users\\Ian\\workspace\\LootWars\\src\\ItemNameList.txt"));
    			while(itemScan.hasNext()) {
    				itemNames.add(itemScan.next());
    			}
    		} catch(Exception e) {}
    		
    			
    		}
    	
    	public void setLoot(){
    		Random random1 = new Random();
    		Random random2 = new Random();
    		Random random3 = new Random();
    		Random random4 = new Random();
    		Random random5 = new Random();
    		Random random6 = new Random();
    		
    		int r1 = random1.nextInt(itemNames.size());
    		int r2 = random2.nextInt(itemNames.size());
    		int r3 = random3.nextInt(itemNames.size());
    		int r4 = random4.nextInt(itemNames.size());
    		int r5 = random5.nextInt(itemNames.size());
    		int r6 = random6.nextInt(itemNames.size());
    		
    		
    		String temp1 = new String(itemNames.get(r1)); 
    		String temp2 = new String(itemNames.get(r2));
    		String temp3 = new String(itemNames.get(r3));
    		String temp4 = new String(itemNames.get(r4));
    		String temp5 = new String(itemNames.get(r5));
    		String temp6 = new String(itemNames.get(r6));
    		
    		Frame f = new Frame();
    		f.Random1.setText(temp1);
    		f.Random2.setText(temp2);
    		f.Random3.setText(temp3);
    		f.Random4.setText(temp4);
    		f.Random5.setText(temp5);
    		f.Random6.setText(temp6);
    		
    		System.out.println(temp1);
    		System.out.println(temp2);
    		System.out.println(temp3);
    		System.out.println(temp4);
    		System.out.println(temp5);
    		System.out.println(temp6);
    		System.out.println(f.Random1);
    	}
    }
    Java Code:
    import java.awt.BorderLayout;
    
    
    @SuppressWarnings({ "serial", "unused" })
    public class Frame extends JFrame {
    	Icon backg = new ImageIcon(getClass().getResource("LootWarsBackground.jpg"));
    	private JPanel contentPane;
    	JToggleButton Random1, Random2, Random3, Random4, Random5, Random6;
    	/**
    	 * Create the frame.
    	 */
    	public Frame() {
    		setResizable(false);
    		
    		setTitle("Loot Wars");
    		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Ian\\Desktop\\lootwarsicon.jpg"));
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 440, 690);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		JButton MainMenuButton = new JButton("Main");
    		MainMenuButton.setBounds(0, 614, 89, 50);
    		contentPane.add(MainMenuButton);
    		
    		JButton HomeButton = new JButton("Home");
    		HomeButton.setBounds(88, 614, 89, 50);
    		contentPane.add(HomeButton);
    		
    		JButton LootButton = new JButton("Loot!");
    		LootButton.setBounds(175, 614, 89, 50);
    		contentPane.add(LootButton);
    		
    		JButton TownButton = new JButton("Town");
    		TownButton.setBounds(261, 614, 89, 50);
    		contentPane.add(TownButton);
    		
    		JButton ArenaButton = new JButton("Arena");
    		ArenaButton.setBounds(345, 614, 89, 50);
    		contentPane.add(ArenaButton);
    		
    		JMenuBar menuBar = new JMenuBar();
    		menuBar.setBounds(0, 0, 434, 21);
    		contentPane.add(menuBar);
    		
    		JMenu File = new JMenu("File");
    		menuBar.add(File);
    		
    		JMenuItem Open = new JMenuItem("Open");
    		File.add(Open);
    		
    		JMenu mnOptions = new JMenu("Options");
    		menuBar.add(mnOptions);
    		
    		JMenuItem mntmIdkYet = new JMenuItem("IDK yet");
    		mnOptions.add(mntmIdkYet);
    		
    		JButton ScavengeButton = new JButton("Scavenge Loot");
    		ScavengeButton.setForeground(Color.BLACK);
    		ScavengeButton.setBounds(40, 32, 139, 34);
    		contentPane.add(ScavengeButton);
    		
    		JButton SwapButton = new JButton("Swap Loot");
    		SwapButton.setBounds(230, 32, 139, 34);
    		contentPane.add(SwapButton);
    		
    		Random1 = new JToggleButton("Item");
    		Random1.setToolTipText("");
    		Random1.setHorizontalAlignment(SwingConstants.LEFT);
    		Random1.setBounds(10, 103, 414, 34);
    		contentPane.add(Random1);
    		
    		Random2 = new JToggleButton("Item");
    		Random2.setHorizontalAlignment(SwingConstants.LEFT);
    		Random2.setBounds(10, 136, 414, 34);
    		contentPane.add(Random2);
    		
    		Random3 = new JToggleButton("Item");
    		Random3.setHorizontalAlignment(SwingConstants.LEFT);
    		Random3.setBounds(10, 169, 414, 34);
    		contentPane.add(Random3);
    		
    		Random4 = new JToggleButton("Item");
    		Random4.setHorizontalAlignment(SwingConstants.LEFT);
    		Random4.setBounds(10, 202, 414, 34);
    		contentPane.add(Random4);
    		
    		Random5 = new JToggleButton("Item");
    		Random5.setHorizontalAlignment(SwingConstants.LEFT);
    		Random5.setBounds(10, 235, 414, 34);
    		contentPane.add(Random5);
    		
    		Random6 = new JToggleButton("Item");
    		Random6.setHorizontalAlignment(SwingConstants.LEFT);
    		Random6.setBounds(10, 268, 414, 34);
    		contentPane.add(Random6);
    		
    		JToggleButton Duffel1 = new JToggleButton("Item");
    		Duffel1.setHorizontalAlignment(SwingConstants.LEFT);
    		Duffel1.setBounds(10, 354, 414, 34);
    		contentPane.add(Duffel1);
    		
    		JToggleButton Duffel2 = new JToggleButton("Item");
    		Duffel2.setHorizontalAlignment(SwingConstants.LEFT);
    		Duffel2.setBounds(10, 387, 414, 34);
    		contentPane.add(Duffel2);
    		
    		JToggleButton Duffel3 = new JToggleButton("Item");
    		Duffel3.setHorizontalAlignment(SwingConstants.LEFT);
    		Duffel3.setBounds(10, 420, 414, 34);
    		contentPane.add(Duffel3);
    		
    		JToggleButton Duffel4 = new JToggleButton("Item");
    		Duffel4.setHorizontalAlignment(SwingConstants.LEFT);
    		Duffel4.setBounds(10, 453, 414, 34);
    		contentPane.add(Duffel4);
    		
    		JToggleButton Duffel5 = new JToggleButton("Item");
    		Duffel5.setHorizontalAlignment(SwingConstants.LEFT);
    		Duffel5.setBounds(10, 486, 414, 34);
    		contentPane.add(Duffel5);
    		
    		JToggleButton Duffel6 = new JToggleButton("Item");
    		Duffel6.setHorizontalAlignment(SwingConstants.LEFT);
    		Duffel6.setBounds(10, 519, 414, 34);
    		contentPane.add(Duffel6);
    		
    		JLabel FoundLabel = new JLabel("Found Loot");
    		FoundLabel.setFont(new Font("Times New Roman", Font.PLAIN, 22));
    		FoundLabel.setForeground(Color.BLACK);
    		FoundLabel.setBounds(50, 77, 184, 21);
    		contentPane.add(FoundLabel);
    		
    		JLabel YourLabel = new JLabel("Your Loot");
    		YourLabel.setForeground(Color.BLACK);
    		YourLabel.setFont(new Font("Times New Roman", Font.PLAIN, 22));
    		YourLabel.setBounds(50, 320, 127, 34);
    		contentPane.add(YourLabel);
    		
    		JLabel backG = new JLabel(backg);
    		backG.setBounds(0, 21, 444, 653);
    		contentPane.add(backG);
    		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{contentPane, MainMenuButton, HomeButton, LootButton, TownButton, ArenaButton, menuBar, File, Open, mnOptions, mntmIdkYet}));
    		
    		
    	}
    }
    Thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    That's a heck of a lot of code to ask volunteers to go through. Care to simplify things a lot? I suggest creating a much smaller GUI that compiles, runs, and shows the problem.

  3. #3
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Sorry :/ didnt realize how much code was here till you said something. Ill shorten it for you guys :)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    for some reason the text on the JToggleButtons is not being set correctly
    Please describe what is happening? What is incorrect about the buttons?

  5. #5
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    The buttons are fine but when i try to set the text its not changing it in the GUI while the program is running.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Lots of problems in that code, for one you're mixing AWT and Swing components (such as the Panel StatPanel), which is causing bad behavior. Don't do that, but stick with all Swing components. For another, you should be using layout managers and avoiding absolute positioning.

    Next you'll need to point out where in the code the button's text is supposed to be changed.

    Edit 1:
    Also:
    • Why a bunch of Random variables? Why not use just one?
    • The code does not adhere to Java naming standards making it very hard for others to read and understand. It should be edited so that all class names start with capital letters and all variable and method names start with lower case letters.
    • Don't expose your class fields to other classes. Most all of them should be private and only changed via public methods, and try to keep this to a minimum.
    Last edited by Fubarable; 08-10-2011 at 05:07 AM.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    Here is a big NO NO:

    public class Frame extends JFrame {

    Don't use the name of a Java SE class for your own. This makes for very confusing code reading

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I see your problem. You're setting button names alright, but not on the GUI that is being displayed. When you create a new Frame object in your setLoot method, you're creating a new and totally distinct object. So setting JButton names on this object will have absolutely no effect on the separate displayed object. The solution is to pass a reference to the displayed JFrame into Loot (probably best to do this in Loot's constructor), and set the button names on this object.

  9. #9
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks guys i got that fixed now :) But now i have a new problem. I am trying to take items out of an Arraylist called inventory and its not letting me. Im getting an IndexOutOfBoundsException. I made a seperate program to show you the problem because the main program has alot of code. So here it is. Thanks for all your help! :)
    Java Code:
    import java.util.ArrayList;
    
    public class Inventory {
    	public static String temp1, temp2, temp3, temp4, temp5, temp6;
    	public static ArrayList<String> Inventory = new ArrayList<String>();
    	
    	
    	
    	///////////////////////////////////
    	//Empty Constructor              //
    	///////////////////////////////////
    	public Inventory() {
    		
    	}
    	
    	//gets a certain item
    	public String getItem(int item) {
    		return Inventory.get(item);
    	}
    	
    	//Checks if inventory is empty
    	public boolean isEmpty() {
    		if(Inventory.isEmpty()) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    	
    	//Checks if Inventory is full
    	public boolean isFull() {
    		if(Inventory.size() == 6) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    	
    	//Adds one item
    	public void addItem(String item) {
    		Inventory.add(item);
    	}
    	
    	//Removes item
    	public void removeItem(String item) {
    		Inventory.remove(item);
    	}
    	
    	//Swaps items in the array
    	public void swapItem(String item1, String item2) {
    		removeItem(item1);
    		addItem(item2);
    	}
    	
    	//Checks if you can just take the item
    	public boolean canTake() {
    		if(isFull() == false) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    	
    	//Checks if you have to swap
    	public boolean mustSwap() {
    		if(isFull() == true) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    }
    Java Code:
    public class Main {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Inventory i = new Inventory();
    		i.addItem("Rawr");
    		i.getItem(1);
    		System.out.println(i.Inventory);
    	}
    
    }

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    Im getting an IndexOutOfBoundsException.
    Please post the full text of the message. It contains the bad index value and the line number.
    Look at the line where the error occurred and see why the index is OOB.

    If you can't figure out what is happening, add a println just before the statement with the error and print out the values of ALL the variables in the line with the error.

  11. #11
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Inventory.getItem(Inventory.java:18)
    at Main.main(Main.java:10)
    Heres the error and i have been printing the whole arraylist. And when it prints its showing that there is items in the array.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    IndexOutOfBoundsException: Index: 1, Size: 1
    Remember arrays are zero based. If it has one element, then 0 is the highest index.

    at Inventory.getItem(Inventory.java:18)
    Look at this line where you call get(). Make sure the index's value is less than the size of the arraylist

  13. #13
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Wow. I feel dumb. Thank you Norm you are always very helpful. :) I think i have it fixed now. Although im sure i will have more problems in the future...

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    Come on back when you do.

  15. #15
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Well i was right haha. I am trying to make a swapping system that swaps items in an arrayList as well as on the gui.I have the methods to change it in the arry but i need it to change in the gui first. The only problem is that im not sure how to go about doing that other than a large string of if elses. Im using 2 arrays of JToggle buttons to allow the user to switch out the items. So my question is. Is there a better way to do this? (Other than a large string of if elses that is.)

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    a swapping system that swaps items in an arrayList as well as on the gui
    how to go about doing that
    Can you explain what you mean by "swapping"?
    Can you explain some more about the relationship between the swapping and the if/else statements?

  17. #17
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Well i basically there is twelve buttons. The text put on the first six is randomly selected from a text file. The other six are empty of text. What i want it to do is to get the six that are on the randomly picked buttons to be put on the empty buttons based on user input. And if the button that they select already has something on it they get rid of that item and put the new item on. The if elses that i mentioned were what i was doing before. I made a very large if else statement that checked if the random button1 was selected and the inventory button1 were selected. I did this to check every possible instance of user input. But this was very long and hard to read as well as very difficult to change anything on it.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    Can you post the code that does this?
    I really can't tell what your code is doing without seeing it.

    user input. And if the button that they select
    User selection of a button would be detected by a listener which could see if the selected button was empty or not.
    Also the button could be disabled if you did not want to allow the user to select it.

  19. #19
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    There is quite a bit of code so sorry if its too much. And actually your comment sparked an idea of sorts. ill show the code and then try it out.
    Java Code:
    import java.awt.BorderLayout;
    
    
    @SuppressWarnings({ "serial", "unused" })
    public class Frame extends JFrame {
    	private JPanel contentPane;
    	Inventory i = new Inventory();
    	PlayerStats stats = new PlayerStats();
    	Loot l = new Loot();
    	private static ArrayList<JToggleButton> randomButtons = new ArrayList<JToggleButton>();
    	private static ArrayList<JToggleButton> inventoryItems = new ArrayList<JToggleButton>();
    	/**
    	 * Create the frame.
    	 */
    	public Frame() {
    		Loot l = new Loot();
    		l.getLootNames();
    		l.setLoot();
    		setTitle("Loot Wars");
    		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Ian\\Desktop\\lootwarsicon.jpg"));
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 706);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		JButton MainMenuButton = new JButton("Main");
    		MainMenuButton.setBounds(0, 620, 89, 50);
    		contentPane.add(MainMenuButton);
    		
    		JButton scavengeButton = new JButton("Scavenge Loot");
    		scavengeButton.setBounds(22, 32, 167, 35);
    		contentPane.add(scavengeButton);
    		
    		JButton swapButton = new JButton("Swap Loot");
    		swapButton.setBounds(229, 32, 167, 35);
    		contentPane.add(swapButton);
    		
    		JButton HomeButton = new JButton("Home");
    		HomeButton.setBounds(88, 620, 89, 50);
    		contentPane.add(HomeButton);
    		
    		JButton LootButton = new JButton("Loot!");
    		LootButton.setBounds(176, 620, 89, 50);
    		contentPane.add(LootButton);
    		
    		JButton TownButton = new JButton("Town");
    		TownButton.setBounds(262, 620, 89, 50);
    		contentPane.add(TownButton);
    		
    		JButton ArenaButton = new JButton("Arena");
    		ArenaButton.setBounds(345, 620, 89, 50);
    		contentPane.add(ArenaButton);
    		
    		JLabel foundLabel = new JLabel("Found Loot");
    		foundLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		foundLabel.setBounds(10, 90, 122, 27);
    		contentPane.add(foundLabel);
    		
    		JMenuBar menuBar = new JMenuBar();
    		menuBar.setBounds(0, 0, 434, 21);
    		contentPane.add(menuBar);
    		
    		JMenu File = new JMenu("File");
    		menuBar.add(File);
    		
    		JMenuItem Open = new JMenuItem("Open");
    		File.add(Open);
    		
    		JMenuItem saveButton = new JMenuItem("Save");
    		SaveListener s = new SaveListener();
    		saveButton.addActionListener(s);
    		File.add(saveButton);
    		
    		JMenu mnOptions = new JMenu("Options");
    		menuBar.add(mnOptions);
    		
    		JMenuItem SaveButton = new JMenuItem("Save");
    		mnOptions.add(SaveButton);
    		
    		JLabel yourLabel = new JLabel("Your Loot");
    		yourLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		yourLabel.setBounds(10, 367, 122, 21);
    		contentPane.add(yourLabel);
    		
    		JToggleButton random1 = new JToggleButton(l.getItem(0));
    		random1.setHorizontalAlignment(SwingConstants.LEFT);
    		random1.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		random1.setBounds(11, 128, 413, 27);
    		randomButtons.add(random1);
    		contentPane.add(random1);
    		
    		JToggleButton random2 = new JToggleButton(l.getItem(1));
    		random2.setHorizontalAlignment(SwingConstants.LEFT);
    		random2.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		random2.setBounds(10, 166, 413, 27);
    		randomButtons.add(random2);
    		contentPane.add(random2);
    		
    		JToggleButton random3 = new JToggleButton(l.getItem(2));
    		random3.setHorizontalAlignment(SwingConstants.LEFT);
    		random3.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		random3.setBounds(11, 204, 413, 27);
    		randomButtons.add(random3);
    		contentPane.add(random3);
    		
    		JToggleButton random4 = new JToggleButton(l.getItem(3));
    		random4.setHorizontalAlignment(SwingConstants.LEFT);
    		random4.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		random4.setBounds(11, 242, 413, 27);
    		randomButtons.add(random4);
    		contentPane.add(random4);
    		
    		JToggleButton random5 = new JToggleButton(l.getItem(4));
    		random5.setHorizontalAlignment(SwingConstants.LEFT);
    		random5.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		random5.setBounds(11, 280, 413, 27);
    		randomButtons.add(random5);
    		contentPane.add(random5);
    		
    		JToggleButton random6 = new JToggleButton(l.getItem(5));
    		random6.setHorizontalAlignment(SwingConstants.LEFT);
    		random6.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		random6.setBounds(11, 318, 413, 27);
    		randomButtons.add(random6);
    		contentPane.add(random6);
    		
    		JToggleButton inventory1 = new JToggleButton("Item");
    		inventory1.setHorizontalAlignment(SwingConstants.LEFT);
    		inventory1.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		inventory1.setBounds(11, 399, 413, 27);
    		inventoryItems.add(inventory1);
    		contentPane.add(inventory1);
    		
    		JToggleButton inventory2 = new JToggleButton("Item");
    		inventory2.setHorizontalAlignment(SwingConstants.LEFT);
    		inventory2.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		inventory2.setBounds(11, 437, 413, 27);
    		inventoryItems.add(inventory2);
    		contentPane.add(inventory2);
    		
    		JToggleButton inventory3 = new JToggleButton("Item");
    		inventory3.setHorizontalAlignment(SwingConstants.LEFT);
    		inventory3.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		inventory3.setBounds(10, 475, 413, 27);
    		inventoryItems.add(inventory3);
    		contentPane.add(inventory3);
    		
    		JToggleButton inventory4 = new JToggleButton("Item");
    		inventory4.setHorizontalAlignment(SwingConstants.LEFT);
    		inventory4.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		inventory4.setBounds(11, 513, 413, 27);
    		inventoryItems.add(inventory4);
    		contentPane.add(inventory4);
    		
    		JToggleButton inventory5 = new JToggleButton("Item");
    		inventory5.setHorizontalAlignment(SwingConstants.LEFT);
    		inventory5.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		inventory5.setBounds(11, 551, 413, 27);
    		inventoryItems.add(inventory5);
    		contentPane.add(inventory5);
    		
    		JToggleButton inventory6 = new JToggleButton("Item");
    		inventory6.setHorizontalAlignment(SwingConstants.LEFT);
    		inventory6.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		inventory6.setBounds(11, 589, 413, 27);
    		inventoryItems.add(inventory6);
    		contentPane.add(inventory6);
    		
    		JLabel backG = new JLabel();
    		backG.setIcon(new ImageIcon("C:\\Users\\Ian\\Desktop\\LootWarsImages\\LootWarsBackground.jpg"));
    		backG.setBounds(0, 0, 434, 670);
    		contentPane.add(backG);
    	}
    	
    	//Creates the save file when user hits the save menu button
    	public class SaveListener implements ActionListener {
    		public void actionPerformed(ActionEvent ezpz) {
    			Save s = new Save();
    			s.createSaveFile();
    			s.SaveEverything();
    			s.closeFile();
    		}
    	}
    	
    	//Refreshes the loot at the cost of 1 energy
    	public class scavengeListener implements ActionListener {
    		public void actionPerformed(ActionEvent ezpz) {
    			Loot l = new Loot();
    			if(stats.canLoot()==true) {
    				stats.removeEnergy(1);
    			}
    			
    		}
    	}
    	
    	//Changes the view to the menu screen
    	public class HomeListener implements ActionListener {
    		public void actionPerformed(ActionEvent ezpz) {
    			setVisible(false);
    			MainMenu m = new MainMenu();
    			m.setVisible(true);
    			
    		}
    	}
    	
    	//swaps items from the looted items to your inventory. 
    	public class SwapButtonListener implements ActionListener {
    		public void actionPerformed(ActionEvent ezpz) {
    			Inventory i = new Inventory();
    			Loot l = new Loot();
    			
    		}
    	}
    }
    Java Code:
    import java.util.ArrayList;
    /*
     * Here you go James
     * This should make things much simpler for you :D
     * 
     */
    
    public class Inventory {
    	private static ArrayList<String> Inventory = new ArrayList<String>(5);
    	
    	
    	
    	///////////////////////////////////
    	//Empty Constructor              //
    	///////////////////////////////////
    	public Inventory() {
    		
    	}
    	//get the size of the arraylist
    	public int getSize() {
    		return Inventory.size();
    	}
    	
    	//gets a certain item
    	public String getItem(int item) {
    		return Inventory.get(item);
    	}
    	
    	//Checks if inventory is empty
    	public boolean isEmpty() {
    		if(Inventory.isEmpty()) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    	
    	//Checks if Inventory is full
    	public boolean isFull() {
    		if(Inventory.size() == 6) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    	
    	//Adds one item
    	public void addItem(String item) {
    		Inventory.add(item);
    	}
    	
    	//Removes item
    	public void removeItem(String item) {
    		Inventory.remove(item);
    	}
    	
    	//Swaps items in the array
    	public void swapItem(String item1, String item2) {
    		removeItem(item1);
    		addItem(item2);
    	}
    	
    	//Checks if you can just take the item
    	public boolean canTake() {
    		if(isFull() == false) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    	
    	//Checks if you have to swap
    	public boolean mustSwap() {
    		if(isFull() == true) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    }
    Java Code:
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.Scanner;
    
    
    
    public class Loot {
    	private ArrayList<String> itemNames = new ArrayList<String>();
    	private String temp1;
    	private File x = new File("ItemNameList.txt");
    	private ArrayList<String> randomItems = new ArrayList<String>();
    	///////////////////////
    	//Empty Constructor
    	///////////////////////
    	public Loot () {
    		
    	}
    	
    	
    	public void getLootNames() {
    		try {
    			if(x.exists()) {
    				x.list();
    				Scanner itemScan = new Scanner(x);
    				while(itemScan.hasNext()) {
    					itemNames.add(itemScan.next());
    				}
    			}
    			
    		} catch(Exception e) {}
    		
    			
    		}
    	//sets the loot
    	public void setLoot(){
    		Random random1 = new Random();
    		for(int x = 0;x<6;x++) {
    			int r1 = random1.nextInt(itemNames.size());
    			temp1 = new String(itemNames.get(r1));
    			randomItems.add(temp1);
    			System.out.println(temp1);
    		}
    		System.out.println(randomItems);
    		System.out.println("rawr");
    	}
    	
    	public String getItem(int index) {
    		return randomItems.get(index);
    	}
    	
    }

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    Way too much code. Make a smaller version that will compile and execute and show the problem.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 06-29-2011, 06:46 PM
  2. Replies: 7
    Last Post: 03-05-2011, 10:25 AM
  3. Setting text width and height in PDFBox
    By svenskmand in forum Advanced Java
    Replies: 0
    Last Post: 12-21-2010, 01:23 AM
  4. Replies: 0
    Last Post: 12-09-2010, 06:56 PM
  5. Replies: 3
    Last Post: 05-25-2009, 04:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •