使用windowbuilder重新调整大小

时间:2015-11-19 16:40:06

标签: java swing windowbuilder form-layout

我正在尝试使用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

1 个答案:

答案 0 :(得分:1)

好的,所以第一个问题是我需要在构建路径中包含jgoodies-common-1.1.1.jar。第二个问题是用以下方式设置图像的边界:

lblNewLabel.setBounds(0, 0, 800, 500);