[英]JTextArea on Nimbus being inconsistent across different machines
I have a class extending JPanel
that I want to embed into a JFrame
. 我有一个扩展
JPanel
的类,我想将其嵌入到JFrame
。 The L&F is set to Nimbus, and the layout I'm using for the panel is a GridBagLayout
. L&F设置为Nimbus,而我用于面板的布局是
GridBagLayout
。
When I gave the JAR to a friend, a JTextArea
I intend to use as a log console started acting up and wouldn't stay the size I set it to. 当我将JAR交给一个朋友时,我打算用作日志控制台的
JTextArea
开始起作用,并且不会保持我设置的大小。
textAreaLog.setMinimumSize(new Dimension(295, 48));
I'm using WinXP SP2, and my friend's using Win7 64-bit. 我正在使用WinXP SP2,而我的朋友正在使用Win7 64位。 Here's a picture of how it looks on my PC (left) and his PC (right):
这是它在我的PC(左)和他的PC(右)上的外观的图片:
Obviously I intended it to be the way I have it on my machine. 显然,我打算将其作为在计算机上安装它的方式。
Here's the relevant code (almost the whole class used for the panel): 这是相关的代码(几乎整个面板都使用了该类):
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.
*/
}
Furthermore, here's the main JFrame
class which instantiates the LogPanel
, albeit uncommented. 此外,这是实例化
LogPanel
的主要JFrame
类,尽管没有注释。 Included is also a download link for the JAR. 还包括JAR的下载链接。
I'm using JPanel.setMinimumSize()
so I can tame the JTextArea
without using a JScrollPane
. 我正在使用
JPanel.setMinimumSize()
因此可以在不使用JScrollPane
情况下驯服JTextArea
。 I'm thinking the display inconsistency has to do with this. 我认为显示不一致与此有关。 If I do use a
JScrollPane
, it messes up the panel layout completely, so I'd rather stay away. 如果我确实使用
JScrollPane
,它将完全弄乱面板布局,所以我宁愿远离。
Thanks in advance. 提前致谢。
EDIT 1: 编辑1:
If I change the L&F to the default or the system L&F, I get the same issue my friend did; 如果将L&F更改为默认值或将系统L&F更改为我的朋友,则会遇到相同的问题。 therefore, it's most likely something to do with Nimbus itself.
因此,这很可能与Nimbus本身有关。
EDIT 2: 编辑2:
It turns out there are differences in the Nimbus code between JDK6, which I was using, and JDK7.
事实证明,我使用的JDK6和JDK7之间的Nimbus代码有所不同。 I have since updated and replaced the faulty code with
setPreferredSize()
—it works great now.此后,我更新了错误的代码,并用
setPreferredSize()
替换了错误的代码-现在可以正常使用了。
I've found a solution: 我找到了一个解决方案:
It turns out there are differences in the Nimbus code between JDK6, which I was using, and JDK7.
事实证明,我使用的JDK6和JDK7之间的Nimbus代码有所不同。 I have since updated and replaced the faulty code with
setPreferredSize()
—it works great now.此后,我更新了错误的代码,并用
setPreferredSize()
替换了错误的代码-现在可以正常使用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.