Applet 面板、一种固定尺寸和动态 JTextField

发布于 2024-09-02 14:02:07 字数 1216 浏览 1 评论 0 原文

我需要一个包含一个面板的小程序。面板需要为 550x400 像素,JTextField 需要低于面板动态大小。我希望它是这样的: [顶部面板] [textPanel]

但是我正在尝试这个,看起来面板填满了所有空间。代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Client extends JApplet
{

@Override
public void init()
{
    try {
        java.awt.EventQueue.invokeAndWait(new Runnable()
            {

            public void run()
            {
                initComponents();
            }
            });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

private void initComponents()
{
    JPanel topPanel = new javax.swing.JPanel();

    topPanel.setBackground(Color.red);

    topPanel.setSize(550, 400);
    topPanel.setPreferredSize(new Dimension(550, 400));
    topPanel.setMinimumSize(new Dimension(550, 400));
    topPanel.setMaximumSize(new Dimension(550, 400));

    JTextField myTextBox = new JTextField(255);

    getContentPane().add(topPanel, java.awt.BorderLayout.NORTH);
    getContentPane().add(myTextBox, java.awt.BorderLayout.SOUTH);
}
// TODO overwrite start(), stop() and destroy() methods
}

谢谢!

I need an applet which contains one panel. The panel needs to be 550x400 pixels, the JTextField needs to be under the panel dynamic size. I want it to be like this:
[topPanel]
[textPanel]

However I am trying this, and it seems like the panel is filling all the space. The code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Client extends JApplet
{

@Override
public void init()
{
    try {
        java.awt.EventQueue.invokeAndWait(new Runnable()
            {

            public void run()
            {
                initComponents();
            }
            });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

private void initComponents()
{
    JPanel topPanel = new javax.swing.JPanel();

    topPanel.setBackground(Color.red);

    topPanel.setSize(550, 400);
    topPanel.setPreferredSize(new Dimension(550, 400));
    topPanel.setMinimumSize(new Dimension(550, 400));
    topPanel.setMaximumSize(new Dimension(550, 400));

    JTextField myTextBox = new JTextField(255);

    getContentPane().add(topPanel, java.awt.BorderLayout.NORTH);
    getContentPane().add(myTextBox, java.awt.BorderLayout.SOUTH);
}
// TODO overwrite start(), stop() and destroy() methods
}

Thanks!

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

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

发布评论

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

评论(2

戏剧牡丹亭 2024-09-09 14:02:07

当我测试上面的代码时,这些组件似乎处于正确的位置。我唯一注意到的是初始视口尺寸小于 550x400。由于 JPanel 的大小始终为 550x400,因此这会导致显示 JTextField 时出现一些问题。

The components seemed to be in the correct positions when I tested the above code. The only thing I noticed was that the initial view-port size was smaller than 550x400. This caused some artifacts in displaying the JTextField since the size of the JPanel is invariably 550x400.

原来分手还会想你 2024-09-09 14:02:07

JPanel 似乎是正确的(使用 setPreferredSize,添加到 NORTH)假设您的小程序宽度为 550,高度至少为 400。由于 BorderLayout 拉伸其组件的方式,我可能会尝试将文本字段移动到 CENTER 而不是 SOUTH。根据 Javadoc:

组件根据其首选尺寸和容器尺寸的限制进行布局。 NORTH 和 SOUTH 分量可以水平拉伸; EAST 和 WEST 组件可以垂直拉伸; CENTER 组件可以水平和垂直拉伸以填充剩余的空间。

因此,将文本字段放在中间应该可以使其垂直拉伸,从而使其能够占用小程序的其余可用空间。

The JPanel seems correct (using setPreferredSize, adding to NORTH) assuming the your applet is 550 wide and at least 400 high. I might try moving the text field to CENTER instead of SOUTH because of how BorderLayout stretches its components. According to the Javadocs:

The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.

So putting the text field into the center should give it the vertical stretch you are looking for to enable it to take up the rest of the applet's available space.

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