JTabbedPane JLabel、JTextField
是的,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 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.
使用 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.
假设默认的
JPanel
布局FlowLayout
,给出JTextField
非零数量的列
,并给出JLabel
a < code>JLabel.LEFT 约束。附录:
像这样的东西吗?
Assuming the default
JPanel
layout,FlowLayout
, give theJTextField
a non-zero number ofcolumns
, and give theJLabel
aJLabel.LEFT
constraint.Addendum:
Something like this?