未显示JSplitPane setOneTouchExpandable(true)

时间:2016-05-15 01:53:59

标签: java swing jsplitpane

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

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

此处完整代码

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

在预览模式下

enter image description here

未显示 ...

enter image description here

问题是什么?

我该如何解决?

测试外观

enter image description here

雨云 enter image description here

Windows Classic enter image description here

CDE /基序 enter image description here

金属 enter image description here

2 个答案:

答案 0 :(得分:1)

可能您正在寻找SplitPane.oneTouchButtonOffset

enter image description here

import java.awt.*;
import javax.swing.*;

public class OneTouchButtonOffsetTest {
  public JComponent makeUI() {
    System.out.println(UIManager.getInt("SplitPane.oneTouchButtonOffset"));
    UIManager.put("SplitPane.oneTouchButtonOffset", 0);

    UIDefaults d = new UIDefaults();
    d.put("SplitPane:SplitPaneDivider[Enabled+Vertical].foregroundPainter",
      new Painter<JComponent>() {
        @Override public void paint(Graphics2D g, JComponent c, int w, int h) {
          /* empty */
        }
    });
    JSplitPane splitPane = new JSplitPane();
    splitPane.putClientProperty("Nimbus.Overrides", d);
    splitPane.setLeftComponent(makePanel());
    splitPane.setRightComponent(makePanel());
    splitPane.setOneTouchExpandable(true);

    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    p.add(splitPane, BorderLayout.NORTH);
    return p;
  }
  private static JComponent makePanel() {
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JButton(), BorderLayout.WEST);
    p.add(new JTextField("aaaaaaaa"));
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      try {
        for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
          if ("Nimbus".equals(laf.getName())) {
            UIManager.setLookAndFeel(laf.getClassName());
          }
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new OneTouchButtonOffsetTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

答案 1 :(得分:1)

根据@aterai:

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

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

使用的方法代码......

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

请评论!