Java 为什么我的日本人最初看起来是白色的?
我制作了一个JApplet应用程序,它由九个按钮组成Java 为什么我的日本人最初看起来是白色的?,java,swing,graphics,paint,japplet,Java,Swing,Graphics,Paint,Japplet,我制作了一个JApplet应用程序,它由九个按钮组成 当小程序第一次启动时,它是完全白色的。如果单击按钮所在的区域,按钮将变为可见并起作用 启动小程序30秒后,按钮变为可见,但背景仍为白色 主菜单应该是这样的 为什么小程序不能正确加载? 小程序的代码如下所示: ConverterApplet.java: public class ConverterApplet extends JApplet { public void init() { try
- 当小程序第一次启动时,它是完全白色的。如果单击按钮所在的区域,按钮将变为可见并起作用
- 启动小程序30秒后,按钮变为可见,但背景仍为白色
- 主菜单应该是这样的
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);
}
}
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);
}
}
MainPanel.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);
}
}
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);
}
}
public类主面板扩展JPanel实现ActionListener{
专用最终静态字符串单位选择面板=“单位选择卡”;
私有最终静态字符串UNIT\u CONVERSION\u PANEL=“UNIT\u CONVERSION\u PANEL”;
JPanel cardPanel;
私有JButton[]单元按钮;
私有JButton backButton;
私人卡布局卡布局;
专用单元转换面板单元转换面板;
公共主面板()
{
超级(新GridBagLayout());
cardPanel=新的JPanel();
JPanel unitSelectionPanel=newjpanel();
backButton=新的JButton(“
不要调用在paint中更改GUI的方法,这样做通常会触发重新绘制,而重新绘制又会调用paint,而paint又会设置大小,进而
另外,不要试图从Java代码中调整小程序的大小(永远)。应在HTML中设置小程序的大小。删除整个方法。确保在添加组件后调用validate()
…希望制作这个..可执行jar。我应该使用JFrame吗
是的,当然
然后使用链接启动JFrame
。创建一个“双击-启动”应用程序。为最终用户提供了一个不错的体验,但JWS让它变得非常流畅和专业。抱歉,我养成了一个坏习惯。我试图更改主面板的大小,但没有效果。还有其他方法吗?删除该方法解决了问题BLIM,但是Applet窗口太小了。我有点希望把这个小程序变成一个可执行的jar。我应该使用JFrice吗?对不起,如果这是一个愚蠢的问题。谢谢Web启动提示:)@ W.K.S.:也考虑一个空的边框在代码>主板< /代码> + 1。