views:

49

answers:

3

Im not sure how to reference to JPanel when it was declared like this.

This is the coding of the entire program: Everything works but the layout is not how I want it. adding BorderLayouts to it doesnt seem to work.

class FrameDemo
{
    public static void main(String[] args)
    {
        final JFrame frame = new JFrame("CIT Test Program");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(350, 250));
        frame.add(new JPanel()
        {{
            String[] tests = {"A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package"};
            JComboBox comboBox = new JComboBox(tests);
            TextArea text = new TextArea(5,10);
            add(new JLabel("Welcome to the CIT Test Program "));
            add(new JLabel("Please select which Test Package from the list below."));

            JMenuBar menuBar = new JMenuBar();
            JMenu fileMenu = new JMenu("File");
            JMenu editMenu = new JMenu("Edit");
            JMenu helpMenu = new JMenu("Help");
            menuBar.add(fileMenu);
            menuBar.add(editMenu);
            menuBar.add(helpMenu);
            JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
            JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
            JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
            JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
            JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
            JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
            JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
            JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
            fileMenu.add(newMenu);
            fileMenu.add(openMenu);
            fileMenu.add(saveMenu);
            fileMenu.add(exitMenu);
            editMenu.add(cutMenu);
            editMenu.add(copyMenu);
            editMenu.add(pasteMenu);
            helpMenu.add(infoMenu);
            this.add(comboBox, BorderLayout.NORTH);
            this.add(text, BorderLayout.SOUTH);
            frame.setJMenuBar(menuBar);
            add(new JButton("Select")
            {{
                addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e )
                    {
                        frame.dispose();
                        JOptionPane.showMessageDialog(frame,"IT WORKS!");

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

    }
}

Not sure how to reference to JPanel to use BorderLayout. How would I go about doing this?

+2  A: 

If you add a panel to a JFrame (using add() as you are doing here) , you can assume that it is being added as the contentPanel. However, it is much better to be explicit. Instead of this:

frame.add(new JPanel()
{}

use this:

JPanel panel = new JPanel(new BorderLayout());
// add your stuff to the panel;
frame.add(panel);

EDIT:

after looking at your edit, what is clear is that you are initializing an anonymous class. This is not generally bad practice, but here you are putting a lot of initialization code. The code you are putting in is in a double-braced block, which in essence puts it into a static initializer. It seems like a normal anonymous class, but it really isn't. With that much code, it deserves its own class. If you went so far as to code a new class, you should be fine. I would suggest that you then define it as an extension of JPanel and in your own constructor pass a new BorderLayout() to the super.

EDIT 2:

if you create a brand new file/class named Bar and you coded it like this:

public class Bar extends JPanel {
    public Bar(final JFrame frame) {
        String[] tests = { "A+ Certification", "Network+ Certification",
                "Security+ Certification", "CIT Full Test Package" };
        JComboBox comboBox = new JComboBox(tests);
        TextArea text = new TextArea(5, 10);
        add(new JLabel("Welcome to the CIT Test Program "));
        add(new JLabel("Please select which Test Package from the list below."));

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
        JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
        JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
        JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
        JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
        JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
        JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
        JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
        fileMenu.add(newMenu);
        fileMenu.add(openMenu);
        fileMenu.add(saveMenu);
        fileMenu.add(exitMenu);
        editMenu.add(cutMenu);
        editMenu.add(copyMenu);
        editMenu.add(pasteMenu);
        helpMenu.add(infoMenu);
        this.add(comboBox, BorderLayout.NORTH);
        this.add(text, BorderLayout.SOUTH);
        frame.setJMenuBar(menuBar);
        add(new JButton("Select") {
            {
                addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        frame.dispose();
                        JOptionPane.showMessageDialog(frame, "IT WORKS!");

                    }
                });
            }
        });

    }

All you would need to do to use it would be to call

  JPanel panel = new Bar(frame);

however, the goal here is to use a BorderLayout, so I would suggest that you put this call in to start:

 public Bar(final JFrame frame) {
        super(new BorderLayout());
        .... everything else
 }
akf
I have inserted the whole program code above so you can see the coding. adding it like your way I got about 33 errors.
Nick G.
ha. sorry about that. i will look at your edit and give you some feedback.
akf
Thanks, Don't be sorry. If your telling me something correct and I'm just not getting it then its my fault :P
Nick G.
Sorry, im kinda new to Java, I kind of understand that but not really. Where exactly where I code the new class, to me everything should be in the frame correct? If I create a new class wont it not be in the frame?
Nick G.
were you to code a new class, you could create a new instance just like you would any other new object. it really is pretty easy.
akf
Well, I believe I have coded this right, but I am getting an errorclass frame2 is public, should be declared in a file named frame2.javaI coded it as public class frame2 extends JFrame {}
Nick G.
cannot find symbolsymbol : constructor Bar()location: class Bar JPanel panel = new Bar();
Nick G.
sorry, i had the frame in the constructor so i could keep the rest of your implementation untouched.
akf
A: 

Adding BorderLayout to it doesn't seem to work.

The default layout of a new JPanel is FlowLayout, as can be seen by resizing the frame. To see the difference, replace

frame.add(new JPanel()

with

frame.add(new JPanel(new BorderLayout())

As @akf suggests, lengthy static initialization using double braces can be obscure.

trashgod
+1  A: 

On top of all answers already given... Your program has fundamental flaw. All manipulation with Swing components has to be done on EDT thread. So your code should be slightly different

class FrameDemo
{
    public static void main(String[] args)
    {
         SwingUtilities.invokeLater( new Runnable() {
                 void run() {
                    /// your code here
                 }    
         }); 
    }
}

Otherwise what happens is unpredictable. You can read more about it at http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html

eugener
+1 Even when it's not the problem, it's the only way to rule it out.
trashgod