如何创建滑动面板

发布于 2024-10-26 01:42:05 字数 2885 浏览 4 评论 0原文

我尝试更改面板的布局,但存在问题,因为当我更改布局时,组件保留在面板上并且有很多包。

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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞花火° 2024-11-02 01:42:05

看一下 JSplitPane 组件。您可以嵌套其中一些。

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

检查上面的代码并根据您的要求进行改造

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文