JTabbedPane JLabel, JTextField

倾然丶 夕夏残阳落幕 提交于 2019-11-30 09:47:17

问题


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:

http://i.stack.imgur.com/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 :)


回答1:


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.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/5773874/jtabbedpane-jlabel-jtextfield

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