Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

java - How to create a slide panel

I try to change a layout of the panel, but there are problems, because when I change layout the components remain on the panel and there are many bags.

import java.awt.LayoutManager;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LayoutTest extends JFrame {
public LayoutTest() {
    initComponents();
}

private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton = new JButton();

    jPanel1.setBackground(new java.awt.Color(204, 204, 255));
    jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

    jButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            LayoutManager tempGroupLayout = jPanel1.getLayout();
            jPanel1.setLayout(layoutManager2);
            jPanel1.getParent().validate();
            layoutManager = layoutManager2;
            layoutManager2 = tempGroupLayout;
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
            addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().
            addContainerGap(286, Short.MAX_VALUE).
            addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
    jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
            addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
    layoutManager = jPanel1Layout;

    javax.swing.GroupLayout jPanel1Layout2 = new javax.swing.GroupLayout(jPanel1);
    jPanel1Layout2.setHorizontalGroup(
            jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
            addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout2.createSequentialGroup().
            addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
    jPanel1Layout2.setVerticalGroup(
            jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
            addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
    layoutManager2 = jPanel1Layout2;
    getContentPane().add(jPanel1);
    pack();
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new LayoutTest().setVisible(true);
        }
    });
}
private JButton jButton;
private JPanel jPanel1;
private LayoutManager layoutManager;
private LayoutManager layoutManager2;

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have a look at the JSplitPane component. You can nest a few of them.

splitPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, new SplitPanePropertyChangeListener());

private class SplitPanePropertyChangeListener implements PropertyChangeListener {    
    public void propertyChange(java.beans.PropertyChangeEvent evt) {    
        int nv = ((Integer) evt.getNewValue()).intValue();    
         if (nv == 1) { //adjust this depending on your divider location    
         setSize(new Dimension(800, 560));    
      }    
      if (nv == 221) { //adjust this depending on your divider location    
         setSize(new Dimension(1000, 560));    
      }     
   }    
}

Check with above code and remodel it for your requirement


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...