我做了一個由九個按鈕組成的JApplet應用程序。爲什麼我的JApplet最初顯示爲白色?
- 當小程序首次啓動時,它是完全白色的。如果您點擊按鈕所在的區域,該按鈕將變爲可見並將起作用。
- 30秒,小程序啓動後,該按鈕變得可見,但背景仍然是白色。
- 主菜單應該是這樣的。
爲什麼小程序無法正常加載? 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);
}
}
對不起,抱起一個壞習慣。我試圖改變主面板的大小,但沒有奏效。有另一種方法嗎? –
刪除該方法解決了問題,但小程序窗口太小。我有點兒希望把這個小程序變成一個可執行的jar。我應該使用JFrame嗎?如果這是一個愚蠢的問題,我很抱歉。 –