我正在尝试使用3行3列的formlayout。中间的长方体设置为固定宽度和固定高度,其他长方体将增长。我想这会让我的图像保持在中心,而不会随着用户的扩展而增长。
以下是我正在做的事情,但我在控制台中遇到了错误:
package me.codplaymakers.com;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
public class WindowBuilderTest2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WindowBuilderTest2 frame = new WindowBuilderTest2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public WindowBuilderTest2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0,0, 1200, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new FormLayout(new ColumnSpec[] {
ColumnSpec.decode("left:default:grow"),
ColumnSpec.decode("800px"),
ColumnSpec.decode("right:default:grow"),},
new RowSpec[] {
RowSpec.decode("top:default:grow"),
RowSpec.decode("500px"),
RowSpec.decode("bottom:default:grow"),}));
JLabel lblNewLabel = new JLabel("");
ImageIcon icon = new ImageIcon(this.getClass().getResource("/FullSizeRender.jpg"));
Image img = icon.getImage();
BufferedImage bi = new BufferedImage(lblNewLabel.getWidth(), lblNewLabel.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(img, 0, 0,lblNewLabel.getWidth(),lblNewLabel.getHeight(), null);
ImageIcon newIcon = new ImageIcon(bi);
lblNewLabel.setIcon(newIcon);
contentPane.add(lblNewLabel, "2, 2");
}
}
下面是错误消息:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/jgoodies/common/base/Preconditions
at com.jgoodies.forms.layout.FormSpec.<init>(FormSpec.java:179)
at com.jgoodies.forms.layout.ColumnSpec.<init>(ColumnSpec.java:147)
at com.jgoodies.forms.layout.FormSpecs.<clinit>(FormSpecs.java:62)
at com.jgoodies.forms.layout.LayoutMap.createRoot(LayoutMap.java:569)
at com.jgoodies.forms.layout.LayoutMap.getRoot(LayoutMap.java:217)
at com.jgoodies.forms.layout.ColumnSpec.decode(ColumnSpec.java:199)
at me.codplaymakers.com.WindowBuilderTest2.<init>(WindowBuilderTest2.java:54)
at me.codplaymakers.com.WindowBuilderTest2$1.run(WindowBuilderTest2.java:34)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: com.jgoodies.common.base.Preconditions
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 22 more
ok,所以第一个问题是我需要在构建路径中包含jgoodies-common-1.1.1.jar。第二个问题是用设置图像的边界
lblNewLabel.setBounds(0, 0, 800, 500);