[英]A Multiplicity of Issues With Java Swing
我对Java图形用户界面缺乏经验,我只使用Java的GUI库,因为它方便我目前正在尝试实现的目标。 我有一个JPanel,其中有......多个问题。
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
读取了一个Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
错误。 这是我的代码:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.activation.MimetypesFileTypeMap;
public class UpdateMechanism {
private static void showGUI() {
JFrame frame = new JFrame("The Neverhood Restoration Project");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage icoImage = null;
try {
icoImage = ImageIO.read(
frame.getClass().getResource("/nhood.bmp"));
} catch (IOException e) {
System.out.println(e);
}
frame.setIconImage(icoImage);
JPanel heapPanel = new JPanel();
frame.setSize(new Dimension(1024, 600));
heapPanel.setBackground(new Color(0xA64343));
JLabel headerLabel = new JLabel("The Neverhood Restoration Project");
headerLabel.setHorizontalTextPosition(JLabel.CENTER);
try {
Font sFont = Font.createFont(Font.TRUETYPE_FONT, new File("DUGFB___.TTF"));
sFont = sFont.deriveFont(Font.PLAIN, 48);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(sFont);
headerLabel.setFont(sFont);
} catch (FontFormatException|IOException e) {
System.out.println(e);
}
JEditorPane updateLog = new JEditorPane();
updateLog.setSize(800, Short.MAX_VALUE);
JScrollPane scrollPane = new JScrollPane(updateLog);
updateLog.setEditable(false);
try {
updateLog.setPage("http://theneverhood.sourceforge.net/");
} catch (IOException e) {
updateLog.setContentType("text/html");
updateLog.setText("<html>The application could not load the webpage.</html>");
}
frame.add(headerLabel);
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
showGUI();
}
});
}
}
不要在组件上使用setSize()方法。 布局管理器可以使用首选/最小/最大大小来确定如何根据布局管理器的规则布置组件。 相反
//updateLog.setSize(800, Short.MAX_VALUE);
JScrollPane scrollPane = new JScrollPane(updateLog);
scrollPane.setPreferredSize( new Dimension(800, 400) );
JEditorPane位于文本上,使JLabel黯然失色。
默认情况下,JFrame使用BorderLayout。 此外,默认情况下,所有组件都会添加到中心,但仅显示最后添加的组件。 尝试:
//frame.add(headerLabel);
frame.add(headerLabel, BorderLayout.NORTH);
这行代码没有任何作用,因为您稍后在框架上调用pack():
frame.setSize(new Dimension(1024, 600));
如何添加边框?
请参阅Swing教程中有关如何使用边框的部分 。 阅读tuturial中的其他部分也不会有什么坏处。 布局管理器上有一个。
编辑:
如何将文本置于“BorderLayout.NORTH”的中心位置
阅读JLabel API。 您应该使用另一个setHorizontalXXX()方法而不是您尝试使用的方法:
为什么他的背景颜色不显示?
什么是堆面板? 为什么要设置颜色做什么? 您没有将面板添加到框架或向其添加任何组件。
如果您希望更改框架的背景,则需要访问内容窗格。 再次理解为什么需要阅读关于如何制作框架的Swing教程,该教程解释了内容窗格。
frame.getContentPane().setBackground(new Color(0xA64343));
为什么他的背景颜色不显示?
您将两个组件添加到框架的默认BorderLayout
的CENTER
位置。 如果您将标题添加到NORTH
,它可以很好地工作。
如果调用pack()
请不要在帧上调用setSize
。
updateLog.setSize(800, Short.MAX_VALUE);
也没用,因为包装滚动窗格的视口也有自己的布局,它将覆盖你的调用。
Regardin边界,您可以调用setBorder
并从BorderFactory
提供任何工厂值或提供您自己的实现。
查看此代码的更新版本:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class UpdateMechanism {
private static void showGUI() {
JFrame frame = new JFrame("The Neverhood Restoration Project");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel heapPanel = new JPanel();
frame.setSize(new Dimension(1024, 600));
heapPanel.setBackground(new Color(0xA64343));
JLabel headerLabel = new JLabel("The Neverhood Restoration Project");
headerLabel.setHorizontalTextPosition(JLabel.CENTER);
try {
Font sFont = Font.createFont(Font.TRUETYPE_FONT, new File("DUGFB___.TTF"));
sFont = sFont.deriveFont(Font.PLAIN, 48);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(sFont);
headerLabel.setFont(sFont);
} catch (FontFormatException | IOException e) {
System.out.println(e);
}
JEditorPane updateLog = new JEditorPane();
JScrollPane scrollPane = new JScrollPane(updateLog);
updateLog.setEditable(false);
try {
updateLog.setPage("http://theneverhood.sourceforge.net/");
} catch (IOException e) {
updateLog.setContentType("text/html");
updateLog.setText("<html>The application could not load the webpage.</html>");
}
frame.add(headerLabel, BorderLayout.NORTH);
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
showGUI();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.