簡體   English   中英

演示框架已制成,但沒有起作用

[英]Demo Frame made but didnt work

嗨,我做了一個演示Java框架,但編譯后我嘗試運行它,但框架沒有打開。 我沒有編譯它n沒有錯誤有人可以幫我這是基本代碼,請仔細研究n幫助我很多

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

class DemoFrame extends JFrame
{
        JPanel pmain,pmaster;
        JLabel lname,lclas,lyear,lroll,lemail,lphn;
        JTextField name,clas,year,roll,email,phn;
        JButton submit,reset;
        JPasswordField pass;

        void demoFrame()
        {
                setTitle("Registration Form");
                setSize(700,700);

                lname=new JLabel("Name");
                lclas=new JLabel("clas");
                lyear=new JLabel("Year");
                lemail=new JLabel("Email");
                lphn=new JLabel("Phone");
                lroll=new JLabel("Roll No.");

                name=new JTextField(10);
                clas=new JTextField(10);
                year=new JTextField(10);
                email=new JTextField(10);
                phn=new JTextField(10);
                roll=new JTextField(10);

                submit=new JButton("Register Now");
                reset=new JButton("Reset");

                pmain=new JPanel(new GridLayout(5,2,3,3));
                        pmain.add(lname);
                        pmain.add(name);

                        pmain.add(lclas);
                        pmain.add(clas);

                        pmain.add(lroll);
                        pmain.add(roll);

                        pmain.add(lemail);
                        pmain.add(email);

                        pmain.add(lphn);
                        pmain.add(phn);

                pmaster=new JPanel();
                        pmaster.add(pmain);
        }

        public static void main(String args[])
        {
                DemoFrame obj=new DemoFrame();
                obj.demoFrame();

        }
}

demoFrame()或主要方法內部,將setVisible()調用到框架:

public static void main(String args[])
{
    DemoFrame obj=new DemoFrame();
    obj.demoFrame();
    obj.setVisible(true)
}

另外我注意到您沒有將pmaster添加到框架中,請添加以下行:

  pmaster.add(pmain);
  add(pmaster,BorderLayout.CENTER);// PAGE_START , PAGE_END, LINE_START, LINE_END Also can be used

另一件事,也添加此行:

setTitle("Registration Form");
setSize(700,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//<<<

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM