JTabbedPane JLabel、JTextField

发布于 2024-11-03 07:17:36 字数 1201 浏览 2 评论 0原文

是的,我有一个 JTabbedPane,它有一个包含 JLabel 和 JTextField 的 JPanel。

我的代码

JTabbed Pane 声明:

        this.tabPane = new JTabbedPane();
    this.tabPane.setSize(750, 50);
    this.tabPane.setLocation(10, 10);
        tabPane.setSize(750,450);
    tabPane.add("ControlPanel",controlPanel);

文本字段声明:

    this.channelTxtFld = new JTextField("");
    this.channelTxtFld.setFont(this.indentedFont);
    this.channelTxtFld.setSize(200, 30);
    this.channelTxtFld.setLocation(200, 10);

JLabel: this.channelLabel = new JLabel("频道名称:"); this.channelLabel.setSize(150, 30); this.channelLabel.setLocation(10,10);

private void createPanels() {
    controlPanel = new JPanel();
    controlPanel.setSize(650,500);
}
 private void fillPanels() {
    controlPanel.add(channelLabel);
    controlPanel.add(channelTxtFld);

}

所以我的计划是,有一个带有 JPanel 的选项卡式窗格,其中我在固定位置上有一些标签、文本字段和按钮,但执行此操作后,这是我的结果:

https://i.sstatic.net/vXa68.png

我想要的是 JLabel 和它旁边左侧有一个完整的 JTextfield一边不在中间。

有人知道我的错误是什么吗?

谢谢 :)

Right, I have a JTabbedPane that has a JPanel that contains a JLabel and a JTextField.

my code

JTabbed Pane declaration :

        this.tabPane = new JTabbedPane();
    this.tabPane.setSize(750, 50);
    this.tabPane.setLocation(10, 10);
        tabPane.setSize(750,450);
    tabPane.add("ControlPanel",controlPanel);

textfield declaration :

    this.channelTxtFld = new JTextField("");
    this.channelTxtFld.setFont(this.indentedFont);
    this.channelTxtFld.setSize(200, 30);
    this.channelTxtFld.setLocation(200, 10);

JLabel :
this.channelLabel = new JLabel("Channel name : ");
this.channelLabel.setSize(150, 30);
this.channelLabel.setLocation(10,10);

private void createPanels() {
    controlPanel = new JPanel();
    controlPanel.setSize(650,500);
}
 private void fillPanels() {
    controlPanel.add(channelLabel);
    controlPanel.add(channelTxtFld);

}

So what my plan is, was to have a tabbed pane that has a JPanel where I have some Labels, textfields and buttons on fixed positions, but after doing this this is my result:

https://i.sstatic.net/vXa68.png

What I wanted was that I had the JLabel and next to it a full grown JTextfield on the left side not in the middle.

Anyone any idea what my mistake is ?

thank you :)

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

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

发布评论

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

评论(3

忆离笙 2024-11-10 07:17:36

您的 controlPanel 使用哪种布局管理器,您可能需要 BorderLayout,将 Label 放在西边,将 TextField 放在中间。

顺便说一句,设置各种组件的大小和位置没有意义,除非您使用空布局,这不是一个好主意。所以我会删除所有这些东西并让布局管理器为您完成。

What kind of Layout Manager are you using for your controlPanel, you probably want BorderLayout, putting the Label in the West, and the TextField in the center.

BTW, setting the size and position of various components doesn't make sense unless you are using a Null Layout, which isn't a good idea. So i'd remove all that stuff and let the Layout Manager do it for you.

几度春秋 2024-11-10 07:17:36

使用 LayoutManager 并考虑 setPreferredSize、setMinimumSize 方法、 setMaximumSize 根据您想要的效果调整组件边界。

Use a LayoutManager and consider also the methods setPreferredSize, setMinimumSize, setMaximumSize to adjust components bounds according on which is your desired effect.

桃扇骨 2024-11-10 07:17:36

假设默认的 JPanel 布局 FlowLayout,给出 JTextField 非零数量的,并给出 JLabel a < code>JLabel.LEFT 约束。

附录:

一个完整的JTextField

像这样的东西吗?

在此处输入图像描述

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

/**
 * @see http://stackoverflow.com/questions/5773874
 */
public class JTabbedText {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            private final JTabbedPane jtp = new JTabbedPane();

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                jtp.setPreferredSize(new Dimension(400, 200));
                jtp.addTab("Control", new MyPanel("Channel"));

                f.add(jtp, BorderLayout.CENTER);
                f.pack();
                f.setVisible(true);
            }
        });
    }

    private static class MyPanel extends JPanel {

        private final JLabel label = new JLabel("", JLabel.LEFT);
        private final JTextField text = new JTextField();

        public MyPanel(String name) {
            this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
            label.setText(name);
            label.setAlignmentY(JLabel.TOP_ALIGNMENT);
            text.setAlignmentY(JTextField.TOP_ALIGNMENT);
            this.add(label);
            this.add(text);
        }
    }
}

Assuming the default JPanel layout, FlowLayout, give the JTextField a non-zero number of columns, and give the JLabel a JLabel.LEFT constraint.

Addendum:

a full grown JTextField

Something like this?

enter image description here

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

/**
 * @see http://stackoverflow.com/questions/5773874
 */
public class JTabbedText {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            private final JTabbedPane jtp = new JTabbedPane();

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                jtp.setPreferredSize(new Dimension(400, 200));
                jtp.addTab("Control", new MyPanel("Channel"));

                f.add(jtp, BorderLayout.CENTER);
                f.pack();
                f.setVisible(true);
            }
        });
    }

    private static class MyPanel extends JPanel {

        private final JLabel label = new JLabel("", JLabel.LEFT);
        private final JTextField text = new JTextField();

        public MyPanel(String name) {
            this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
            label.setText(name);
            label.setAlignmentY(JLabel.TOP_ALIGNMENT);
            text.setAlignmentY(JTextField.TOP_ALIGNMENT);
            this.add(label);
            this.add(text);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文