Applet什么都没显示

时间:2015-12-10 06:42:52

标签: java swing applet

当我运行Java代码时,我发现Applet根本没有显示任何内容。但Applet窗口向我显示“Applet Started”

HelloWorld.java,主程序。

import javax.swing.*;

    public class HelloWorld extends JApplet{
        public static void main(String[] args){

            TheWindow w = new TheWindow();
            w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            w.setSize(230, 280);
            w.setVisible(true);
        }
    }

DrawOval.java,用于绘制圆圈

import java.awt.*;
import javax.swing.*;

public class DrawOval extends JPanel{

    private int d = 10;

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.fillOval(10, 10, d, d);
    }

    public void setD(int newD){
        d = (newD >=0 ? newD : 10);
        repaint();
    }

    public Dimension getPreferredSize(){
        return new Dimension(200, 200);
    }

    public Dimension getMinimumSize(){
        return getPreferredSize();
    }

}

TheWindow.java,用于定义滑块的属性。

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;


public class TheWindow extends JFrame{

    private JSlider slider;
    private DrawOval myPanel;

    public TheWindow(){
        super("title");
        myPanel = new DrawOval();
        myPanel.setBackground(Color.ORANGE);

        slider = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
        slider.setMajorTickSpacing(10);
        slider.setPaintTicks(true);

        slider.addChangeListener(
                new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        myPanel.setD(slider.getValue());
                    }
                }
        );

        add(slider, BorderLayout.SOUTH);
        add(myPanel, BorderLayout.CENTER);

    }
}

任何人只是给我一个提示,我们将不胜感激。 : - )

0 个答案:

没有答案