Panel overlap

I am trying to make the following layout where all JPanels are visible, with the exception of panel2, when the program starts. When the user presses btn1, then JCalendar and panel3 become invisible, and panel2 becomes visible. The problem I ran into is that panel2 is not showing with btn1 pressed. However, if I change the borderlayout of panel2 to one that is not used (in this case WEST), it will be displayed when the button is pressed, but it will align on the left side, and I want it to be centered in shape.

code:

public class GUI extends JFrame implements ActionListener, PropertyChangeListener { private JPanel panel1, panel2, panel3; private com.toedter.calendar.JCalendar calendar; private Button btn1, btn2; private JLabel label1, label2; public GUI() { init(); } private void init() { //panel1 components panel1 = new JPanel(); btn1 = new JButton("Click me"); panel1.add(btn1); //panel2 components panel2 = new JPanel(); label1 = new JLabel("Time:"); label2 = new JLabel("Date:"); panel2.add(label1); panel2.add(label2); //JCalendar calendar = new com.toedter.calendar.JCalendar(); //panel3 panel3 = new JPanel(); //Add panels to JFrame add(panel1, BorderLayout.NORTH); add(calendar, BorderLayout.CENTER); add(panel2, BorderLayout.CENTER); //if i set this to WEST it show!! add(panel3, BorderLayout.EAST); //event handling btn1.addActionListener(this); //hide panel2 panel2.setVisible(false); pack(); } public void actionPerformed(ActionEvent ae) { if(ae.getSource().equals(btn1) { calendar.setVisible(false); panel3.setVisible(false); panel2.setVisible(true); //make panel2 visible panel2.updateUI(); revalidate(); repaint(); } } public static void main(String args[]) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new GUI().setVisible(true); } }); } 

enter image description here

When I press btn1, JCalendar and panel3 are invisible, but panel2 does not show

enter image description here

+4
source share
2 answers

There are a number of problems that I can find ...

  • BorderLayout will allow only one component to take any given position. That is, the two components cannot share the CENTER position at the same time, regardless of whether it is invisible or not.
  • You should never call updateUI , this is used to tell the user interface components that the appearance has changed, and they should be updated in response to it.
  • Use revalidate to tell the container that some changes have occurred to the layout, that it should perform a new revalidate process ...

Before clicking ...

enter image description here

After a click ...

enter image description here

 import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.LineBorder; public class GUI extends JFrame implements ActionListener, PropertyChangeListener { private JPanel panel1, panel2, panel3; // private com.toedter.calendar.JCalendar calendar; private JPanel calendar; private JButton btn1, btn2; private JLabel label1, label2; public GUI() { setDefaultCloseOperation(EXIT_ON_CLOSE); init(); } private void init() { //panel1 components panel1 = new JPanel(); btn1 = new JButton("Click me"); panel1.add(btn1); //panel2 components panel2 = new JPanel(); label1 = new JLabel("Time:"); label2 = new JLabel("Date:"); panel2.add(label1); panel2.add(label2); //JCalendar calendar = new JPanel();//new com.toedter.calendar.JCalendar(); calendar.setBorder(new LineBorder(Color.RED)); calendar.add(new JLabel("Calendar")); //panel3 panel3 = new JPanel(); panel3.setBorder(new LineBorder(Color.BLUE)); panel3.add(new JLabel("Panel3")); panel2.setBorder(new LineBorder(Color.GREEN)); //Add panels to JFrame add(panel1, BorderLayout.NORTH); add(calendar, BorderLayout.WEST); add(panel2, BorderLayout.CENTER); add(panel3, BorderLayout.EAST); //event handling btn1.addActionListener(this); //hide panel2 panel2.setVisible(false); pack(); } public void actionPerformed(ActionEvent ae) { if (ae.getSource().equals(btn1)) { calendar.setVisible(false); panel3.setVisible(false); panel2.setVisible(true); //make panel2 visible // panel2.updateUI(); revalidate(); repaint(); } } public static void main(String args[]) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new GUI().setVisible(true); } }); } @Override public void propertyChange(PropertyChangeEvent evt) { } } 

Now I’m sure that this will not meet you according to all requirements (as I see them) ... You have at least two options ...

  • Remove the Calendar component and add panel2 to the CENTER position when you click
  • Preferably Use CardLayout
+4
source

BorderLayout does not overlap, IIRC none of the layout managers speak of a "overlapping layout".

You need to do it differently - try using a JLayeredPane with existing JPanel and BorderLayout as the bottom layer, and then your (optional) panel as the top layer.

See: http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

+1
source

Source: https://habr.com/ru/post/1480020/


All Articles