gpt4 book ai didi

Java Applet 大小行为与 JFrame 大小行为

转载 作者:行者123 更新时间:2023-12-04 05:23:27 25 4
gpt4 key购买 nike

enter image description here
*

(Legend) 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 的外部“ shell ”采用该 shell ,而不是我添加到小程序上的内部面板。

最佳答案

+1 为 SSCCE

1)我不会将 Swing 组件与 AWT 混合使用 Applet宁可使用 JApplet相反。

public class Test2 extends JApplet {

2) 使用 SwingUtilities.invokeAndWait()块以覆盖您的 UI 创建 init() see here .

像这样(省略了异常处理)。
@Override
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
//create UI here
}
});
}

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

enter image description 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>

关于Java Applet 大小行为与 JFrame 大小行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13461993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com