2016-03-02 122 views
0

我想一个简单的文本框添加到我的班JDialog类型,但它似乎没有工作,我不知道我做错了:添加文本框窗格

Container pane = getContentPane();  

    JPanel panel = new JPanel(); 
    pane.add(panel); 
    JTextField userText = new JTextField(20); 
    userText.setBounds(100, 10, 160, 25); 
    pane.setVisible(true); 

    panel.add(userText); 
    panel.setLayout(null); 
+0

。利用适当的布局管理器 – MadProgrammer

+1

*“我不知道我做错了:” * - 考虑提供[可运行示例](https://stackoverflow.com/help/mcve)这表明你的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这会减少混淆和更好的反应 – MadProgrammer

+0

你能分享完整的代码吗? –

回答

0

由于您的实际的代码不可用,试图复制一个类似的代码。 尝试将JFrame引用传递给超类,并使用setSize和setVisible方法来指定大小并使对话框可见。尝试此操作并检查它是否可用。

class SampleDialog extends JDialog { 
/** 
* The constructor creates the window with all the controls. 
* Constructor takes a JFrame object 
*/ 
SampleDialog(JFrame jframe) { 
    //pass the parameters to the superclass(JDialog) 
    super(jframe,true); 
    Container pane = getContentPane();  
    JPanel panel = new JPanel(); 
    pane.add(panel); 
    JTextField userText = new JTextField(20); 
    userText.setBounds(100, 10, 160, 25); 
    panel.add(userText); 
    WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED); 
    this.addWindowListener(new WindowAdapter() { 
     public void windowClosing(WindowEvent we) { 
      System.exit(0); 
     } 
    }); 
    setSize(200, 200); 
    setVisible(true);  
} 
public static void main(String[] args) throws IOException { 
    SampleDialog sampleDialog = new SampleDialog(new JFrame()); 
}} 
+0

为您做了这个工作吗? –