Как сделать так, чтобы JFrame и JMenubar отсутствовали в publi c stati c void main (String [] args) - PullRequest
0 голосов
/ 09 мая 2020

Я впервые прошу помощи на этом сайте. Мне нужно переместить JFrame и JMenubar из publi c stati c 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);
}

Это код, который мне нужно переместить из publi c stati c void main (String [] args)

Я попытался переместить это в класс GRandom () (это основной класс) и сделать publi c stati c void main (String [] args) выглядит так:

public static void main(String[] args)
{
    JFrame.setDefaultLookAndFeelDecorated(true);
    new JMenuTest();
}

Работало без ошибок, но ничего не показывалось: (

Помогите, пожалуйста, как переместить его в класс GRandom () или сделать другой

1 Ответ

0 голосов
/ 09 мая 2020

Вот один из способов сделать это.

Это помогает разделить ваш код на методы. Таким образом, вы можете сосредоточиться на одной части GUI за раз.

Методы JFrame должны вызываться в определенном порядке c. Это порядок, который я использую со всеми своими приложениями Swing.

Мне пришлось закомментировать некоторый код, чтобы это работало на моем компьютере.

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;
    }

}
...