Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm new to this page I want to ask you to critique my code on jInternalFrame. Basically the problem is that there is no complete example on using this, so I wrote this "template" but I don't know if it lacks of something.

So here is the code(It does 'nothing' just calling, positioning and basic stuff):

Main.java

package forms;

import java.awt.*;
import javax.swing.*;

public class Main {

    static JFrame frame;
    static panel pan;
    static menu men;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });

    }

    public static void createAndShowGUI() {

        UIManager.LookAndFeelInfo look[];
        look = UIManager.getInstalledLookAndFeels();
        try {
            UIManager.setLookAndFeel(look[3].getClassName());
        } catch (Exception ex) {
        }


        frame = new JFrame();
        pan = new panel();
        men = new menu(pan);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        frame.getContentPane().add(pan);
        frame.setJMenuBar(men.getMenuBar());

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
}

mTmp.java

package forms;

import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

public class mTmp extends JInternalFrame {

    //referencia a singleton
    private static mTmp ref;
    //desktop, panel, pos. relativa
    JDesktopPane desktop;
    JPanel panel;
    Dimension idx;
    //componentes swing
    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JTextArea jTextArea1;
    private JTextField jTextField1;

    public mTmp() {
        super("ventana", true, true, true, true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public static mTmp getmTmp(JDesktopPane desktop, JPanel panel, Dimension idx) {
        if (ref == null) {
            ref = new mTmp();
            ref.desktop = desktop;
            ref.panel = panel;
            ref.idx = idx;
            ref.crearmTmp();
        }
        return ref;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    @Override
    public void dispose() {
        ref = null;
        super.dispose();
    }

    private void initComponents(GroupLayout layout) {

        jPanel1 = new JPanel();
        jTextField1 = new JTextField();
        jScrollPane1 = new JScrollPane();
        jTextArea1 = new JTextArea();
        jButton1 = new JButton();
        jButton2 = new JButton();
        jButton3 = new JButton();


        jPanel1.setBorder(BorderFactory.createTitledBorder(""));

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("jButton1");

        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addGap(27, 27, 27).addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addGroup(jPanel1Layout.createSequentialGroup().addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1))).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(jButton1)).addGap(27, 27, 27).addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE).addContainerGap()));

        jButton2.setText("jButton2");

        jButton3.setText("jButton3");

        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(27, 27, 27).addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(jButton2).addComponent(jButton3)).addContainerGap(38, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(25, 25, 25).addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addGap(92, 92, 92).addComponent(jButton2).addGap(36, 36, 36).addComponent(jButton3))).addContainerGap(30, Short.MAX_VALUE)));

        actions();
    }

    private void crearmTmp() {
        GroupLayout layout = new GroupLayout(panel);
        initComponents(layout);
        panel.setLayout(layout);

        if (panel != null) {
            this.setContentPane(panel);
            this.getRootPane().setOpaque(false);
        }

        desktop.add(this);
        this.setOpaque(false);
        this.setVisible(true);
        this.setLocation(idx.width, idx.height);
        this.setSize(500, 400);
        desktop.getDesktopManager().activateFrame(this);
    }

    private void actions() {
        jButton2.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    }
}

menu.java

package forms;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class menu extends JMenuBar implements ActionListener {

    private JMenuBar menuBar = new JMenuBar();
    private LinkedList<JMenu> list = new LinkedList<JMenu>();
    private panel Panel;
    private Dimension dim;

    public menu(panel Panel) {

        this.Panel = Panel;

        list.add(newMenu("Archivo", KeyEvent.VK_A));
        list.get(0).add(newMenuItem("Salir", KeyEvent.VK_N, "salir"));
        list.get(0).add(newMenuItem("Formlario 1", KeyEvent.VK_E, "form1"));
        list.get(0).add(newMenuItem("Formlario 2", KeyEvent.VK_J, "form2"));
        list.get(0).add(newMenu("Submenu", KeyEvent.VK_O));
        list.get(0).getItem(3).add(newMenuItem("Salir", KeyEvent.VK_N, "salir"));

        list.add(newMenu("Editar", KeyEvent.VK_D));

        for (int i = 0; i < list.size(); i++) {
            menuBar.add((JMenu) list.get(i));
        }

        dim = new Dimension(120, 60);
    }

    private JMenu newMenu(String name, int key) {
        JMenu menu = new JMenu(name);
        menu.setMnemonic(key);
        return menu;
    }

    private JMenuItem newMenuItem(String name, int key, String command) {
        JMenuItem menuItem = new JMenuItem(name);
        menuItem.setMnemonic(key);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(key, ActionEvent.ALT_MASK));
        menuItem.setActionCommand(command);
        menuItem.addActionListener(this);
        return menuItem;
    }

    public JMenuBar getMenuBar() {
        return menuBar;
    }

    protected void quit() {
        System.exit(0);
    }

    public void actionPerformed(ActionEvent e) {
        if ("salir".equals(e.getActionCommand())) {
            quit();
        } else if ("form1".equals(e.getActionCommand())) {
            //Panel.newAlumno(dim);
            dim.setSize(dim.width + 20, dim.height + 50);
        } else if ("form2".equals(e.getActionCommand())) {
            Panel.newmTmp(dim);
            dim.setSize(dim.width + 20, dim.height + 20);
        }
    }
}

panel.java

package forms;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JDesktopPane;
import javax.swing.JPanel;

public final class panel extends JPanel {

    private final JDesktopPane desktop = new JDesktopPane();
    private Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    public panel() {
        super(new BorderLayout());

        desktop.setBackground(Color.white);

        add(desktop);
        dim.setSize(0.9 * dim.width, 0.8 * dim.height);
        setPreferredSize(dim);

    }


    public void newmTmp(Dimension dim) {
        JPanel p1 = new JPanel();
        p1.setOpaque(true);
        mTmp.getmTmp(desktop, p1, dim);
    }
}

Thanks in advance

share|improve this question
add comment

2 Answers

Your class names don't follow the normal standard of starting with a capital letter. Also they are not very specific so it is hard to understand what their purpose might be. Your variable names suffer from similar problems of not being

share|improve this answer
add comment
  • UIManager.setLookAndFeel(look[3].getClassName()); is dangerous: As far as I know there is nowhere defined how many L&Fs have to be present in which order, so on a different VM this line could simply blow up.
  • Please use uppercase class names
  • Why do you go a detour when creating menus by using a list? You might need a few lines less using a list, but the fun starts if you want to insert menus later, and you rely on list indexes.

    JMenu archivo = newMenu("Archivo", KeyEvent.VK_A);
    archivo.add(newMenuItem("Salir", KeyEvent.VK_N, "salir"));
    archivo.add(newMenuItem("Formlario 1", KeyEvent.VK_E, "form1"));
    archivo.add(newMenuItem("Formlario 2", KeyEvent.VK_J, "form2"));
    JMenu submenu = newMenu("Submenu", KeyEvent.VK_O);
    submenu.add(newMenuItem("Salir", KeyEvent.VK_N, "salir"));
    archivo.add(submenu); 
    
    JMenu editar = newMenu("Editar", KeyEvent.VK_D);
    
    menuBar.add(archivo);
    menuBar.add(editar);
    
share|improve this answer
    
thanks for the review. About the menus, just seem a little more organized to me to have "levels", but yes, It could get messy –  MHero Jul 3 '11 at 19:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.