Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help with project

Options
  • 24-02-2014 12:44pm
    #1
    Registered Users Posts: 474 ✭✭


    Hi guys,
    I was hoping ye would help me with a few issues in my partner and I's Java project. We were given a task to create a calculator, so we did... a battle calculator. It needs to be turned in on Friday and we are having issues with a few things. I am pulling my hair out at this stage.

    Anyway the main issue we have:

    We have a JFrame set up with an image with some menu items.
    We added actionListeners to 2 items(just to test stuff) and we cannot get them to work. What we want to do is when you click on an item, a picture and a little bio will display. However nothing happens. I have looked at various tutorials and I am confused.

    This is my code:
    package BRWC;

    import javax.swing.*;

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;

    public class MyFrame extends JFrame implements ActionListener {

    private static final String JMenuItem = null;

    public MyFrame() {
    // setting up the JFrame
    setTitle("BattleRoyaleWithCheese");
    setSize(499, 499);
    setSize(1000, 700);
    setLocation(0, 0);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setLayout(new BorderLayout());
    setContentPane(new JLabel(new ImageIcon(
    "C:\\Users\\bw12\\Desktop\\BattleRoyaleWithCheese\\BRWC.jpg")));
    setLayout(new FlowLayout());
    setSize(999, 699);// I have to have two more setSize methods or else the
    // image
    setSize(1000, 700);// wont load straight away. It acts like a "Refresh"
    // button.

    // creates a menubar for a JFrame
    JMenuBar menuBar = new JMenuBar();

    // add the menubar to the frame
    setJMenuBar(menuBar);

    // define and add the drop down menu to the menubar
    JMenu heroMenu = new JMenu("Hero's");
    JMenu villianMenu = new JMenu("Villian's");
    JMenu antiHMenu = new JMenu("Anti-Hero's");
    JMenu teamsMenu = new JMenu("Teams/Duo's");
    JMenu fight = new JMenu("Fight");
    JMenu calc = new JMenu("Calculator");
    menuBar.add(heroMenu);
    menuBar.add(villianMenu);
    menuBar.add(antiHMenu);
    menuBar.add(teamsMenu);
    menuBar.add(fight);
    menuBar.add(calc);

    // createing a hero drop down menu
    JMenuItem ashWilliams = new JMenuItem("Ash Williams");
    JMenuItem cloud = new JMenuItem("Cloud");
    JMenuItem jaysus = new JMenuItem("Jaysus");
    JMenuItem machete = new JMenuItem("Machete");
    JMenuItem rick = new JMenuItem("Rick");
    JMenuItem snake = new JMenuItem("Snake");

    // adding the heros to the drop down menu
    heroMenu.add(ashWilliams);
    heroMenu.add(cloud);
    heroMenu.add(jaysus);
    heroMenu.add(machete);
    heroMenu.add(rick);
    heroMenu.add(snake);

    // creating a villian drop down menu
    JMenuItem capnSpalding = new JMenuItem("Captain Spalding");
    JMenuItem otis = new JMenuItem("Otis Driftwood");
    JMenuItem pinhead = new JMenuItem("Pinhead");
    JMenuItem satan = new JMenuItem("Satan");
    JMenuItem sephiroth = new JMenuItem("Sephiroth");
    JMenuItem guv = new JMenuItem("The Guvernor");

    // adding the villians to the drop down menu
    villianMenu.add(capnSpalding);
    villianMenu.add(otis);
    villianMenu.add(pinhead);
    villianMenu.add(satan);
    villianMenu.add(sephiroth);
    villianMenu.add(guv);

    // creating a antihero drop down menu
    JMenuItem deadpool = new JMenuItem("Deadpool");
    JMenuItem punisher = new JMenuItem("The Punisher");
    JMenuItem spawn = new JMenuItem("Spawn");

    // adding the antiheros to the drop down menu
    antiHMenu.add(deadpool);
    antiHMenu.add(punisher);
    antiHMenu.add(spawn);

    // creating a teams/duos drop down menu
    JMenuItem vinJule = new JMenuItem("Vincent Vega & Jules Winnfield");
    JMenuItem dReject = new JMenuItem("The Devils Rejects");

    // adding the teams/duos to the drop down menu
    teamsMenu.add(vinJule);
    teamsMenu.add(dReject);

    ashWilliams.addActionListener(this);
    cloud.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) {

    if (ashWilliams) {
    ImageIcon AshW = new ImageIcon(
    "C:\\Users\\bw12\\Desktop\\BattleRoyaleWithCheese\\AshWilliams1.jpg");
    JOptionPane
    .showMessageDialog(
    null,
    "Origin: Evil Dead"
    + "\nWepons: Chainsaw arm, Shotgun, Axe"
    + "\nQuote: Your in charge of jack and ****, and Jack has already left town",
    "Ash Williams", JOptionPane.OK_OPTION, AshW);
    }

    if (JMenuItem == cloud) {
    ImageIcon Cloud = new ImageIcon(
    "C:\\Users\\bw12\\Desktop\\BattleRoyaleWithCheese\\Cloud.jpg");
    JOptionPane.showMessageDialog(null, "Origin: Final Fantasy 7"
    + "\nWepons: Bustersowrd" + "\nQuote: ...................",
    "Cloud", JOptionPane.OK_OPTION, Cloud);
    }
    // This piece of code is causing the problem of Ash bio then cloud, for
    // some reason actionPerformed won't let me add sources to your
    // menuitems
    // why idk.

    }

    public static void main(String[] args) {

    new MyFrame().setVisible(true);

    }

    }

    If you could offer me any pointers I would be happy x 9000!
    Thanks.


Comments

  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Doesn't look like this code would compile.

    Dissecting the code:
     if (ashWilliams) {
    

    ashWilliams is only defined as a local variable in MyFrame, and is not available to actionPerformed. Even if it were available, the statement doesn't make sense: if does a check on true/false, where as ashWilliams is a JMenuItem.

    I assume you want to check if the selected item is Ash Williams.

    Looking at the way your code is written, you probably mean to make you JMenuItem instance members, and then use ActionEvent's getSource to check if the item matches.

    Something like:
    public class MyFrame extends JFrame implements ActionListener {
        // If both the constructor and actionPerformed needs access,
        // we better define our JMenuItem's as instance variables.
        private JMenuItem ashWilliams;
    
    
        // [..] constructor code
    
    
        public void actionPerformed(ActionEvent e) {
            
            if (e.getSource() == ashWilliams) {
                // [..] do whatever is needed for ashWilliams.
            }
        }
    }
    

    Of course, there is other ways to skin the cat.


  • Registered Users Posts: 291 ✭✭Seridisand


    Like procrastinator said, your variables ashWiliams and cloud are outside of the scope of your actionPerformed

    If you add this to the top of your class

    private static final String JMenuItem = null;
    protected JMenuItem ashWilliams = new JMenuItem("Ash Williams");
    protected JMenuItem cloud = new JMenuItem("Cloud");


    and inside the actionPerformed:
    if (e.getSource() == ashWilliams)
    if (e.getSource() == cloud)

    It runs okay


  • Registered Users Posts: 474 ✭✭Umekichi


    Excellent guys, thanks for the help.
    We got it working :D


Advertisement