JApplet中添加JButton控件,JButton填满整个屏幕

发布于 2024-12-08 06:00:31 字数 1428 浏览 1 评论 0原文

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

I am new to Java Applet programming and Java in general but I am very good in C# and C++. Anyway I am making a simple calculator with a JTextField, JButton and JLabel. But the JButton and JTextField take up the whole screen and the strangest thing is I set the size and it is not setting the size. So what am I doing wrong? Here is the code below so somebody can please help me; I will appreciate any answers.

/*
 * 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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁对谁错谁最难过 2024-12-15 06:00:31

您需要在这里考虑布局。 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)。

You need to take layouts into consideration here. A JApplet's contentPane uses BorderLayout by default, and if you add a component to BorderLayout without a second parameter telling it where to add it, it will be added BorderLayout.CENTER and will fill as much space as possible. The key to solving this is to learn as much as possible about layout managers and use this information to your advantage. You can find this information here: Visual Guide to the Layout Managers.

Note that often we nest JPanels, each using its own coder-friendly layout manager, in order to achieve complex but easy to manage layouts.

e.g.,

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);
   }

}

Note also that setSize is usually ignored by most layout managers. Instead preferredSize is usually honored, but setting this should be avoided as instead you'll want the components themselves to set their preferredSize based on their contents or other properties (such as rows and columns for a JTextArea or columns for a JTextField, or String text for a JLabel).

随心而道 2024-12-15 06:00:31

掌握 Swing 类的功能可能非常棘手,所以我能理解您的痛苦。

您必须考虑到您正在使用的 JApplet 框架有一个使用默认 LayoutContainer,即 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 布局指南

Getting the hang of how the Swing classes function can be pretty tricky, so I feel your pain.

You have to consider that the JApplet frame that you're using has a Container that is using the default Layout, which is BorderLayout. In this case, any content you add will snap to the size of the JApplet (which you are likely setting in HTML).

Try the following:

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);
}

A good rule of thumb I use is to stick all Component objects into their own JPanel before adding them to their parent container, which avoids some inherent Swing layout weirdness.

Also, see: Swing Layout Guide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文