Java JTextField不会显示

时间:2014-04-25 20:31:37

标签: java user-interface

我正在创建一些GUI,我已经创建了这个JDialog,我试图为名称,电话号码和年龄创建模板。 我的问题是当你按下按钮时你会调用这个方法:

public void createKunde(){
        JDialog addDialog = new JDialog(frame);
        JPanel addContentPane =(JPanel) addDialog.getContentPane();
        addContentPane.setBorder(new EmptyBorder(12,12,12,12));

        addContentPane.setLayout(new BorderLayout(6,6));

        addDialog.setTitle("Opret Kunde");
        addDialog.setSize(800, 400);
        addDialog.setLocationRelativeTo(frame);
        addDialog.setModal(true);
        addDialog.setVisible(true);

        JPanel addContent = new JPanel();
        addContent.setLayout(new GridLayout(4,4));

        JTextField addName = new JTextField(50);
        addContent.add(addName);

        JTextField addAge = new JTextField(50);
        addContent.add(addAge);

        JTextField addPhone = new JTextField(50);
        addContent.add(addPhone);


        addContentPane.add(addContent, BorderLayout.WEST);
        addDialog.add(addContentPane);
    }

我无法在我的JDialog中显示TextFields。我无法确定问题应该在哪里?

2 个答案:

答案 0 :(得分:1)

你必须让你的JDialog可见,最后添加它并将其从顶部删除:

addDialog.setVisible(true); 

据我记得java swing,你不能先做一些可见的东西并构建它。

这里有另一个例子:

private void showSimpleDialog() {
    final JDialog d = new JDialog(this, "Run", true);
    d.setSize(200, 150);
    JLabel l = new JLabel("Pagination Swing Demo", JLabel.CENTER);
    d.getContentPane( ).setLayout(new BorderLayout( ));
    d.getContentPane( ).add(l, BorderLayout.CENTER);
    JButton b = new JButton("Run");
    b.addActionListener(new ActionListener( ) {
       public void actionPerformed(ActionEvent ev) {
          createFrame();
          d.setVisible(false);
          d.dispose( );
         }
      });
    JPanel p = new JPanel( );     // Flow layout will center button.
    p.add(b);
    d.getContentPane( ).add(p, BorderLayout.SOUTH);
    d.setLocationRelativeTo(this);
    d.setVisible(true);
}

答案 1 :(得分:0)

您的代码正在尝试将相同的父级添加到容器本身,即jdailog到jdsilog本身

并且在添加组件

后,您的addDialog未设置为可见

最好让它在最后可见

这是您完整修改的工作程序

package com.kb.gui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class JdialogEx1 {
    public static void main(String[] args) {
        createKunde();
    }

    public static void createKunde(){
        Frame frame = new JFrame();
        JDialog addDialog = new JDialog(frame);
        JPanel addContentPane =(JPanel) addDialog.getContentPane();
        addContentPane.setBorder(new EmptyBorder(12,12,12,12));

        addContentPane.setLayout(new BorderLayout(6,6));

        addDialog.setTitle("Opret Kunde");
        addDialog.setSize(800, 400);
        addDialog.setLocationRelativeTo(frame);
        addDialog.setModal(true);


        JPanel addContent = new JPanel();
        addContent.setLayout(new GridLayout(4,4));

        JTextField addName = new JTextField(50);
        addContent.add(addName);

        JTextField addAge = new JTextField(50);
        addContent.add(addAge);

        JTextField addPhone = new JTextField(50);
        addContent.add(addPhone);

       addContentPane.add(addContent, BorderLayout.WEST);
       addDialog.setVisible(true);
    }

}