JTabbedPane JLabel, JTextField

…衆ロ難τιáo~ 提交于 2019-11-29 17:18:24

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.

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

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?

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);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!