-
JMenuBar error
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);
}
}
}
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!
-
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.
-
So I changed JPanel to JFrame...it compiles, but it won't run :confused:
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);
}
}
}
-
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.
-
The menu bar won't show: :confused:
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);
}
}
}
-
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
-