【问题标题】:My JTextField doesn't appear我的 JTextField 没有出现
【发布时间】:2014-08-28 08:21:01
【问题描述】:

这是我的java代码:

JLabel persAdi,persSoyadi,persKodu,ust,alt;
JTextField ad,soyad,tckimlikno;
JButton bul,iptal;

    public persBul(){
        setSize (400,600);
        setResizable(false);
        setLocation (20, 20);
        setVisible(true);

        Container icerik = getContentPane();
        icerik.setLayout(null);
        icerik.getX();

        ust=new JLabel("Bulmak İstediğiniz Personelin;");
        ust.setBounds(10, 10, 200, 30);
        icerik.add(ust);

        persAdi=new JLabel("Adı:");
        persAdi.setBounds(10,40,80,15);
        icerik.add(persAdi);

        ad=new JTextField();
        ad.setBounds(50, 40, 80, 20);
        icerik.add(ad);


        persSoyadi=new JLabel("Soyadı:");
        persSoyadi.setBounds(10, 180, 80, 30);
        icerik.add(persSoyadi);

        soyad=new JTextField();
        soyad.setBounds(200, 40, 100, 30);

        Icon bulPng=new ImageIcon(getClass().getResource("search.png"));
        Icon iptalPng=new ImageIcon(getClass().getResource("cancel.png"));

        bul=new JButton("",bulPng);
        bul.setBounds(20, 120, 40, 40);
        icerik.add(bul);
        iptal=new JButton("",iptalPng);
        iptal.setBounds(90, 120, 40, 40);
        icerik.add(iptal);

    }

public static void main(String[] args){
    persBul app=new persBul();
}

当我调试这段代码时,我的JTextField 没有出现。只有第一个JLabel 可以出现,我看不到任何其他JLabelJTextFieldJButton。当我的光标在它上面时,我的按钮就会出现。我必须做这个项目,但我还没有创建用户界面。有人可以帮帮我吗?

【问题讨论】:

标签: java swing layout layout-manager null-layout-manager


【解决方案1】:

您应该避免使用null 布局。

您可以使用layout 管理器实现相同(或近似)的用户界面。正如我在this comment 中指出的那样。

这是MCVE,我建议您在进一步的问题中这样做。也请查看How to ask guide,以便提出更好的问题。

我创建了一个新课程,因为尝试修改你的课程比这更费力。复制粘贴它并了解它的工作原理,然后将其调整到您自己的课程中。

编辑

您可以添加separators 来添加空格。同样正如@Andrew Thompson 在他的评论中所说,您可以查看How to use multiple layout managers(我在示例中做了类似的事情)。以下是How to add spaces in swing 上的其他选项。也许GridBag Layout 看起来更像null 布局。

这是关于布局的guide(链接也由 Andrew 提供)。

阅读您的问题后:

Can I use setBounds(x,y,width,height); function with this code? Or What can I do for using this function (setBounds(x,y,width,height);)? Because I want to determine my own self x,y points of the JLabel,JTextField,JButton

你应该不惜一切代价避免使用setBounds(x,y,width,height);,因为,正如 Andrew 解释了原因一样:

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components.

使用null 布局可能会在执行程序时带来一些意外错误。使用 null 布局很好(我的意思是,setBounds(x,y,width,height);当且仅当你是一个摇摆不定的新手,但尽可能尝试开始使用布局管理器而不是null 布局。

我想说的是:将null 布局仅用于教育目的并没有错,但即使它更大,有时更复杂并且需要更多思考,它更好使用它们以避免意外错误。当您是学生时使用它,但如果用于专业程序,请避免使用它。

这是this answer的输出,所以你可以看到swing中空格的使用情况。

import javax.swing.*;
import java.awt.*;
class example {
    JFrame frame;
    JPanel panel, hPanel1, hPanel2;
    JLabel label1, label2;
    JTextField field1, field2;
    example() {
        frame = new JFrame("example");

        panel = new JPanel();
        hPanel1 = new JPanel();
        hPanel2 = new JPanel();

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        hPanel1.setLayout(new FlowLayout());
        hPanel2.setLayout(new FlowLayout());

        label1 = new JLabel("Label 1");
        label2 = new JLabel("Label 2");
        field1 = new JTextField();
        field2 = new JTextField();

        field1.setColumns(6);
        field2.setColumns(6);

        hPanel1.add(label1);
        hPanel1.add(field1);

        hPanel2.add(label2);
        hPanel2.add(field2);

        panel.add(hPanel1);
        panel.add(hPanel2);

        frame.add(panel);

        frame.setSize(400,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new example();
            }
        });
    }
}

【讨论】:

  • 非常感谢。现在问题已经解决了。但是我会问一个问题。我可以使用 setBounds(x,y,width,height);使用此代码功能?或者我可以做什么来使用这个函数 (setBounds(x,y,width,height);)?因为我想确定自己的JLabel,JTextField,JButton的x,y点
  • @umtklc 实际上这就是我一直在谈论的空布局。你应该不惜一切代价避免它,如果这个程序是用于教育目的并且你还在学习 Java Swing,那么你可以使用 null 布局,否则,BoxLayout 有一个 addSeparator 方法,它会让你在组件之间留出或多或少的空间。一旦我在 PC 上,我将添加文档链接。请阅读我发布的布局管理器,因为 Swing 旨在使用它们而不是空布局(即 setBounds(...))
  • @umtklc 请检查编辑,如果您有任何疑问,请询问,我会尽快回答并清楚。
  • 感谢所有答案 :) 有点晚了 :) 抱歉
  • 是的,没关系...希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多