*
(图例)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);
}
}
我试图在HTML中嵌入Applet并在那里指定大小,但是,只有Applet的外部'shell'才能获取该shell,而不是我添加到applet中的内部Panel。
答案 0 :(得分:3)
+1 SSCCE
1)我不会将Swing组件与AWT Applet
混合而是使用JApplet
。
public class Test2 extends JApplet {
2)使用SwingUtilities.invokeAndWait()
块在覆盖的init()
see here中包装您的UI创建。
像这样(省略了异常处理)。
@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>