1

我是 Java Applet 编程和一般 Java 的新手,但我非常擅长 C# 和 C++。无论如何,我正在用 JTextField、JButton 和 JLabel 制作一个简单的计算器。但是 JButton 和 JTextField 占据了整个屏幕,最奇怪的是我设置了大小,它没有设置大小。那么我做错了什么?这是下面的代码,所以有人可以帮助我;我将不胜感激任何答案。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.swing.JApplet;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JTextField;
/**
 *
 * @author Danny
 */
public class Applet extends JApplet {

    private JButton button1;
    private JTextField textBox1;
    @Override
    public void paint(Graphics g)
    {
        g.setFont(new Font("Courier", Font.PLAIN, 12));
        g.drawString("Enter a number: ", 36, 36);
        return;
    }

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    @Override
    public void init() {
          button1 = new JButton("Calculate");
          textBox1 = new JTextField("number");
          textBox1.setFont(new Font("Courier", Font.PLAIN, 12));
          textBox1.setSize(100, 36);
          button1.setSize(100, 36);
          textBox1.setLocation(new Point(36, 76));
          button1.setLocation(new Point(36, 70));
          Container c = getContentPane();
          c.add(button1);
          c.add(textBox1);
          c.setBackground(Color.gray);
    }
    // TODO overwrite start(), stop() and destroy() methods
}
4

2 回答 2

4

您需要在此处考虑布局。JApplet 的 contentPane 默认使用 BorderLayout,如果你在 BorderLayout 中添加一个组件而没有第二个参数告诉它在哪里添加它,它将被添加 BorderLayout.CENTER 并尽可能多地填充空间。解决这个问题的关键是尽可能多地了解布局管理器,并利用这些信息来发挥自己的优势。您可以在此处找到此信息:布局管理器的视觉指南

请注意,我们经常嵌套 JPanel,每个 JPanel 都使用自己的对编码人员友好的布局管理器,以实现复杂但易于管理的布局。

例如,

import java.awt.*;
import javax.swing.*;

public class Applet extends JApplet {

   private JButton button1;
   private JTextField textBox1;

   @Override
   public void init() {
      button1 = new JButton("Calculate");
      textBox1 = new JTextField("number");
      textBox1.setFont(new Font("Courier", Font.PLAIN, 12));
      JPanel myPanel = new JPanel(); // JPanel uses FlowLayout by default
      myPanel.add(new JLabel("Enter a number: "));
      myPanel.add(textBox1);
      myPanel.add(button1);
      myPanel.setBackground(Color.gray);

      getContentPane().add(myPanel, BorderLayout.CENTER);
   }

}

另请注意,大多数布局管理器通常会忽略 setSize。相反,preferredSize 通常是受尊重的,但应该避免设置它,因为您希望组件本身根据它们的内容或其他属性(例如 JTextArea 的行和列或 JTextField 的列或字符串文本)设置它们的 preferredSize对于 JLabel)。

于 2011-10-05T20:33:44.563 回答
2

掌握 Swing 类的功能可能非常棘手,所以我感受到了你的痛苦。

您必须考虑到JApplet您正在使用的框架有一个Container使用默认值的框架,LayoutBorderLayout. 在这种情况下,您添加的任何内容都将捕捉到JApplet(您可能在 HTML 中设置)的大小。

尝试以下操作:

public void init() {
      setLayout(new FlowLayout());
      button1 = new JButton("Calculate");
      textBox1 = new JTextField("number");
      textBox1.setFont(new Font("Courier", Font.PLAIN, 12));
      textBox1.setSize(100, 36);
      button1.setSize(100, 36);

      JPanel contentPanel = new JPanel();
      contentPanel.add(button1);
      contentPanel.add(textBox1);

      getContentPane().add(contentPanel );
      c.setBackground(Color.gray);
}

我使用的一个好的经验法则是在将所有Component对象JPanel添加到它们的父容器之前将它们粘贴到它们自己的位置,这避免了一些固有的 Swing 布局怪异。

另请参阅:Swing 布局指南

于 2011-10-05T20:36:20.803 回答