【问题标题】:JTextField / JTextArea are not showingJTextField / JTextArea 未显示
【发布时间】:2017-01-18 15:47:45
【问题描述】:

当我启动 JTextField 和或 JTextArea 时,它们没有显示。仅显示 JButton 和 JLabel。

我错过了什么?请给我一些解决方案。

我正在使用JFrame 和JPanel。

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

public class Login extends JFrame{

    JFrame window_login =new JFrame();

    public Login(){
        //creating frame
        window_login.setVisible(true);
        window_login.setSize(500, 500);
        window_login.setTitle("Login Screen");
        window_login.setLocationRelativeTo(null);
        window_login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createView();
    }
    private void createView(){
        //create panel
        JPanel top_title = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        //JLabel b1=new JLabel("Submit");
        JButton b2=new JButton("Reset");
        JTextField b1=new JTextField("text",10);

        c.insets = new Insets(5,5,5,5);
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        top_title.add(b1, c);

        c.gridx = 1;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LAST_LINE_START;
        top_title.add(b2, c);

        window_login.add(top_title);
    }

    public static void main(String[] args) {
        new Login();
    }
}**`strong text`**

【问题讨论】:

  • 请在构造函数中创建window_login.setVisible(true);。

标签: java swing awt jtextfield jtextarea


【解决方案1】:

试试这个编辑:

    public Login(){
        //creating frame

        window_login.setSize(500, 500);
        window_login.setTitle("Login Screen");
        window_login.setLocationRelativeTo(null);
        window_login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createView();
        window_login.setVisible(true);//this statement has been move to last in the constructor.
    }

【讨论】:

  • 您必须通过勾选正确或最佳答案来批准答案。或者甚至给我投票。这样做,您甚至可以因接受答案而获得更多声誉。
  • OP 没有足够的代表。投票(向上或向下)。
  • 至少,如果它对他有用,如果他真的相信答案,他可以标记为正确或最佳答案。
猜你喜欢
  • 1970-01-01
  • 2023-01-13
  • 2013-04-04
  • 1970-01-01
  • 2018-11-14
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多