Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > Помогите разобраться с Графическим интерфейсом


Автор: natika 28.3.2014, 16:50
Помогите пожалйста найти ошибки: Почему функция которая создает окно управления инвентарем не работает? Она как будто два раза вызывает конструктор. И кнопки тоже почему то не работают :( Подскажите пожалуйста что я делаю не так, может быть я что то забыла добавить, просто это моя первая программка с графическим интерфейсом 


Код
public class GestionInventaire extends JFrame {
    
    
    private static final int FRAME_WIDTH = 600;
    private static final int FRAME_HEIGTH = 300;
    private static JFrame menuprinc = new JFrame("Gestion inventaire");
    private static JPanel panelcombox;
    private static JPanel panelprincipal;
    private static JPanel panelSecond;
    private static JPanel panelAjout;
    private static JPanel panelSupp;
    private static JPanel panelMod;

    private static JRadioButton buttonAjout;
    private static JRadioButton buttonSupp;
    private static JRadioButton buttonMod;
    private static JButton buttonReturnMenu;
    private static ActionListener listener;
    
    
    //Создаем окно управления инвентарем
    public GestionInventaire(){
    
    createMenuBox();
    createPanelPrincipal();
    createPanelSecondaire();
    System.out.println("pipi caca");
    menuprinc.getContentPane().add(panelprincipal, BorderLayout.CENTER);
    panelprincipal.setLayout(new GridLayout(2, 0));
    setSize(FRAME_WIDTH, FRAME_HEIGTH);
    }
    
/**
    * Создаем меню
    */
    public void createMenuBox(){
    
    buttonAjout = new JRadioButton();
    buttonSupp = new JRadioButton();
    buttonMod = new JRadioButton();
    buttonReturnMenu = new JButton();
    
    class ChoiceListener implements ActionListener{ 
    
    public void actionPerformed (ActionEvent e) {
    
    setChoice();
    
    }
    
    }
    listener = new ChoiceListener();
    
    buttonAjout = new JRadioButton("1- Ajouter un article");
    buttonAjout.addActionListener(listener);
    
    
    buttonSupp = new JRadioButton("2- Supprimer un article");
    buttonSupp.addActionListener(listener);
    
    buttonMod = new JRadioButton("3 - Modifier un article");
    buttonMod.addActionListener(listener);
    
    buttonReturnMenu = new JButton("Retour menu principal");
    buttonReturnMenu.addActionListener(listener);
    
    ButtonGroup group = new ButtonGroup();
    group.add(buttonAjout);
    group.add(buttonSupp);
    group.add(buttonMod);
    group.add(buttonReturnMenu);
    
    
    }
    
    /**
    *Создаем главное меню 

    */
    
    public JPanel createPanelPrincipal() {
    
    panelprincipal = new JPanel(new GridLayout(2,1));
    panelcombox = new JPanel(new GridLayout(4,1));
    panelcombox.add(buttonAjout);
    panelcombox.add(buttonSupp);
    panelcombox.add(buttonMod);
    panelcombox.add(buttonReturnMenu);
    panelcombox.setBorder(new TitledBorder(new EtchedBorder(), "Options"));
    panelprincipal.add(panelcombox);
    return panelcombox;
    
    }
    
    /**
    * Создаем дополнительную панель в зависимости от выбора пользователя
    
    */
    public void createPanelSecondaire(){
    
    panelSecond = new JPanel();
    
    panelAjout = createPanelAjout();
    panelSupp = createPanelSupp();
    panelMod = createPanelMod();
    panelSecond.setBorder(new TitledBorder(new EtchedBorder(), "Titre"));
    panelSecond.setLayout(new BorderLayout());
    panelprincipal.add(panelSecond);
    //return panelSecond;

    
    }
    
    //Создаем панель добавления предметов
    public JPanel createPanelAjout(){
    JPanel panel = new JPanel();
    return panel;
    }
    
    //Создаем панель удаления предметов
    public JPanel createPanelSupp(){
    JPanel panel = new JPanel();
    return panel;
    
    }
    
    //Создаем панель изменения предметов
    public JPanel createPanelMod(){
    JPanel panel = new JPanel();
    return panel;
    
    }
    
    /**
    * Метод который позволяет открыть выбранное окно.
    */
    public void setChoice(){

    String titre = "";
    
    if(buttonAjout.isSelected()){
    titre = "Ajouter un article";
    panelSecond.add(panelAjout, BorderLayout.CENTER);
    panelSecond.setBorder(new TitledBorder(new EtchedBorder(), titre));
    }
    }
    
       public static Window getInventaireFrame(){
    
    System.out.println("....");
    menuprinc.setBounds(325, 250, FRAME_WIDTH, FRAME_HEIGTH);
    menuprinc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    menuprinc.setVisible(true);
    return menuprinc;
    }
    


}

Автор: MarkHunt 30.3.2014, 19:46
Мудрённый класс какой-то.
поля можно было и не объявлять как static
Код

    private static JFrame menuprinc = new JFrame("Gestion inventaire");
    private static JPanel panelcombox;
    private static JPanel panelprincipal;
    private static JPanel panelSecond;
    private static JPanel panelAjout;
    private static JPanel panelSupp;
    private static JPanel panelMod;
    private static JRadioButton buttonAjout;
    private static JRadioButton buttonSupp;
    private static JRadioButton buttonMod;
    private static JButton buttonReturnMenu;
    private static ActionListener listener;


Ну, в общем, метод getInventaireFrame показывает фрейм, который Вам нужен. Но он объвлен как static, а инициализация фрейма происходит в конструкторе. Поэтому по логике этого класса, нужно вызвать сначала конструктор, а потом метод getInventaireFrame.

Код

    public static void main(String[] args) {
        GestionInventaire gestionInventaire = new GestionInventaire();
        GestionInventaire.getInventaireFrame();
    }


И кстати, т.к. класс GestionInventaire наследует JFrame нужно будет его тоже закрывать, а то программа так и будет в процессах висеть, т.е. так хотя бы
Код

    public static void main(String[] args) {
        GestionInventaire gestionInventaire = new GestionInventaire();
        GestionInventaire.getInventaireFrame();
        gestionInventaire.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

Автор: natika 4.4.2014, 02:32
Спасибо!!!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)