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.
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);
}
}
}
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);
}
}
Thanks in advance :)-: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}));
}
}
