*
(图例) 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 上的内部面板。