我有一个扩展JPanel
的课程,我希望将其嵌入JFrame
。 L& F设置为Nimbus,我用于面板的布局是GridBagLayout
。
当我把JAR交给朋友时,我打算用JTextArea
作为日志控制台开始表现,并且不会保持我设定的大小。
textAreaLog.setMinimumSize(new Dimension(295, 48));
我正在使用WinXP SP2,而我的朋友正在使用Win7 64位。这是一张关于它在我的电脑(左)和他的电脑(右)上的外观的图片:
显然我打算将它放在我的机器上。
这是相关代码(几乎是用于面板的整个类):
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的下载链接。
我正在使用JPanel.setMinimumSize()
,因此我可以在不使用JTextArea
的情况下驯服JScrollPane
。 我认为显示不一致与此有关。如果我使用JScrollPane
,它会彻底搞乱面板布局,所以我宁愿远离。
提前致谢。
编辑1:
如果我将L& F更改为默认值或系统L& F,我会得到我朋友所做的相同问题;因此,它最有可能与Nimbus本身有关。
编辑2:
事实证明,JDK6之间的Nimbus代码存在差异, 我正在使用的是JDK7。我已经更新并更换了 使用
setPreferredSize()
的错误代码 - 现在效果很好。
答案 0 :(得分:1)
我找到了解决方案:
事实证明,JDK6之间的Nimbus代码存在差异, 我正在使用的是JDK7。我已经更新并更换了 使用
setPreferredSize()
的错误代码 - 现在效果很好。