2016-03-29 13 views
0

私はJComboBoxの次元を調べていました(私は他のSwingオブジェクトで提示されているかどうかわかりません)。スイング(JComboBox)バグ?印刷寸法

寸法が印刷されるときに問題が発生します。 バグですか?

ここに私のコード:

JPanel jpPanelOut = new JPanel(); 
    jpPanelOut.setBackground(new java.awt.Color(255, 255, 255)); 
    jpPanelOut.setLayout(new BoxLayout(jpPanelOut, BoxLayout.PAGE_AXIS)); 

    JPanel jpPanel = new JPanel(); 
    jpPanel.setPreferredSize(new Dimension(800, 26)); 

    Font inFont = new Font("Serif", Font.PLAIN, 10); 
    JPanel jpPanel0 = new JPanel(); 
    jpPanel0.setLayout(new BoxLayout(jpPanel0, BoxLayout.LINE_AXIS)); 

    JLabel jlLabel0 = new JLabel("this is a new text"); 
    jlLabel0.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 
    jlLabel0.setFont(inFont); 

    JSlider jslSlider0 = new JSlider(JSlider.HORIZONTAL, 0, 1000000000, 1); 
    jslSlider0.setFont(inFont); 

    JSpinner jspSpinner0 = new JSpinner(new SpinnerNumberModel(1, 0, 1000000000, 1)); 
    jspSpinner0.setFont(inFont); 

    JTextField jtfTextField0 = new JTextField("1234567890abcdef_gpqyTÑ",48); 
    jtfTextField0.setFont(inFont); 

    JComboBox jcbComboBox0 = new JComboBox<>(new String[] {"Average"}); 
    jcbComboBox0.setFont(inFont); 
    ((JLabel)jcbComboBox0.getRenderer()).setHorizontalAlignment(JLabel.RIGHT); 

    jpPanel0.add(jlLabel0); 
    jpPanel0.add(jslSlider0); 
    jpPanel0.add(jspSpinner0); 
    jpPanel0.add(jtfTextField0); 
    jpPanel0.add(jcbComboBox0); 



    inFont = new Font("Serif", Font.PLAIN, 10); 
    JPanel jpPanel1 = new JPanel(); 
    jpPanel1.setLayout(new BoxLayout(jpPanel1, BoxLayout.LINE_AXIS)); 

    JLabel jlLabel1 = new JLabel("this is a new text"); 
    jlLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 
    jlLabel1.setFont(inFont); 

    JSlider jslSlider1 = new JSlider(JSlider.HORIZONTAL, 0, 1000000000, 1); 
    jslSlider1.setFont(inFont); 

    JSpinner jspSpinner1 = new JSpinner(new SpinnerNumberModel(1, 0, 1000000000, 1)); 
    jspSpinner1.setFont(inFont); 

    JTextField jtfTextField1 = new JTextField("1234567890abcdef_gpqyTÑ",48); 
    jtfTextField1.setFont(inFont); 

    JComboBox jcbComboBox1 = new JComboBox<>(new String[] {"Average"}); 
    jcbComboBox1.setFont(inFont); 
    ((JLabel)jcbComboBox1.getRenderer()).setHorizontalAlignment(JLabel.RIGHT); 

    jpPanel1.add(jlLabel1); 
    jpPanel1.add(jslSlider1); 
    jpPanel1.add(jspSpinner1); 
    jpPanel1.add(jtfTextField1); 
    jpPanel1.add(jcbComboBox1); 

    System.out.println("Height:" 
     + " jtfTextField:" + jtfTextField1.getPreferredSize().getHeight() + "," 
     + jtfTextField1.getMinimumSize().getHeight() 
     + " jlLabel:" + jlLabel1.getPreferredSize().getHeight() + "," 
     + jlLabel1.getMinimumSize().getHeight() 
     + " jcbComboBox:" + jcbComboBox1.getPreferredSize().getHeight() + "," 
     + jcbComboBox1.getMinimumSize().getHeight() 
     + " jslSlider:" + jslSlider1.getPreferredSize().getHeight() + "," 
     + jslSlider1.getMinimumSize().getHeight() 
     + " jspSpinner:" + jspSpinner1.getPreferredSize().getHeight() + "," 
     + jspSpinner1.getMinimumSize().getHeight() 
     ); 

    jpPanelOut.add(jpPanel); 
    jpPanelOut.add(jpPanel0); 
    jpPanelOut.add(jpPanel1); 

しかし、寸法が変更されます!

enter image description here

見てください:のJTextFieldの

appereance(幅)

完全なコード

グリーンラインJComboBoxsの赤丸

appereance(テキスト)

package myPackage; 

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


public class NewJFrame extends javax.swing.JFrame { 

