【问题标题】:Added JButton control to JApplet and the JButton fills the whole screen为 JApplet 添加 JButton 控件,JButton 填满整个屏幕
【发布时间】:2011-10-05 20:24:26
【问题描述】:

我是 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
}

【问题讨论】:

    标签: java swing layout-manager japplet


    【解决方案1】:

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

    请注意,我们经常嵌套 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)。

    【讨论】:

      【解决方案2】:

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

      您必须考虑到您正在使用的JApplet 框架有一个Container,它使用默认的Layout,即BorderLayout。在这种情况下,您添加的任何内容都将捕捉到 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 Layout Guide

      【讨论】:

      • 抱歉,我不同意你的“经验法则”。通常将组件放在自己的 JPanel 中并不是一个好主意。更好的是了解布局管理器并利用这些知识正确使用它们。
      • 即使使用正确的布局过程,如果您尝试将“原始”组件添加到容器中,Swing 也会做一些奇怪的事情。我并不是说将每个组件都放在一个单独的 JPanel 中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多