[英]JTextField not visible in invoked method
I'm trying to populate a Panel with a textfield and a label, the label is being reflecting as expected, however the textfield is not showing up. 我正在尝试使用文本字段和标签填充面板,标签正在按预期方式反映,但是文本字段未显示。 Below is the code that is being used: 下面是正在使用的代码:
package qmutility;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class panetest1
{
public static void main(String[] args)
{
createSubframe();
}
public static void createSubframe()
{
final JFrame subframe = new JFrame("Object Choice");
subframe.setSize(1000, 500);
subframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
subframe.getContentPane().setLayout(new GridLayout(1, 1));
JTextArea out = new JTextArea();
out.setEditable (false);
JScrollPane jp = new JScrollPane(out);
jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel queue = new JPanel();
JLabel lblqname = new JLabel("Please enter the queue name");
JTextField txtqname = new JTextField(20);
queue.add(lblqname, txtqname);
JPanel chl = new JPanel();
tabbedPane.addTab("Queues", queue);
tabbedPane.addTab("Channels", chl);
subframe.getContentPane().add(tabbedPane);
subframe.getContentPane().add(jp);
tabbedPane.setVisible(true);
subframe.setVisible(true);
}
}
Try changing the Layout of your panel. 尝试更改面板的布局。
Like, queue.setLayout(new FlowLayout());
就像queue.setLayout(new FlowLayout());
or Add components to panel one by one, 或将组件一一添加到面板中,
queue.add(lblqname);
queue.add(txtqname);
The method Container.add(Component comp,Object constraints) adds a given Component
with given constraints , it is not meant to add two Component
at once. 方法Container.add(Component comp,Object Constraints)添加具有给定约束的给定Component
,这并不意味着一次添加两个Component
。
Replace : 更换:
queue.add(lblqname, txtqname);
With 同
queue.add(lblqname);
queue.add(txtqname);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.