    public NewJFrame() { 
    initComponents(); 
    } 

    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

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

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jButton1.setText("jButton1"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
     jButton1ActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 0, Short.MAX_VALUE) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 220, Short.MAX_VALUE) 
    ); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
     .addContainerGap(739, Short.MAX_VALUE) 
     .addComponent(jButton1) 
     .addContainerGap()) 
     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
     .addComponent(jButton1) 
     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
     .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
     .addGap(0, 51, Short.MAX_VALUE)) 
    ); 

    pack(); 
    }// </editor-fold>       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
    JPanel jpPanelOut = new JPanel(); 
    jpPanelOut.setBackground(new Color(255, 255, 255)); 
    jpPanelOut.setLayout(new BoxLayout(jpPanelOut, BoxLayout.PAGE_AXIS)); 

    JPanel jpPanel = new JPanel(); 
    jpPanel.setPreferredSize(new Dimension(600, 26)); 

    Font inFont = new Font("Serif", Font.PLAIN, 10); 
    JPanel jpPanel0 = new JPanel(); 
    jpPanel0.setLayout(new BoxLayout(jpPanel0, BoxLayout.LINE_AXIS)); 

    JLabel jlLabel0 = new JLabel("this is a new text"); 
    jlLabel0.setBorder(BorderFactory.createEtchedBorder()); 
    jlLabel0.setFont(inFont); 

    JSlider jslSlider0 = new JSlider(JSlider.HORIZONTAL, 0, 1000000000, 1); 
    jslSlider0.setFont(inFont); 

    JSpinner jspSpinner0 = new JSpinner(new SpinnerNumberModel(1, 0, 1000000000, 1)); 
    jspSpinner0.setFont(inFont); 

    JTextField jtfTextField0 = new JTextField("1234567890abcdef_gpqyTÑ",48); 
    jtfTextField0.setFont(inFont); 

    JComboBox jcbComboBox0 = new JComboBox<>(new String[] {"Average"}); 
    jcbComboBox0.setFont(inFont); 
    ((JLabel)jcbComboBox0.getRenderer()).setHorizontalAlignment(JLabel.RIGHT); 

    jpPanel0.add(jlLabel0); 
    jpPanel0.add(jslSlider0); 
    jpPanel0.add(jspSpinner0); 
    jpPanel0.add(jtfTextField0); 
    jpPanel0.add(jcbComboBox0); 



    inFont = new Font("Serif", Font.PLAIN, 10); 
    JPanel jpPanel1 = new JPanel(); 
    jpPanel1.setLayout(new BoxLayout(jpPanel1, BoxLayout.LINE_AXIS)); 

    JLabel jlLabel1 = new JLabel("this is a new text"); 
    jlLabel1.setBorder(BorderFactory.createEtchedBorder()); 
    jlLabel1.setFont(inFont); 

    JSlider jslSlider1 = new JSlider(JSlider.HORIZONTAL, 0, 1000000000, 1); 
    jslSlider1.setFont(inFont); 

    JSpinner jspSpinner1 = new JSpinner(new SpinnerNumberModel(1, 0, 1000000000, 1)); 
    jspSpinner1.setFont(inFont); 

    JTextField jtfTextField1 = new JTextField("1234567890abcdef_gpqyTÑ",48); 
    jtfTextField1.setFont(inFont); 

    JComboBox jcbComboBox1 = new JComboBox<>(new String[] {"Average"}); 
    jcbComboBox1.setFont(inFont); 
    ((JLabel)jcbComboBox1.getRenderer()).setHorizontalAlignment(JLabel.RIGHT); 

    jpPanel1.add(jlLabel1); 
    jpPanel1.add(jslSlider1); 
    jpPanel1.add(jspSpinner1); 
    jpPanel1.add(jtfTextField1); 
    jpPanel1.add(jcbComboBox1); 

    System.out.println("Height:" 
     + " jtfTextField:" + jtfTextField1.getPreferredSize().getHeight() + "," 
     + jtfTextField1.getMinimumSize().getHeight() 
     + " jlLabel:" + jlLabel1.getPreferredSize().getHeight() + "," 
     + jlLabel1.getMinimumSize().getHeight() 
     + " jcbComboBox:" + jcbComboBox1.getPreferredSize().getHeight() + "," 
     + jcbComboBox1.getMinimumSize().getHeight() 
     + " jslSlider:" + jslSlider1.getPreferredSize().getHeight() + "," 
     + jslSlider1.getMinimumSize().getHeight() 
     + " jspSpinner:" + jspSpinner1.getPreferredSize().getHeight() + "," 
     + jspSpinner1.getMinimumSize().getHeight() 
     ); 

    jpPanelOut.add(jpPanel); 
    jpPanelOut.add(jpPanel0); 
    jpPanelOut.add(jpPanel1); 
    jPanel1.removeAll(); 
    jPanel1.setLayout(new GridLayout(1, 1)); 
    jPanel1.add(jpPanelOut); 
    }           


    public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    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) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
     new NewJFrame().setVisible(true); 
     } 
    }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JPanel jPanel1; 
    // End of variables declaration     
} 
+5

