JTextField在JWindow中无法使用

TU_*_*KAI 5 java swing jtextfield

我有一个Jwindow,当我添加一个Jtextfield时,文本字段变得无法编辑.

JWindow window = new JWindow();
window.setBounds(400, 100, 700,500);
window.setVisible(true);
window.setLayout(null);
JTextField text = new JTextField();
text.setBounds(300, 300, 150, 30);
text.setEditable(true);       
window.getContentPane().add(text);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用Jframe作为Jwindow的所有者时,文本字段现在可以编辑,但框架与jwindow一起显示:

JFrame frame = new JFrame();
frame.setVisible(true);
JWindow window = new JWindow();
window.setBounds(400, 100, 700,500);
window.setVisible(true);
window.setLayout(null);
JTextField text = new JTextField();
text.setBounds(300, 300, 150, 30);
text.setEditable(true);       
window.getContentPane().add(text);
Run Code Online (Sandbox Code Playgroud)

所以,我有两个问题:

  1. 为什么JTextField在JWindow中是不可编辑的,我怎么能让它可编辑?
  2. 使用JFrame作为JWindow边框的主要目的是什么?

mKo*_*bel 5

编辑,

  • JWindow只有在屏幕上显示其父级时才能访问内容

  • 对于可编辑和可访问的内容使用un_decorated JDialog代替JWindow,jDialog不会导致不可访问的内容,

  • 为什么...,我无法解释,没有找不到原因,在这一刻没有办法,API没有告诉我什么导致可访问,可编辑...

...

1. Why JTextField is uneditable in JWindow and how could i let it able to edit?
Run Code Online (Sandbox Code Playgroud)

真的不知道

import java.awt.*;
import javax.swing.*;

public class WindowTest {

    private JFrame frame;

    public JPanel createContentPane() {
        JTextField text = new JTextField("Whatewer");        
        JPanel panel = new JPanel();
        panel.add(text);
        createAndShowWindow();
        return panel;
    }

    void createAndShowGUI() {
        frame = new JFrame("Window Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(createContentPane());
        frame.setLocation(50, 50);
        frame.pack();
        frame.setVisible(true);
    }

    private void createAndShowWindow() {
        JTextField text = new JTextField("Whatewer");
        JWindow win = new JWindow(frame);
        win.setLayout(new GridLayout(0, 1));
        win.add(text);
        win.pack();
        win.setLocation(150, 50);
        win.setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                new WindowTest().createAndShowGUI();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

Yes, both are editable, and i wannt only JWindow to be displayed. Thanks!! 
Run Code Online (Sandbox Code Playgroud)
  • 默认情况下,JWindow需要JFrame才能获得正确的解决方法

  • 没有人告诉我这个JFrame必须是可见的(对GUI有效),然后从frame.setDefaultClose....中删除这些代码行,包括frame.setVisible(true);我的例子

  • 这种形式目前的JVM实例从来没有从RAM消失了,直到你的电脑重新启动或关闭开关等,你必须添加分离退出JButton与代码行System.exit(0)ActionListener