您好,我制作了一个演示 Java 框架,但编译后我尝试运行它,但框架没有打开。我确实编译了它 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()
或 main 方法中调用 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);//<<<
关于java - Demo Frame 已制作但无法使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18032190/