2012-07-15 75 views
1

我有一個類延伸JPanel,我想嵌入到JFrame。 L & F設置爲Nimbus,我用於面板的佈局是GridBagLayoutJTextArea上Nimbus跨不同機器


當我把JAR的朋友,一個JTextArea我打算爲日誌控制檯使用開始演戲了,也不會留我將它的大小。

textAreaLog.setMinimumSize(new Dimension(295, 48));

我使用的WinXP SP2和我的朋友使用的Win7 64位。下面是它的外觀我的電腦(左)和他的PC(右)上的圖片:

Image

很顯然,我想要它是我有我的機器上的方式。


下面是相關的代碼(幾乎用於面板全班同學):

package com.sassilization.mapfix.gui; 

// Imports the package with the inner-workings of the application 
import com.sassilization.mapfix.MapFixGenerator; 

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class LogPanel extends JPanel { 
    private static final long serialVersionUID = 8324191587703173738L; 

    /* 
    * Constructor 
    */ 
    public LogPanel() { 
     setPreferredSize(new Dimension(350, 70)); 
     // Creates a default Nimbus border 
     setBorder(BorderFactory.createTitledBorder((String) null)); 
     setOpaque(false); 

     setLayout(new GridBagLayout()); 
     // Calls the method which initializes all the components 
     initComponents(); 
    } 

    /* 
    * Component declarations 
    */ 
    private JButton buttonFgd; 
    private JButton buttonHelp; 
    private JButton buttonLogCopy; 
    private JButton buttonLogDown; 
    private JButton buttonLogUp; 
    private JTextArea textAreaLog; 
    private JToggleButton toggleButtonAppend; 

    /* 
    * Initializes and adds all the components to the panel 
    */ 
    private void initComponents() { 
     // The constraints used to lay out the components in a GBL 
     GridBagConstraints gbc = new GridBagConstraints(); 

     // The brick button 
     toggleButtonAppend = new JToggleButton(appendIcons[0]); 
     toggleButtonAppend.setBorder(BorderFactory.createEmptyBorder()); 
     toggleButtonAppend.setToolTipText("Turn append mode on"); 
     toggleButtonAppend.addItemListener(new ItemListener() { 
      public void itemStateChanged(ItemEvent event) { 
       buttonAppendItemStateChanged(event); 
      } 
     }); 
     add(toggleButtonAppend, gbc); 

     // The question mark button 
     buttonHelp = new JButton(new ImageIcon(getClass().getResource("resources/help.png"))); 
     buttonHelp.setBorder(BorderFactory.createEmptyBorder()); 
     buttonHelp.setToolTipText("Open help"); 
     buttonHelp.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       // buttonHelpActionPerformed(event); 
      } 
     }); 
     gbc.gridy = 1; 
     add(buttonHelp, gbc); 

     // The white page button 
     buttonFgd = new JButton(new ImageIcon(getClass().getResource("resources/page_white_put.png"))); 
     buttonFgd.setBorder(BorderFactory.createEmptyBorder()); 
     buttonFgd.setToolTipText("Extract FGD file"); 
     buttonFgd.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       // buttonFgdActionPerformed(event); 
      } 
     }); 
     gbc.gridy = 2; 
     add(buttonFgd, gbc); 

     // The problematic JTextArea 
     textAreaLog = new JTextArea(); 
     textAreaLog.setMinimumSize(new Dimension(295, 48)); 
     textAreaLog.setBorder(BorderFactory.createMatteBorder(0, 12, 0, 0, 
       new ImageIcon(getClass().getResource("resources/border.png")))); 
     textAreaLog.setBackground(new Color(0, 0, 0, 0)); 
     textAreaLog.setForeground(new Color(171, 193, 207)); 
     textAreaLog.setFont(new Font(null, Font.PLAIN, 9)); 
     textAreaLog.setLineWrap(true); 
     textAreaLog.setWrapStyleWord(true); 
     textAreaLog.setEditable(false); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.gridheight = 3; 
     add(textAreaLog, gbc); 

     // The up arrow button 
     buttonLogUp = new JButton(new ImageIcon(getClass().getResource("resources/bullet_arrow_up.png"))); 
     buttonLogUp.setBorder(BorderFactory.createEmptyBorder()); 
     buttonLogUp.setContentAreaFilled(false); 
     buttonLogUp.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       // buttonLogUpActionPerformed(event); 
      } 
     }); 
     gbc.gridx = 2; 
     gbc.gridheight = 1; 
     add(buttonLogUp, gbc); 

     // The floppy disk button 
     buttonLogCopy = new JButton(new ImageIcon(getClass().getResource("resources/bullet_disk.png"))); 
     buttonLogCopy.setBorder(BorderFactory.createEmptyBorder()); 
     buttonLogCopy.setContentAreaFilled(false); 
     buttonLogCopy.setToolTipText("Copy log to clipboard"); 
     buttonLogCopy.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       // buttonLogCopyActionPerformed(event); 
      } 
     }); 
     gbc.gridy = 1; 
     add(buttonLogCopy, gbc); 

     // The down arrow button 
     buttonLogDown = new JButton(new ImageIcon(getClass().getResource("resources/bullet_arrow_down.png"))); 
     buttonLogDown.setBorder(BorderFactory.createEmptyBorder()); 
     buttonLogDown.setContentAreaFilled(false); 
     buttonLogDown.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       // buttonLogDownActionPerformed(event); 
      } 
     }); 
     gbc.gridy = 2; 
     add(buttonLogDown, gbc); 
    } 

    private ImageIcon appendIcons[] = { new ImageIcon(getClass().getResource("resources/brick.png")), 
      new ImageIcon(getClass().getResource("resources/brick_add.png")) }; 

    /* 
    * Event listener methods for the components go here. 
    */ 
} 

此外,這裏的主要JFrame類實例化LogPanel,儘管註釋。包括的也是JAR的下載鏈接。

Link


我使用JPanel.setMinimumSize()這樣我就可以馴服JTextArea不使用JScrollPane我在想這個顯示不一致與此有關。如果我確實使用JScrollPane,它會完全混淆面板佈局,所以我寧願遠離。

在此先感謝。


編輯1:

如果我改變將L & F到默認或系統L & F,I得到了同樣的問題,我的朋友做;因此,這很可能與Nimbus本身有關。


編輯2:

原來有在JDK6, 我用其中,和JDK7之間雨雲代碼差異。我已經更新並用setPreferredSize()取代了 錯誤代碼 - 現在它的功能很棒。

+2

爲了更快得到更好的幫助,請將[SSCCE](http://sscce.org/)(沒有第三方API,文本代替圖像,簡稱..)作爲對問題的編輯。 – 2012-07-15 03:41:54

+0

@Andrew事實證明,我使用的JDK6和JDK7之間的Nimbus代碼存在差異。我已經更新並用'setPreferredSize()'替換了錯誤的代碼,現在它工作的很好。 – Konstantin 2012-07-15 05:01:36

+0

你應該考慮發表你的評論作爲答案並接受它,這種方式很容易找到其他人有類似的問題:-) – kleopatra 2012-07-22 16:24:02

回答

1

我已經找到了解決辦法:

原來有在JDK6, 我用它,並JDK7的雨雲代碼差異。我已經更新並用setPreferredSize()取代了 錯誤代碼 - 現在它的功能很棒。