this is my first time asking for help on this site. I need to move JFrame and JMenubar from public static void main(String[] args).
public static void main(String[] args){
ResourceBundle res = ResourceBundle.getBundle("georglider.grandom.lang.lang");
JFrame F = new JFrame(res.getString("GRandom"));
F.setContentPane(new GRandom().JP1);
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
F.pack();
F.setVisible(true);
F.setSize(300,163);
F.setResizable(false);
JMenuBar gmenu = new JMenuBar();
JMenu Mode = new JMenu("Режим");
JMenu Display = new JMenu("После генерации");
JMenu GenerateOptions = new JMenu("Опции для генерации");
gmenu.add(Mode);
gmenu.add(Display);
gmenu.add(GenerateOptions);
Icon dicon = new Icon() {
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
}
@Override
public int getIconWidth() {
return 0;
}
@Override
public int getIconHeight() {
return 0;
}
};
//M = Menu | D = Display | GO = GenerateOptions
JRadioButtonMenuItem Mnumbers = new JRadioButtonMenuItem("Генерировать числа",dicon,true);
Mnumbers.setActionCommand("Mnumbers");
JRadioButtonMenuItem Mstring = new JRadioButtonMenuItem("Генерировать заданные строки");
Mstring.setActionCommand("Mstring");
JRadioButtonMenuItem Ddefault = new JRadioButtonMenuItem("По умолчанию",dicon,true);
Ddefault.setActionCommand("Ddefault");
JRadioButtonMenuItem Dopen = new JRadioButtonMenuItem("Открыть файл");
Dopen.setActionCommand("Dopen");
JRadioButtonMenuItem Dshowhere = new JRadioButtonMenuItem("Показать здесь");
Dshowhere.setActionCommand("Dshowhere");
JRadioButtonMenuItem GOninclude = new JRadioButtonMenuItem("Не включать числа");
Dshowhere.setActionCommand("GOninclude");
Mode.add(Mnumbers);
Mode.add(Mstring);
Display.add(Ddefault);
Display.add(Dopen);
Display.add(Dshowhere);
GenerateOptions.add(GOninclude);
F.setJMenuBar(gmenu);
}
This is the code, that I need to move from public static void main(String[] args)
I tried to move this to GRandom() class (It's main class) and make public static void main(String[] args) look like this:
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
new JMenuTest();
}
It worked without any errors, but nothing was showing:(
Help me please, how to move it to GRandom() class, or make another
Here's one way to do it.
It helps to separate your code into methods. That way, you can focus on one part of the GUI at a time.
The JFrame methods have to be called in a specific order. This is the order I use with all my Swing applications.
I had to comment out some code for this to run on my computer.
import java.awt.Component;
import java.awt.Graphics;
import java.util.ResourceBundle;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
public class JFrameExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JFrameExample());
}
@Override
public void run() {
// ResourceBundle res = ResourceBundle.getBundle(
// "georglider.grandom.lang.lang");
// JFrame frame = new JFrame(res.getString("GRandom"));
JFrame frame = new JFrame("JFrame Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(createMenu());
frame.pack();
frame.setSize(400, 200);
frame.setResizable(false);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JMenuBar createMenu() {
JMenuBar gmenu = new JMenuBar();
JMenu Mode = new JMenu("Режим");
JMenu Display = new JMenu("После генерации");
JMenu GenerateOptions = new JMenu("Опции для генерации");
// M = Menu | D = Display | GO = GenerateOptions
JRadioButtonMenuItem Mnumbers =
new JRadioButtonMenuItem("Генерировать числа",
createIcon(), true);
Mnumbers.setActionCommand("Mnumbers");
JRadioButtonMenuItem Mstring =
new JRadioButtonMenuItem(
"Генерировать заданные строки");
Mstring.setActionCommand("Mstring");
JRadioButtonMenuItem Ddefault =
new JRadioButtonMenuItem("По умолчанию",
createIcon(), true);
Ddefault.setActionCommand("Ddefault");
JRadioButtonMenuItem Dopen =
new JRadioButtonMenuItem("Открыть файл");
Dopen.setActionCommand("Dopen");
JRadioButtonMenuItem Dshowhere =
new JRadioButtonMenuItem("Показать здесь");
Dshowhere.setActionCommand("Dshowhere");
JRadioButtonMenuItem GOninclude =
new JRadioButtonMenuItem("Не включать числа");
Dshowhere.setActionCommand("GOninclude");
Mode.add(Mnumbers);
Mode.add(Mstring);
Display.add(Ddefault);
Display.add(Dopen);
Display.add(Dshowhere);
GenerateOptions.add(GOninclude);
gmenu.add(Mode);
gmenu.add(Display);
gmenu.add(GenerateOptions);
return gmenu;
}
private Icon createIcon() {
Icon dicon = new Icon() {
@Override
public void paintIcon(Component c,
Graphics g, int x, int y) {
}
@Override
public int getIconWidth() {
return 0;
}
@Override
public int getIconHeight() {
return 0;
}
};
return dicon;
}
}