为什么我的JApplet最初显示为白色?

时间:2012-09-01 15:20:42

标签: java swing graphics paint japplet

我制作了一个由九个按钮组成的JApplet应用程序。

  • 首次启动小程序时,它完全是白色的。如果单击按钮所在的区域,该按钮将变为可见并将起作用。

enter image description here

  • 小程序启动后30秒,按钮变为可见,但背景仍为白色。

enter image description here

  • 主菜单应如下所示。

enter image description here

为什么小程序无法正确加载? applet的代码如下:

ConverterApplet.java:

public class ConverterApplet extends JApplet {

    public void init()
    {
        try{
            SwingUtilities.invokeAndWait(new Runnable(){

                @Override
                public void run() 
                {
                    createGUI();

                }

            });
        }catch(Exception e)
        {
            System.err.println("GUI could not be created.");
        }
    }

    public void start(){}
    public void stop(){}

    public void destroy(){}

    public void paint(java.awt.Graphics g){
        resize(400,400);   
    };

    private void createGUI()
    {
        MainPanel m = new MainPanel();
        getContentPane().add(m,BorderLayout.CENTER); 
    }

}

MainPanel.java:

public class MainPanel extends JPanel implements ActionListener {

    private final static String UNIT_SELECTION_PANEL = "UNIT_SELECTION_CARD";
    private final static String UNIT_CONVERSION_PANEL = "UNIT_CONVERSION_PANEL";    

    JPanel cardPanel;
    private JButton[] UnitButtons;
    private JButton backButton; 
    private CardLayout cardLayout;
    private UnitConversionPanel unitConversionPanel;

    public MainPanel()
    {
        super(new GridBagLayout());

        cardPanel = new JPanel();
        JPanel unitSelectionPanel = new JPanel();

        backButton = new JButton("<-");
        backButton.addActionListener(this);
        backButton.setVisible(false);

        UnitButtons = new JButton[9];
        UnitButtons[0] = new JButton("Length");
        UnitButtons[1] = new JButton("Time");
        UnitButtons[2] = new JButton("Mass");
        UnitButtons[3] = new JButton("Speed");
        UnitButtons[4] = new JButton("Volume");
        UnitButtons[5] = new JButton("Area");
        UnitButtons[6] = new JButton("Pressure");       
        UnitButtons[7] = new JButton("Temperature");
        UnitButtons[8] = new JButton("Energy");

        for(int i=0;i<UnitButtons.length;i++)
        {
            UnitButtons[i].addActionListener(this);
        }

        GridLayout layout = new GridLayout(0,3,20,20);
        unitSelectionPanel.setLayout(layout);

        for(JButton buttons: UnitButtons)
        {
            unitSelectionPanel.add(buttons);
        }

        unitConversionPanel = new UnitConversionPanel();

        cardLayout = new CardLayout();
        cardPanel.setLayout(cardLayout);
        cardPanel.add(UNIT_SELECTION_PANEL, unitSelectionPanel);
        cardPanel.add(UNIT_CONVERSION_PANEL, unitConversionPanel);
        cardLayout.show(cardPanel, UNIT_SELECTION_PANEL);

        GridBagConstraints c = new GridBagConstraints();
        //c.fill = GridBagConstraints.VERTICAL;
        c.gridx = 0;
        c.gridy = 0;
        //c.insets = new Insets(5,5,5,5);
        add(backButton,c); 

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth=2;
        c.gridheight=1;
        add(cardPanel,c);       
    }

}

1 个答案:

答案 0 :(得分:3)

public void paint(java.awt.Graphics g){ 
    resize(400,400); 
}; 

不要调用改变绘画中GUI的方法,这样做通常会触发重绘,反过来又会调用绘画,而反过来会设置一个大小,反过来......

此外,不要尝试从Java代码(永远)中调整applet的大小。应在HTML中设置applet大小。删除整个方法。添加组件后务必致电validate()

  

..希望能够制作这个..可执行jar。我应该使用JFrame吗?

是的,当然。

然后使用Java Web Start从链接启动JFrame。创建“双击 - 启动”应用。为最终用户提供了良好的体验,但JWS使它非常灵活和专业。