java 无法将 JTextField 添加到 JFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15172831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:43:09  来源:igfitidea点击:

Not able to add JTextField to JFrame

javaswingjframejtextfieldlayout-manager

提问by Sam

I am not able to add JTextFieldto JFrame. My JFramecontains a JLabeland a JTextField. First, i have added the JLabel, and it is working. Here is the code.

我无法添加JTextFieldJFrame. 我的JFrame包含 aJLabel和 a JTextField。首先,我添加了JLabel,并且它正在工作。这是代码。

private static void createandshowGUI()
     {

    JFrame frame =new JFrame("HelloSwing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setBackground(Color.red);
    frame.setSize(200,200);

    JLabel label=new JLabel("New To Java!!");
    frame.getContentPane().add(label);
    frame.setVisible(true);
}
public static void main(String[] args) {
    createandshowGUI();}   //and it shows the output like below .

Frame having JLabel

具有 JLabel 的框架

Then i added JTextField .

然后我添加了 JTextField 。

    JLabel label=new JLabel("New To Java!!");
    frame.getContentPane().add(label);

   JTextField jtf=new JTextField();
   frame.getContentPane().add(jtf);

    frame.setVisible(true);

But then it shows output like this.

但随后它显示了这样的输出。

Frame having JLabel and JTextField

具有 JLabel 和 JTextField 的框架

Please somebody help me on this issue.Can i add more than one component to JFrame?As i am new to Java, i am having a confusion between frame,ContentPane and Layouts.

请有人帮助我解决这个问题。我可以向 JFrame 添加多个组件吗?由于我是 Java 新手,我对框架、内容窗格和布局之间存在混淆。

采纳答案by Daniel Rikowski

Actually you are successful in adding the JTextField. The problem you are experiencing stems from the layout manager which stretches it across the whole frame.

实际上,您已成功添加JTextField. 您遇到的问题源于布局管理器,它将它延伸到整个框架。

The content pane of JFrameuses a BorderLayoutmanager by default. (See How to Use BorderLayout)

默认情况下,内容窗格JFrame使用BorderLayout管理器。(请参阅如何使用 BorderLayout

In my application a always ended up using the MigLayoutmanager, but first you might want to familiarize yourself with layout managers in general. (See A Visual Guide to Layout Managers)

在我的应用程序中,a 总是最终使用MigLayout管理器,但首先您可能需要熟悉一般的布局管理器。(请参阅布局管理器可视指南

回答by mgibsonbr

Your ContentPanehas a BorderLayoutby default, which only accepts a single element (in the default position - the center). After you added a second element (the JTextField), it replaced the last one (the JLabel).

默认情况下,您ContentPane有一个BorderLayout,它只接受一个元素(在默认位置 - 中心)。添加第二个元素 (the JTextField) 后,它替换了最后一个 (the JLabel)。

When you add elements to a Containerusing the single-argument add, you're not specifying in which position you want them, so the layout manager chooses a position at will. OTOH if you specify constraints (using the overloaded add) then you have more control over where the element will be placed. Check the docs for each layout manager to see how they work and which constraints it supports.

当您Container使用单参数将元素添加到 a 时add,您没有指定您想要它们的位置,因此布局管理器会随意选择一个位置。OTOH 如果您指定约束(使用重载 add),那么您可以更好地控制元素的放置位置。检查每个布局管理器的文档以了解它们是如何工作的以及它支持哪些约束。

For your current situation, you can use an intermediate JPanel(which has FlowLayoutas its default LayoutManager- the simplest IMHO for who is still learning) instead of adding elements directly to the content pane, or simply change its layout to something else.

对于您当前的情况,您可以使用中间体JPanelFlowLayout默认情况下LayoutManager- 对于仍在学习的人来说最简单的恕我直言),而不是直接将元素添加到内容窗格,或者只是将其布局更改为其他内容。

回答by stommestack

A JFramecan only have one component (unless you are using that it has a BorderLayout). The solution is to use a JPanel. You add the objects to a JPanel and then add the JPanelto the JFrame. You need to add import javax.swing.JPanel;too. You can do it something like this:

AJFrame只能有一个组件(除非您使用它有一个BorderLayout)。解决方案是使用JPanel. 你的对象添加到JPanel然后添加JPanelJFrame。你也需要添加import javax.swing.JPanel;。你可以这样做:

private static void createandshowGUI()
{
    JFrame frame =new JFrame("HelloSwing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setBackground(Color.red);
    frame.setSize(200,200);

    JPanel panel = new JPanel(); //Create a JPanel

    JLabel label=new JLabel("New To Java!!");
    panel.add(label); // Add the label to the panel

    JTextField jtf = new JTextField();
    panel.add(jtf); // Add the JTextField to the panel

    frame.getContentPane().add(panel); // Add the panel to the JFrame
    frame.setVisible(true);
}
public static void main(String[] args) {
    createandshowGUI();}

This should work.

这应该有效。