ポスト問題を示し、適切な[SSCCE](http://sscce.org/)。コードが提供されている – camickr

+0

に問題が再現できます。実際にこのコードでは、2つの下側パネルが供給された画像に表示されています。問題を再現するためにコードは必要ありません。 –

+0

投稿したコードをコンパイルしてテストできないため、問題を再現できません。 – camickr

答えて

1

問題の原因となったコンポーネントをより簡単に示すために、actionListenerコードを単純化することで、より良いSSCCEを作成することができました。 SSCCEには、問題を示す関連コードのみが含まれている必要があります。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    JPanel jpPanelOut = new JPanel(); 
    jpPanelOut.setBackground(new Color(255, 255, 255)); 
    jpPanelOut.setLayout(new BoxLayout(jpPanelOut, BoxLayout.PAGE_AXIS)); 

    JPanel jpPanel0 = new JPanel(); 
    JComboBox jcbComboBox0 = new JComboBox<>(new String[] {"Average"}); 
    jpPanel0.add(jcbComboBox0); 

    JPanel jpPanel1 = new JPanel(); 
    JComboBox jcbComboBox1 = new JComboBox<>(new String[] {"Average"}); 
    jpPanel1.add(jcbComboBox1); 

    SwingUtilities.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
      System.out.println("Height:" 
       + " jcbComboBox:" + jcbComboBox1.getPreferredSize().getHeight() + "," 
      ); 
     } 
    }); 

    jpPanelOut.add(jpPanel0); 
    jpPanelOut.add(jpPanel1); 
    jPanel1.removeAll(); 
    jPanel1.setLayout(new GridLayout(1, 1)); 
    jPanel1.add(jpPanelOut); 
} 

そこで、基本的な問題はのSystem.out.println(...)は別のスレッドで実行されます。 Swingコンポーネントのすべての更新は、Event Dispatch Threadで実行する必要があります。そのため、コンボボックスのgetPreferredSize()メソッドは、何らかの原因で問題が発生しているEDT外のコンボボックスに変更を引き起こしています。

解決策の1つは、コードをSwingUtilities.invokeLaterにラップすることです。別の解決策は、System.out.println(...)ステートメントをメソッドの最後に移動することです。これらの両方の解決策は、高さの印刷前にEDT上でコンボボックスを適切に実現することを可能にする。

0

たぶんこの問題は、Nimbus(Synth)LookAndFeelで発生します。

SynthArrowButtonUI

public Dimension getPreferredSize(JComponent c) { 
    SynthContext context = getContext(c); 
    Dimension dim = null; 
    if (context.getComponent().getName() == "ScrollBar.button") { 
    //... 
    } 
    if (dim == null) { 
    // For all other cases (including Spinner, ComboBox), we will 
    // fall back on the single ArrowButton.size value to create 
    // a square return value 
    int size = context.getStyle().getInt(context, "ArrowButton.size", 16); 
     dim = new Dimension(size, size); 
    } 
//... 

NimbusComboBoxSizeTest

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

public class NimbusComboBoxSizeTest { 
    public JComponent makeUI() { 
    String[] model = {"Average"}; 

    JPanel p0 = new JPanel(); 
    JComboBox<String> combo0 = new JComboBox<>(model); 
    p0.add(combo0); 

    JPanel p1 = new JPanel(); 
    JComboBox<String> combo1 = new JComboBox<>(model); 
    p1.add(combo1); 

    Box box = Box.createVerticalBox(); 
    box.add(p0); 
    box.add(p1); 

    EventQueue.invokeLater(() -> System.out.println("combo0: " + combo0.getPreferredSize())); 
    System.out.println("combo1: " + combo1.getPreferredSize()); 

    JPanel p = new JPanel(new BorderLayout()); 
    p.add(box, BorderLayout.NORTH); 

    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()); 
      UIDefaults d = UIManager.getLookAndFeelDefaults(); 
      System.out.println("ArrowButton.size: " + d.getInt("ArrowButton.size")); 
      System.out.println("ComboBox.arrowButton.size: " + d.getInt("ComboBox:\"ComboBox.arrowButton\".size")); 
      d.put("ArrowButton.size", 19); 
      //or: d.put("ComboBox:\"ComboBox.arrowButton\".size", 16); 
      } 
     } 
     } catch (Exception ex) { 
     ex.printStackTrace(); 
     } 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     f.getContentPane().add(new NimbusComboBoxSizeTest().makeUI()); 
     f.setSize(320, 240); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    }); 
    } 
} 
  • ArrowButton.size: 16
  • ComboBox.arrowButton.size: 19
  • combo1: java.awt.Dimension[width=74,height=26]
  • combo0: java.awt.Dimension[width=77,height=26]
関連する問題