I have been trying to adjust my JPanel sizes while using LayoutManager such as BoxLayout to arrange components inside that Layout in a specific order, I achieved the way I want it to look by NOT USING the Box Layout Manager.
Original idea which I want in this image:
But I want to achieve that while using Box Layout Manager for the Panels and when I do that, I get this output :
The output I get when I add BoxLayout:
This is the code :
AppPanel.java :
import javax.swing.*;
import java.awt.*;
public class AppPanel extends JPanel {
int Width, Height;
MenuButton mb;
AppPanel(int width, int height)
{
this.Width = width;
this.Height = height;
this.setMinimumSize(new Dimension(this.Width/ 4, this.Height));
this.setPreferredSize(new Dimension(3 * this.Width/ 4, this.Height));
this.setMaximumSize(new Dimension(this.Width/ 4, this.Height));
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.setBackground(Color.GREEN);
this.mb = new MenuButton("HELLO",this.getPreferredSize());
this.add(mb);
}
public void update(int width, int height){
this.Width = width;
this.Height = height;
this.setPreferredSize(new Dimension(3 * this.Width/ 4, this.Height));
// this.mb.update(this.mb.getGraphics());
}
}
MenuPanel.java:
import javax.swing.*;
import java.awt.*;
public class MenuPanel extends JPanel {
JLabel l_AppMenu;
int Width, Height;
MenuButton mb;
MenuPanel(int width, int height)
{
this.Width = width;
this.Height = height;
this.setMinimumSize(new Dimension(this.Width/ 4, this.Height));
this.setPreferredSize(new Dimension(this.Width/ 4, this.Height));
this.setMaximumSize(new Dimension(this.Width/ 4, this.Height));
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.setBackground(Color.BLACK);
this.mb = new MenuButton("HELLO 1",this.getPreferredSize());
l_AppMenu = new JLabel("App Menu");
l_AppMenu.setForeground(Color.GRAY);
this.add(l_AppMenu);
this.add(mb);
}
public void update(int width, int height){
this.Width = width;
this.Height = height;
this.setPreferredSize(new Dimension(3 * this.Width/ 4, this.Height));
// this.mb.update(this.mb.getGraphics());
}
}
MainFrame.java:
import javax.swing.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
public class MainFrame extends JFrame implements ComponentListener {
int Width, Height;
MenuPanel mp;
AppPanel ap;
public MainFrame()
{
this.Width = 1600;
this.Height = 900;
mp = new MenuPanel(this.Width, this.Height);
ap = new AppPanel(this.Width, this.Height);
this.setSize(this.Width, this.Height);
this.setVisible(true);
this.setTitle("ToolKit");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE );
this.add(mp);
this.add(ap);
addComponentListener(this);
this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),BoxLayout.X_AXIS));
}
public static void main(String[] args)
{
MainFrame m = new MainFrame();
}
@Override
public void componentResized(ComponentEvent componentEvent) {
System.out.println(this.getWidth() + " " + this.getHeight());
mp.update(this.getWidth(), this.getHeight());
ap.update(this.getWidth(), this.getHeight());
// this.setSize(this.getHeight() * 16 / 9 , this.getHeight());
}
@Override
public void componentMoved(ComponentEvent componentEvent) {
}
@Override
public void componentShown(ComponentEvent componentEvent) {
}
@Override
public void componentHidden(ComponentEvent componentEvent) {
}
}
MenuButton.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MenuButton extends JButton implements MouseListener {
String Text;
int Entered = 0;
int Pressed = 0;
MenuButton(String str, Dimension d)
{
super(str);
this.Text = str;
addMouseListener(this);
this.setMinimumSize(new Dimension((int) d.getWidth() - 20,40));
this.setMaximumSize(new Dimension((int) d.getWidth() - 20,40));
this.setPreferredSize(new Dimension((int) d.getWidth() - 20,40));
this.setBorder(null);
}
public void paintComponent(Graphics g)
{
if(this.Pressed == 0) {
g.setColor(new Color(182, 25, 25));
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(new Color(255, 107, 107));
g.fillRect(10, 10, this.getWidth() - 20, this.getHeight() - 20);
}
else{
g.setColor(new Color(255, 107, 107));
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(new Color(255, 107, 107));
g.fillRect(10, 10, this.getWidth() - 20, this.getHeight() - 20);
}
if(this.Entered == 0) {
g.setColor(new Color(253, 210, 199));
g.drawString(this.Text, this.getWidth() / 2 - g.getFontMetrics().stringWidth(this.Text) / 2, 25);
}
else{
g.setColor(new Color(1, 36, 67));
g.drawString(this.Text, this.getWidth() / 2 - g.getFontMetrics().stringWidth(this.Text) / 2, 25);
}
}
@Override
public void mouseClicked(MouseEvent mouseEvent) {
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
this.Pressed = 1;
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
this.Pressed = 0;
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
this.Entered = 1;
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
this.Entered = 0;
}
}