0

I have a JFrame and i need to add panels to it where i like, but I am not sure how to do it. I have tried all the layouts but i cant get what i want.

Basically FunProgramming is the main frame and I need 3 other panels on that frame. i need one in the middle/right. i need a panel to the left and one across the bottom and about 1/3 up the frame. After trying the different layout managers I'm not sure how to go about doing this(also sorry, i couldn't seem to separate the two classes, the first one it now missing a {)

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class FunProgrammingFrame {

private JFrame frame; 
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem;
private final String M_ITEM_ONE = "File", M_ITEM_TWO = "Project",       M_ITEM_THREE = "Help", ITEM_ONE = "New", ITEM_TWO = "Open", ITEM_THREE = "Save",     ITEM_FOUR = "Quit";

public FunProgrammingFrame(){
    ConsolePanel consolePanel = new ConsolePanel();
    frame = new JFrame("Fun Programming"); 
    frame.setLayout(new GridBagLayout());
    //frame.add(consolePanel, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

    menuBar = new JMenuBar();
    createMenu(M_ITEM_ONE);
    addMenuItem(ITEM_ONE);
    menuItem.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){ 

        }
    });
    addMenuItem(ITEM_TWO);
    menuItem.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){ 

        }
    });
    addMenuItem(ITEM_THREE);
    menuItem.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){ 

        }
    });
    menu.addSeparator();
    addMenuItem(ITEM_FOUR);
    menuItem.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){ 
            System.exit(0);
        }
    });
    createMenu(M_ITEM_TWO);

    createMenu(M_ITEM_THREE);

    //addPanel(consolePanel, 10, 10, GridBagConstraints.SOUTH);

    frame.setVisible(true);
}

public void createMenu(String name){
    menu = new JMenu(name);
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
}

 public JMenuItem addMenuItem( String itemName) {
    menuItem = new JMenuItem(itemName);
    menu.add(menuItem); 
    return menuItem;
 }

 public static void main(String[] args){
    FunProgrammingFrame fp = new FunProgrammingFrame();
 }
} 


 import java.awt.BorderLayout;
 import java.awt.Color;
 import javax.swing.JPanel;
 public class ConsolePanel extends JPanel{

 private JPanel Cpanel;
 private final int WIDTH = 100, HEIGHT = 500;

 public ConsolePanel(){
     Cpanel = new JPanel(new BorderLayout());
     Cpanel.setSize(WIDTH, HEIGHT);
     Cpanel.setVisible(true);
     setBackground(Color.BLACK);
 }
}

Link to the what i need https://i.stack.imgur.com/9aibx.jpg the buttons to the side are not needed for now

enter image description here

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Spatulord
  • 3
  • 3
  • Don't forget, also, you can have panels within panels. – Phil Freihofner Feb 23 '15 at 05:00
  • I think I am doing it right for the most part, I'm just not sure how to put the panels where I want them to go, I've tried the layout mangers and they haven't seemed to work son I'm a little stumped – Spatulord Feb 23 '15 at 05:03

3 Answers3

1

Something like...

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ContentPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ContentPane extends JPanel {

        public ContentPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weighty = 0.7;
            add(new TestPane(Color.RED), gbc);
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.gridx++;
            add(new TestPane(Color.BLUE), gbc);

            gbc.gridy = 1;
            gbc.gridx = 0;
            gbc.weighty = 0.3;
            gbc.weightx = 1;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.anchor = GridBagConstraints.NORTH;
            add(new TestPane(Color.GREEN), gbc);
        }

    }

    public class TestPane extends JPanel {

        public TestPane(Color color) {
            setBorder(new LineBorder(color));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

You can try something like this:

public class Test {
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            MBar mBar = new MBar();

            TextAreaPanel scrPan = new TextAreaPanel();
            BottomPan pan = new BottomPan();
            ButtonsPanel buttsCon = new ButtonsPanel();

            f.setJMenuBar(mBar);

            f.getContentPane().add(buttsCon, BorderLayout.LINE_START);
            f.getContentPane().add(scrPan, BorderLayout.CENTER);
            f.getContentPane().add(pan, BorderLayout.PAGE_END);
            f.pack();
            f.setVisible(true);
        }
    });
  }
}

ButtonsPanel.java

public class ButtonsPanel extends JPanel {
    public ButtonsPanel() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        add(Box.createHorizontalStrut(50));

        JPanel butts = new JPanel();
        butts.setLayout(new BoxLayout(butts, BoxLayout.Y_AXIS));
        butts.add(Box.createVerticalStrut(30));
        for (int i = 0; i < 5; i++) {
            JButton butt = new JButton("Button " + String.valueOf(i));
            butts.add(butt);
            butts.add(Box.createVerticalStrut(10));
        }
        butts.setAlignmentY(JPanel.TOP_ALIGNMENT);
        butts.add(Box.createVerticalStrut(40));

        add(butts);
        add(Box.createHorizontalStrut(50));
    }
}

TextAreaPanel.java

public class TextAreaPanel extends JScrollPane {
    public TextAreaPanel() {
        JTextArea jtxtArea = new JTextArea();
        setViewportView(jtxtArea);
    }
}

BottomPan.java

public class BottomPan extends JPanel {
    public BottomPan() {
        setBackground(Color.black);
        setPreferredSize(new Dimension(100, 100));
    }
}

MBar.java

public class MBar extends JMenuBar {
    public MBar() {
        for (int i = 0; i < 5; i++) {
            JMenuItem mi = new JMenuItem("Menu Item " + String.valueOf(i));
            add(mi);
        }

    }
}

enter image description here

Titus
  • 22,031
  • 1
  • 23
  • 33
  • This helps allot, but they need each pane needs to be a separate class and i cant get them to work – Spatulord Feb 23 '15 at 06:20
  • @Spatulord, I've edited my answer, now all the components are in different classes, you should probably also use a `JScrollPane` for the `BottomPan` and `ButtonsPan` – Titus Feb 23 '15 at 06:49
0

You can let your main frame be a BorderLayout.

Place your ConsolePanel as follows:

frame.add(consolePanel, BorderLayout.WEST);
consolePanel.setPreferredSize(100, 400);

I'm guessing you will be able to take it from there.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • I tried the BorderLayout and it didnt work for what i was trying to do. – Spatulord Feb 23 '15 at 05:17
  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Feb 23 '15 at 06:19
  • Thanks for the interesting link! I hope to find time to study the various arguments soon. My inclination is to be cognizant that different layouts treat these methods differently (this has been the source of some confusion in trying to debug Swing layouts until I discovered the lack of consistency) over building custom or using 3rd party layouts. But there are good arguments made for the different strategies. Some people sure get dogmatic about their positions, though! Overly so for my taste. For almost every recommendation there exists a valid counter-example, imho (a real-world observation). – Phil Freihofner Feb 23 '15 at 09:23