2

在此处输入图像描述 *

(图例) Blue-mainpanel, Red-upanel, Black-ulpanel, Green-urpanel

* 所以我对 Applet 和 JFrame 有相同的代码。如您所见,在 `setsize() 相同的情况下,它们都会产生不同的结果。我无法弄清楚如何使我的 Applet 以相同的方式运行。

    import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Test2 extends Applet {

    JPanel mainpanel = new JPanel();
    JPanel upanel = new JPanel();
    JPanel dpanel = new JPanel();
    JPanel ulpanel = new JPanel();
    JPanel urpanel = new JPanel(); 
    JPanel dlpanel = new JPanel();
    JPanel drpanel = new JPanel();

    JLabel l1 = new JLabel("Label 1");
    JLabel l2 = new JLabel("Label 2");
    JLabel l3 = new JLabel("Label 3");
    JLabel l4 = new JLabel("Label 4");

    JTextField tb1 = new JTextField();
    JTextField tb2 = new JTextField();
    JTextField tb3 = new JTextField();

    JTextArea ta1 = new JTextArea();

    public void init() {

        mainpanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
        ulpanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        urpanel.setBorder(BorderFactory.createLineBorder(Color.green, 2));
        upanel.setBorder(BorderFactory.createLineBorder(Color.red, 2));
        mainpanel.setLayout(new GridLayout(1, 1));
        upanel.setLayout(new GridLayout(1, 2));
        urpanel.setLayout(new BorderLayout());
        urpanel.add(ta1,BorderLayout.CENTER);
        ulpanel.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        g.ipadx = 2;
        g.ipady = 2;
        g.insets = new Insets(2, 2, 2, 2);
//      g.anchor = GridBagConstraints.EAST;
        g.gridx = 0; g.gridy = 0;
        ulpanel.add(l1, g);
        g.gridx = 0; g.gridy = 1;
        ulpanel.add(l2, g);
        g.gridx = 2; g.gridy = 1;
        ulpanel.add(l3, g);
        g.gridx = 0; g.gridy = 2;
        ulpanel.add(l4, g);

        g.gridwidth = GridBagConstraints.REMAINDER;
        g.fill = GridBagConstraints.HORIZONTAL;
//      g.anchor = GridBagConstraints.WEST;
        g.weightx = 1;
        g.gridx = 1; g.gridy = 0;
        ulpanel.add(tb1, g);
        g.gridwidth = 1;
        g.gridx = 1; g.gridy = 1;
        ulpanel.add(tb2,g);
        g.gridx = 3; g.gridy = 1;
        ulpanel.add(tb3,g);

        upanel.add(ulpanel);
        upanel.add(urpanel);
        mainpanel.add(upanel);
        add(mainpanel);

    }
}

我尝试将 Applet 嵌入 HTML 并在其中指定大小,但仍然只有 Applet 的外部“外壳”采用该外壳,而不是我添加到 Applet 上的内部面板。

4

1 回答 1

3

+1 为 SSCCE

1)我不会将 Swing 组件与 AWT 混合使用,Applet而是使用JApplet

public class Test2 extends JApplet {

2) 使用SwingUtilities.invokeAndWait()block 将您的 UI 创建包装在覆盖中,init() 请参见此处

像这样(省略异常处理)。

@Override
public void init() {
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
            //create UI here
            }
        });
}

似乎它对我有用还是我错过了什么?:

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.lang.reflect.InvocationTargetException;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Test extends JApplet {

    JPanel mainpanel = new JPanel();
    JPanel upanel = new JPanel();
    JPanel dpanel = new JPanel();
    JPanel ulpanel = new JPanel();
    JPanel urpanel = new JPanel();
    JPanel dlpanel = new JPanel();
    JPanel drpanel = new JPanel();
    JLabel l1 = new JLabel("Label 1");
    JLabel l2 = new JLabel("Label 2");
    JLabel l3 = new JLabel("Label 3");
    JLabel l4 = new JLabel("Label 4");
    JTextField tb1 = new JTextField();
    JTextField tb2 = new JTextField();
    JTextField tb3 = new JTextField();
    JTextArea ta1 = new JTextArea();

    @Override
    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    initComponents();
                }
            });
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } catch (InvocationTargetException ex) {
            ex.printStackTrace();
        }
    }

    private void initComponents() {
        mainpanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
        ulpanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        urpanel.setBorder(BorderFactory.createLineBorder(Color.green, 2));
        upanel.setBorder(BorderFactory.createLineBorder(Color.red, 2));
        mainpanel.setLayout(new GridLayout(1, 1));
        upanel.setLayout(new GridLayout(1, 2));
        urpanel.setLayout(new BorderLayout());
        urpanel.add(ta1, BorderLayout.CENTER);
        ulpanel.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        g.ipadx = 2;
        g.ipady = 2;
        g.insets = new Insets(2, 2, 2, 2);
        //      g.anchor = GridBagConstraints.EAST;
        g.gridx = 0;
        g.gridy = 0;
        ulpanel.add(l1, g);
        g.gridx = 0;
        g.gridy = 1;
        ulpanel.add(l2, g);
        g.gridx = 2;
        g.gridy = 1;
        ulpanel.add(l3, g);
        g.gridx = 0;
        g.gridy = 2;
        ulpanel.add(l4, g);

        g.gridwidth = GridBagConstraints.REMAINDER;
        g.fill = GridBagConstraints.HORIZONTAL;
        //      g.anchor = GridBagConstraints.WEST;
        g.weightx = 1;
        g.gridx = 1;
        g.gridy = 0;
        ulpanel.add(tb1, g);
        g.gridwidth = 1;
        g.gridx = 1;
        g.gridy = 1;
        ulpanel.add(tb2, g);
        g.gridx = 3;
        g.gridy = 1;
        ulpanel.add(tb3, g);

        upanel.add(ulpanel);
        upanel.add(urpanel);
        mainpanel.add(upanel);
        add(mainpanel);
    }
}

更新

我看到(在阅读@AndrewThompson 评论后)您可能在谈论大小以及为什么所有东西都被压扁了?正如他所说,JFrame您可以设置大小或调用pack(),这将设置框架的大小以适合其组件。

对于Applet/JApplet但是,您将通过 HTML 启动器更改大小。在 HTML 中找到它并根据需要进行更改:

    <applet width="600" height="600">         <!-- ALTER WIDTH AND HEIGHT HERE -->
        <param name="jnlp_href" value="launch.jnlp"/>
    </applet>

也可能有这个也需要改变:

    <script>
        var attributes = {
            code:       "Test",
            archive:    "TestJApplet.jar",
            width:      600,            <!-- ALTER WIDTH HERE -->
            height:     600             <!-- ALTER HEIGHT HERE -->
        };
        var parameters = {jnlp_href:"launch.jnlp"}; <!-- Applet Parameters -->
        var version = "1.7"; <!-- Required Java Version -->
        deployJava.runApplet(attributes, parameters, version);
    </script>
于 2012-11-19T21:19:43.507 回答