本文介绍了未显示JSplitPane setOneTouchExpandable(true)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用嵌套的JSplitPane ...

I'm using nested JSplitPane...

我想使用OneTouchExpandable,但是未显示运行代码...但是在预览模式下,我可以看到它.

I want to use OneTouchExpandable, but running the code is not shown...But in preview mode I can see it.

此处完整代码:

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;

package splitpane;

public class JFr_SplitPane extends JFrame {

  private JButton jbLeftRight;
  private JButton jbRightLeft;
  private JButton jbRightRight;
  private JLabel jlLeftLeft;
  private JPanel jpLeftLeft;
  private JPanel jpLeftRight;
  private JPanel jpMain;
  private JPanel jpRightLeft;
  private JPanel jpRightRight;
  private JSplitPane jspLeft;
  private JSplitPane jspMain;
  private JSplitPane jspRight;
  private JTextField jtfLeftLeft;
  private JTextField jtfLeftRight;
  private JTextField jtfRightLeft;
  private JTextField jtfRightRight;  

  public JFr_SplitPane() {
    initComponents();
  }
  private void initComponents() {

    jpMain = new JPanel();
    jspMain = new JSplitPane();
    jspLeft = new JSplitPane();
    jpLeftLeft = new JPanel();
    jlLeftLeft = new JLabel();
    jtfLeftLeft = new JTextField();
    jpLeftRight = new JPanel();
    jbLeftRight = new JButton();
    jtfLeftRight = new JTextField();
    jspRight = new JSplitPane();
    jpRightLeft = new JPanel();
    jbRightLeft = new JButton();
    jtfRightLeft = new JTextField();
    jpRightRight = new JPanel();
    jbRightRight = new JButton();
    jtfRightRight = new JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jpMain.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    jpMain.setPreferredSize(new Dimension(692, 36));

    jspMain.setDividerLocation(344);
    jspMain.setMinimumSize(new Dimension(276, 28));
    jspMain.setOneTouchExpandable(true);
    jspMain.setPreferredSize(new Dimension(688, 34));

    jspLeft.setDividerLocation(169);
    jspLeft.setMinimumSize(new Dimension(132, 25));
    jspLeft.setOneTouchExpandable(true);
    jspLeft.setPreferredSize(new Dimension(338, 30));

    jpLeftLeft.setMinimumSize(new Dimension(60, 25));
    jpLeftLeft.setPreferredSize(new Dimension(320, 25));

    jlLeftLeft.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    jlLeftLeft.setPreferredSize(new Dimension(24, 18));

    jtfLeftLeft.setPreferredSize(new Dimension(6, 25));

    GroupLayout jpLeftLeftLayout = new GroupLayout(jpLeftLeft);
    jpLeftLeft.setLayout(jpLeftLeftLayout);
    jpLeftLeftLayout.setHorizontalGroup(
      jpLeftLeftLayout.createParallelGroup(Alignment.LEADING)
      .addGroup(jpLeftLeftLayout.createSequentialGroup()
        .addGap(4, 4, 4)
        .addComponent(jlLeftLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        .addGap(2, 2, 2)
        .addComponent(jtfLeftLeft, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addContainerGap())
    );
    jpLeftLeftLayout.setVerticalGroup(
      jpLeftLeftLayout.createParallelGroup(Alignment.LEADING)
      .addGroup(jpLeftLeftLayout.createSequentialGroup()
        .addGap(3, 3, 3)
        .addComponent(jlLeftLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      .addComponent(jtfLeftLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    );

    jspLeft.setLeftComponent(jpLeftLeft);

    jpLeftRight.setMinimumSize(new Dimension(60, 25));
    jpLeftRight.setPreferredSize(new Dimension(320, 25));

    jbLeftRight.setPreferredSize(new Dimension(30, 21));

    jtfLeftRight.setPreferredSize(new Dimension(6, 25));

    GroupLayout jpLeftRightLayout = new GroupLayout(jpLeftRight);
    jpLeftRight.setLayout(jpLeftRightLayout);
    jpLeftRightLayout.setHorizontalGroup(
      jpLeftRightLayout.createParallelGroup(Alignment.LEADING)
      .addGroup(jpLeftRightLayout.createSequentialGroup()
        .addGap(4, 4, 4)
        .addComponent(jbLeftRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        .addGap(2, 2, 2)
        .addComponent(jtfLeftRight, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addContainerGap())
    );
    jpLeftRightLayout.setVerticalGroup(
      jpLeftRightLayout.createParallelGroup(Alignment.LEADING)
      .addGroup(jpLeftRightLayout.createParallelGroup(Alignment.CENTER)
        .addComponent(jbLeftRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        .addComponent(jtfLeftRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    );

    jspLeft.setRightComponent(jpLeftRight);

    jspMain.setLeftComponent(jspLeft);

    jspRight.setDividerLocation(169);
    jspRight.setMinimumSize(new Dimension(132, 25));
    jspRight.setOneTouchExpandable(true);
    jspRight.setPreferredSize(new Dimension(338, 30));

    jpRightLeft.setMinimumSize(new Dimension(60, 25));
    jpRightLeft.setPreferredSize(new Dimension(320, 25));

    jbRightLeft.setPreferredSize(new Dimension(30, 21));

    jtfRightLeft.setPreferredSize(new Dimension(6, 25));

    GroupLayout jpRightLeftLayout = new GroupLayout(jpRightLeft);
    jpRightLeft.setLayout(jpRightLeftLayout);
    jpRightLeftLayout.setHorizontalGroup(
      jpRightLeftLayout.createParallelGroup(Alignment.LEADING)
      .addGroup(jpRightLeftLayout.createSequentialGroup()
        .addGap(4, 4, 4)
        .addComponent(jbRightLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        .addGap(2, 2, 2)
        .addComponent(jtfRightLeft, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addContainerGap())
    );
    jpRightLeftLayout.setVerticalGroup(
      jpRightLeftLayout.createParallelGroup(Alignment.LEADING)
      .addGroup(jpRightLeftLayout.createSequentialGroup()
        .addGap(2, 2, 2)
        .addComponent(jbRightLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      .addComponent(jtfRightLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    );

    jspRight.setLeftComponent(jpRightLeft);

    jpRightRight.setMinimumSize(new Dimension(60, 25));
    jpRightRight.setPreferredSize(new Dimension(320, 25));

    jbRightRight.setPreferredSize(new Dimension(30, 21));

    jtfRightRight.setPreferredSize(new Dimension(6, 25));

    GroupLayout jpRightRightLayout = new GroupLayout(jpRightRight);
    jpRightRight.setLayout(jpRightRightLayout);
    jpRightRightLayout.setHorizontalGroup(
      jpRightRightLayout.createParallelGroup(Alignment.LEADING)
      .addGroup(jpRightRightLayout.createSequentialGroup()
        .addGap(4, 4, 4)
        .addComponent(jbRightRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        .addGap(2, 2, 2)
        .addComponent(jtfRightRight, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addContainerGap())
    );
    jpRightRightLayout.setVerticalGroup(
      jpRightRightLayout.createParallelGroup(Alignment.LEADING)
      .addGroup(jpRightRightLayout.createSequentialGroup()
        .addGap(2, 2, 2)
        .addComponent(jbRightRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      .addGroup(jpRightRightLayout.createSequentialGroup()
        .addComponent(jtfRightRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        .addGap(0, 0, Short.MAX_VALUE))
    );

    jspRight.setRightComponent(jpRightRight);

    jspMain.setRightComponent(jspRight);

    GroupLayout jpMainLayout = new GroupLayout(jpMain);
    jpMain.setLayout(jpMainLayout);
    jpMainLayout.setHorizontalGroup(
      jpMainLayout.createParallelGroup(Alignment.LEADING)
      .addComponent(jspMain, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    jpMainLayout.setVerticalGroup(
      jpMainLayout.createParallelGroup(Alignment.LEADING)
      .addGroup(Alignment.TRAILING, jpMainLayout.createSequentialGroup()
        .addGap(0, 0, 0)
        .addComponent(jspMain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    );

    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(Alignment.LEADING)
      .addGap(0, 702, Short.MAX_VALUE)
      .addGroup(layout.createParallelGroup(Alignment.LEADING)
        .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
          .addGap(0, 0, 0)
          .addComponent(jpMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addContainerGap()))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(Alignment.LEADING)
      .addGap(0, 54, Short.MAX_VALUE)
      .addGroup(layout.createParallelGroup(Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
          .addGap(0, 0, 0)
          .addComponent(jpMain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
          .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
    );

    pack();
  }

  public static void main(String args[]) {
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException ex) {
      Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
      Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
    }

    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new JFr_SplitPane().setVisible(true);
      }
    });
  }
}

在预览模式下:

未显示运行情况 ...

出了什么问题?

我该如何解决?

测试外观

Windows

雨云

Windows Classic

CDE/主题

金属

推荐答案

根据@aterai:

这里的代码适用于您项目的所有JSplitPane

Here the code to apply to all JSplitPane's of your project

UIManager.put("SplitPane.oneTouchButtonOffset", 0);
UIDefaults uidef = new UIDefaults();
uidef.put("SplitPane:SplitPaneDivider[Enabled+Vertical].foregroundPainter", 
    (Painter<JComponent>) (Graphics2D g, JComponent c, int w, int h) -> { });

List<JSplitPane> listComp1 = getSpecTypeComponents(this, JSplitPane.class);
for (JSplitPane lc : listComp1) {
  lc.putClientProperty("Nimbus.Overrides", uidef);
}

所用方法的代码...

The code of methods used...

/**
  return a List with all component according to Type class submmit
*/
private <T> List<T> getSpecTypeComponents(Container cntr, 
    Class<? extends javax.swing.JComponent> JCompClass) {
  List<Component> lstComp = new ArrayList<>();
  fillSpecTypeComponents(cntr, JCompClass, lstComp);
  return (ArrayList<T>)(lstComp);  // I'm not sure is bad practice...
}

/**

*/
private void fillSpecTypeComponents(Container cntr, 
    Class<? extends javax.swing.JComponent> JCompClass, List<Component> lstComp) {
  for (Component compnnt : cntr.getComponents()) {
    if (compnnt.getClass() == JCompClass) {
      lstComp.add(compnnt);
    }
    if (Container.class.isAssignableFrom(compnnt.getClass())) {
      fillSpecTypeComponents((Container) compnnt, JCompClass, lstComp);
    }
  }
}

请发表评论!

这篇关于未显示JSplitPane setOneTouchExpandable(true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 15:05