Results 1 to 7 of 7
Thread: JMenuBar error
- 03-01-2011, 11:56 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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); } } }ERROR: cannot find symbol method setJMenuBar(javax.swing.JMenuBar)Java Code:import java.awt.*; import javax.swing.*; public class converter { public static void main(String[] args) { JFrame win = new Gui2(); win.setVisible(true); } }
Thanks for the help!
- 03-02-2011, 12:02 AM #2
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.
- 03-02-2011, 03:50 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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); } } }
- 03-02-2011, 03:57 AM #4
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.
- 03-02-2011, 10:56 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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); } } }
- 03-02-2011, 11:41 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-02-2011, 11:55 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
JMenuBar and Popups
By snoopygee in forum AWT / SwingReplies: 2Last Post: 12-10-2010, 10:07 AM -
Mutliple JMenuBar
By chyrl in forum AWT / SwingReplies: 10Last Post: 11-04-2010, 05:43 AM -
Changing position of JMenuBar
By LianaN in forum New To JavaReplies: 8Last Post: 09-17-2010, 12:41 PM -
JMenubar option
By navishkumarb in forum New To JavaReplies: 3Last Post: 05-24-2010, 02:03 AM -
Create JMenuBar using DataBase
By arulmozs in forum AWT / SwingReplies: 1Last Post: 11-03-2009, 11:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks