我试图更改要在JPanel中返回的createComponent()的大小,并使用getParent()将其调整为帧/屏幕的大小。但面板尺寸保持不变。我的CustomPanel的代码如下:

    class CustomPanel extends JPanel {
        final Image backImage = getBackImage();

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);
            g.drawImage(backImage, 0, 0, null);
        }

        @Override
        public Dimension getPreferredSize() {
            Container c = this.getParent();
            Dimension d = new Dimension(c.getWidth(), c.getHeight());
            return d;
        }
    }


createComponent()方法中的示例代码如下:

JPanel buttonPanel = new CustomPanel();
buttonPanel.add(button);
return buttonPanel;


我尝试在实例化buttonPanel时直接这样做,这给了我错误。此示例代码是:

JPanel buttonPanel = new JPanel();
Container c = buttonPanel.getParent();
Dimension d = new Dimension(c.getWidth(), c.getHeight());
buttonPanel.setSize(d);
buttonPanel.add(button);
return buttonPanel;


错误日志是:

Caused by: java.lang.NullPointerException
    at com.test.customcode.SampleScreenNew.createComponent(SampleScreenNew.java:101)
    at com.install4j.runtime.installer.frontend.ScreenEnvelope.addScreenContent(ScreenEnvelope.java:372)
    at com.install4j.runtime.wizard.StandardScreen.createMainPanel(StandardScreen.java:136)
    at com.install4j.runtime.wizard.StandardScreen.setupComponent(StandardScreen.java:78)
    at com.install4j.runtime.wizard.StandardScreen.initScreen(StandardScreen.java:50)
    at com.install4j.runtime.installer.frontend.ScreenEnvelope.init(ScreenEnvelope.java:61)
    at com.install4j.runtime.installer.frontend.ScreenEnvelope.<init>(ScreenEnvelope.java:46)
    at com.install4j.runtime.installer.frontend.WizardScreenExecutor$1.run(WizardScreenExecutor.java:199)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$300(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

在您的屏幕实施中,覆盖

@Override
public boolean isFillVertical() {
    return true;
}

@Override
public boolean isFillHorizontal() {
    return true;
}


然后,由createComponent()返回的面板将填充所有可用空间。

关于java - Install4j:在自定义InstallerScreen中更改JPanel的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33215094/

10-12 23:55