How to set size of applet?

2019-03-03 04:21发布

问题:

I have written a little test applet and start the applet via Eclipse appletviewer. I have noted tag at the begginig of the code, but appletviewer doesn't see it. It starts the applet in standart window with the same size every time. I use JDK 1.7, Eclipse Kepler

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/*<applet code="TestApplet" width=200 height=40>
</applet>
*/
public class TestApplet extends JApplet{
    private Container cp;
    private JPanel mainPanel, testPanel1;
    private JLabel testLabel1 = new JLabel("Test1"),
                testLabel2 = new JLabel("Test2");
    private JTextField testTextField1 = new JTextField(),
            testTextField2 = new JTextField();
    private JButton testButton1 = new JButton("TestButton1"),
            testButton2 = new JButton("TestButton2");
    public void init(){
        cp = getContentPane();
        testPanel1 = new JPanel(new GridLayout(3,1));
        cp.add(testPanel1);
        testPanel1.add(testLabel1);
        testPanel1.add(testTextField1);
        testPanel1.add(testButton1);
    }

    public void start(){
    }

    public void stop(){

    }

    public void destroy(){

    }
}

How can I change size of the applet window when I start it using Eclipse?

回答1:

You can change it in Run Configurations in Eclipse:

Run -> Run Configurations -> Java Applet -> New Configuration. the default size can be changed in the Parameters tab.

Note that this is only for testing and the actual applet your user sees is depends on how the applet is launched for them (Which is done typically via a <applet>, <object> or <embed> tags in a webpage. all of these tags support size attributes.)