Results 1 to 7 of 7

Thread: JMenuBar error

  1. #1
    Tastosis is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default JMenuBar error

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
    
    public class Gui2 extends JFrame {
    
        private JButton button;
        private JTextArea area;
        private JTextArea area2;
        private JScrollPane scroll;
        private JScrollPane scroll2;
        private JSplitPane splitPane;
        private JPanel forbutton;
        private JPanel content;
        private JMenuBar menubar;
    
        public Gui2() {
            initComponents();
        }
    
        private void initComponents() {
    
        	menubar = new JMenuBar();
    
            button = new JButton("Convert");
    
            forbutton = new JPanel();
            forbutton.add(button);
    
            area = new JTextArea(40, 45);
            area2 = new JTextArea(40, 45);
    
            scroll = new JScrollPane(area);
            add(scroll);
    
            scroll2 = new JScrollPane(area2);
            add(scroll2);
    
            splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, area, area2);
            splitPane.setOneTouchExpandable(false);
            splitPane.setResizeWeight(0.5);
    
            content = new JPanel();
            content.setLayout(new BorderLayout());
            content.add(splitPane, BorderLayout.CENTER);
            content.add(forbutton, BorderLayout.PAGE_END);
    
            content.setJMenuBar(menubar);
    
            setContentPane(content);
            setTitle("Code Converter");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
    
            HandlerClass handler = new HandlerClass();
            button.addActionListener(handler);
        }
    
        private class HandlerClass implements ActionListener {
    
            public void actionPerformed(ActionEvent event) {
            	String one = "printf";
            	String two = "scanf";
            	String three = "int";
            	String four = "float";
            	String five = "double";
            	String six = "boolean";
            	String seven = "char";
            	String eight = "String";
            	String nine = "main";
            	String ten = "return";
    
            	String a = "FGJKHNFG";
            	String b = "GHFKGHM";
            	String c = "DGBDGF";
            	String d = "GHNDHG";
            	String e = "BCVNC";
            	String f = "GDFHDGF";
            	String g = "NDFGNFN";
            	String h = "DFGNDGF";
            	String i = "DFGNGDFN";
            	String j = "GDFNGDFN";
    
            	String code = area.getText();
    
            	String code2 = code.replace(one, a);
            	String code3 = code2.replace(two, b);
            	String code4 = code3.replace(three, c);
            	String code5 = code4.replace(four, d);
            	String code6 = code5.replace(five, e);
            	String code7 = code6.replace(six, f);
            	String code8 = code7.replace(seven, g);
            	String code9 = code8.replace(eight, h);
            	String code10 = code9.replace(nine, i);
            	String code_final = code10.replace(ten, j);
    
            	area2.setText(code_final);
            }
    	}
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class converter {
    
    public static void main(String[] args) {
            JFrame win = new Gui2();
            win.setVisible(true);
        }
    }
    ERROR: cannot find symbol method setJMenuBar(javax.swing.JMenuBar)

    Thanks for the help!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    8

    Default

    Just like the compiler says, that method does not exist. The variable content is a JPanel and JPanel class does not have that method. Guess what? JFrame does though.

  3. #3
    Tastosis is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    So I changed JPanel to JFrame...it compiles, but it won't run :confused:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
    
    public class Gui2 extends JFrame {
    
        private JButton button;
        private JTextArea area;
        private JTextArea area2;
        private JScrollPane scroll;
        private JScrollPane scroll2;
        private JSplitPane splitPane;
        private JPanel forbutton;
        private JFrame content;
        private JMenuBar menubar;
    
        public Gui2() {
            initComponents();
        }
    
        private void initComponents() {
    
        	menubar = new JMenuBar();
    
            button = new JButton("Convert");
    
            forbutton = new JPanel();
            forbutton.add(button);
    
            area = new JTextArea(40, 45);
            area2 = new JTextArea(40, 45);
    
            scroll = new JScrollPane(area);
            add(scroll);
    
            scroll2 = new JScrollPane(area2);
            add(scroll2);
    
            splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, area, area2);
            splitPane.setOneTouchExpandable(false);
            splitPane.setResizeWeight(0.5);
    
            content = new JFrame();
            content.setLayout(new BorderLayout());
            content.add(splitPane, BorderLayout.CENTER);
            content.add(forbutton, BorderLayout.PAGE_END);
    
            content.setJMenuBar(menubar);
    
            setContentPane(content);
            setTitle("Code Converter");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
    
            HandlerClass handler = new HandlerClass();
            button.addActionListener(handler);
        }
    
        private class HandlerClass implements ActionListener {
    
            public void actionPerformed(ActionEvent event) {
            	String one = "printf";
            	String two = "scanf";
            	String three = "int";
            	String four = "float";
            	String five = "double";
            	String six = "boolean";
            	String seven = "char";
            	String eight = "String";
            	String nine = "main";
            	String ten = "return";
    
            	String a = "FGJKHNFG";
            	String b = "GHFKGHM";
            	String c = "DGBDGF";
            	String d = "GHNDHG";
            	String e = "BCVNC";
            	String f = "GDFHDGF";
            	String g = "NDFGNFN";
            	String h = "DFGNDGF";
            	String i = "DFGNGDFN";
            	String j = "GDFNGDFN";
    
            	String code = area.getText();
    
            	String code2 = code.replace(one, a);
            	String code3 = code2.replace(two, b);
            	String code4 = code3.replace(three, c);
            	String code5 = code4.replace(four, d);
            	String code6 = code5.replace(five, e);
            	String code7 = code6.replace(six, f);
            	String code8 = code7.replace(seven, g);
            	String code9 = code8.replace(eight, h);
            	String code10 = code9.replace(nine, i);
            	String code_final = code10.replace(ten, j);
    
            	area2.setText(code_final);
            }
    	}
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    8

    Default

    NO!

    You attempted to add the JMenuBar to a JPanel but it should have been added to a JFrame instead. content should be a JPanel. Do you have a JFrame? Perhaps you should try adding the JMenuBar to that instead.

  5. #5
    Tastosis is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    The menu bar won't show: :confused:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
    
    public class Gui2 extends JFrame {
    
        private JButton button;
        private JTextArea area;
        private JTextArea area2;
        private JScrollPane scroll;
        private JScrollPane scroll2;
        private JSplitPane splitPane;
        private JPanel forbutton;
        private JPanel content;
        private JMenuBar menubar;
        private JFrame frame;
    
        public Gui2() {
            initComponents();
        }
    
        private void initComponents() {
    
        	menubar = new JMenuBar();
        	JMenu menu = new JMenu("Menu Label");
            menubar.add(menu);
    
        	JMenuItem item = new JMenuItem("File");
        	menu.add(item);
    
        	frame = new JFrame();
    
            button = new JButton("Convert");
    
            forbutton = new JPanel();
            forbutton.add(button);
    
            area = new JTextArea(40, 45);
            area2 = new JTextArea(40, 45);
    
            scroll = new JScrollPane(area);
            add(scroll);
    
            scroll2 = new JScrollPane(area2);
            add(scroll2);
    
            splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, area, area2);
            splitPane.setOneTouchExpandable(false);
            splitPane.setResizeWeight(0.5);
    
            content = new JPanel();
            content.setLayout(new BorderLayout());
            content.add(splitPane, BorderLayout.CENTER);
            content.add(forbutton, BorderLayout.PAGE_END);
    
            frame.setJMenuBar(menubar);
    
            setContentPane(content);
            setTitle("Code Converter");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
    
            HandlerClass handler = new HandlerClass();
            button.addActionListener(handler);
        }
    
        private class HandlerClass implements ActionListener {
    
            public void actionPerformed(ActionEvent event) {
            	String one = "printf";
            	String two = "scanf";
            	String three = "int";
            	String four = "float";
            	String five = "double";
            	String six = "boolean";
            	String seven = "char";
            	String eight = "String";
            	String nine = "main";
            	String ten = "return";
    
            	String a = "FGJKHNFG";
            	String b = "GHFKGHM";
            	String c = "DGBDGF";
            	String d = "GHNDHG";
            	String e = "BCVNC";
            	String f = "GDFHDGF";
            	String g = "NDFGNFN";
            	String h = "DFGNDGF";
            	String i = "DFGNGDFN";
            	String j = "GDFNGDFN";
    
            	String code = area.getText();
    
            	String code2 = code.replace(one, a);
            	String code3 = code2.replace(two, b);
            	String code4 = code3.replace(three, c);
            	String code5 = code4.replace(four, d);
            	String code6 = code5.replace(five, e);
            	String code7 = code6.replace(six, f);
            	String code8 = code7.replace(seven, g);
            	String code9 = code8.replace(eight, h);
            	String code10 = code9.replace(nine, i);
            	String code_final = code10.replace(ten, j);
    
            	area2.setText(code_final);
            }
    	}
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,017
    Blog Entries
    7
    Rep Power
    23

    Default

    Your Gui2 is a JFrame (it extends one) but it also has a JFrame named 'frame'. You're not really using it so get rid of it and add your JMenuBar to your Gui2 object and not to that 'frame' object.

    kind regards,

    Jos
    I have the stamina of a seal; I lie on the beach instead of running on it.

  7. #7
    Tastosis is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

Similar Threads

  1. JMenuBar and Popups
    By snoopygee in forum AWT / Swing
    Replies: 2
    Last Post: 12-10-2010, 11:07 AM
  2. Mutliple JMenuBar
    By chyrl in forum AWT / Swing
    Replies: 10
    Last Post: 11-04-2010, 06:43 AM
  3. Changing position of JMenuBar
    By LianaN in forum New To Java
    Replies: 8
    Last Post: 09-17-2010, 12:41 PM
  4. JMenubar option
    By navishkumarb in forum New To Java
    Replies: 3
    Last Post: 05-24-2010, 02:03 AM
  5. Create JMenuBar using DataBase
    By arulmozs in forum AWT / Swing
    Replies: 1
    Last Post: 11-03-2009, 12:52 PM

Posting Permissions

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