BorderLayout orientation

349 views Asked by At

I'm trying to create the top buttons of a window. I have a JFrame and a JPanel with the differents buttons when I try to add the panel with the buttons to a JPanel on the frame, it doesn't show... digging and trying to find the solution, I realize that the issue is when I set the orientation to the panel with the buttons on the BorderLayout panel. I think that it might be something dumb that I haven't realize but I haven't found any issue like this.

The issue is here when I set the orientation:

contentPanel.add(buttons,BorderLayout.PAGE_START);

if I remove the:

BorderLayout.PAGE_START

it works

This is my Frame:

package view;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.CardLayout;
import java.awt.BorderLayout;

public class MainFrame extends JFrame{
 private JPanel contentPanel, layOutPanel;
 private CardLayout mainCardLayout;
 private BorderLayout borderLayout;
 private static MainFrame instance = null;
 private FrameButtonsPanel buttons;

private MainFrame(){
 setSize(1000,700);
 //setUndecorated(true);
 setLocationRelativeTo(null);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


 contentPanel = new JPanel();
 borderLayout = new BorderLayout();
 contentPanel.setLayout(borderLayout);
 add(contentPanel);

 buttons = new FrameButtonsPanel();
 buttons.setBackground(Color.red);
 contentPanel.add(buttons,BorderLayout.PAGE_START);

 /*layOutPanel = new JPanel();
 mainCardLayout = new CardLayout();
 layOutPanel.setLayout(mainCardLayout);
 layOutPanel.setBackground(Color.red);
 contentPanel.add(layOutPanel,BorderLayout.SOUTH);*/
}

 public static MainFrame getInstance(){
  if (instance == null){
   instance = new MainFrame();
  }
  return instance;
 }

  public static void main(String[] args) {
   MainFrame.getInstance().setVisible(true);
  }
 }

and this is my panel with the buttons:

package view;

import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.Spring;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

 public class FrameButtonsPanel extends JPanel{
 private Spring spring;
 private JButton iconify, maximize, close;

  public FrameButtonsPanel(){
   SpringLayout mySpring = new SpringLayout();
   setLayout(mySpring);

  iconify = new JButton("-");
  add(iconify);
  maximize = new JButton("O");
  add(maximize);
  close = new JButton("X");
  add(close);

  spring = Spring.constant(850,850,2000);


mySpring.putConstraint(SpringLayout.WEST,iconify,spring,SpringLayout.WEST,this);
mySpring.putConstraint(SpringLayout.WEST,maximize,3,SpringLayout.EAST,iconify);
mySpring.putConstraint(SpringLayout.WEST,close,3,SpringLayout.EAST,maximize);
mySpring.putConstraint(SpringLayout.EAST,this,3,SpringLayout.EAST,close);

iconifyWindow();
maximizeWindow();
closeWindow();
}

 private void iconifyWindow(){
  iconify.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
        MainFrame.getInstance().setExtendedState(JFrame.ICONIFIED);
     }
   });
 }

 private void maximizeWindow(){
  maximize.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0) {
    if(MainFrame.getInstance().getExtendedState() == JFrame.MAXIMIZED_BOTH){
        MainFrame.getInstance().setExtendedState(JFrame.NORMAL);
    }else{
        MainFrame.getInstance().setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
        }
     });
  }

private void closeWindow(){
  close.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    });
 }
}
1

There are 1 answers

2
camickr On BEST ANSWER

I have no idea why you are trying to use a SpringLayout to display buttons.

Just use a JPanel with a right aligned FlowLayout.

Read the FlowLayout API for more information on how to right align the components added to the panel.