Spatulord
Spatulord

Reputation: 3

Trouble with simple JPanels

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.sstatic.net/9aibx.jpg the buttons to the side are not needed for now

enter image description here

Upvotes: 0

Views: 69

Answers (3)

Titus
Titus

Reputation: 22484

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

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347332

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

    }

}

Upvotes: 1

Phil Freihofner
Phil Freihofner

Reputation: 7910

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.

Upvotes: 0

Related Questions