在JFrame表单中显示JPanel [英] Displaying a JPanel within JFrame Form

查看:132
本文介绍了在JFrame表单中显示JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下JPanel,我希望在JFrame表单中显示该JPanel。



JPanel



I have created the following JPanel and I want to display that JPanel within a JFrame form.

JPanel

  import javax.swing.*;

public class JPanelFormInJFrameForm extends JPanel{

    /**
     * @param args the command line arguments
     */
    
        public JPanelFormInJFrameForm(){
        
        setLayout(null);
        JLabel lStudNo = new JLabel("Student No:");
        JLabel lStudName = new JLabel("Student Name:");
        JLabel lMaths = new JLabel("Maths:");
        JLabel lScience = new JLabel("Science:");
        JLabel lEnglish = new JLabel("English:");
        
        JTextField tStudNo = new JTextField();
        JTextField tStudName = new JTextField();
        JTextField tMaths = new JTextField();
        JTextField tScience = new JTextField();
        JTextField tEnglish = new JTextField();
        
        lStudNo.setBounds(50, 40, 80, 25);
        lStudName.setBounds(50, 80, 110, 25);
        lMaths.setBounds(50, 120, 80, 25);
        lScience.setBounds(50, 160, 80, 25);
        lEnglish.setBounds(50, 200, 80, 25);
        
        tStudNo.setBounds(145, 40, 110, 30);
        tStudName.setBounds(145, 80, 110, 30);
        tMaths.setBounds(145, 120, 110, 30);
        tScience.setBounds(145, 160, 110,30);
        tEnglish.setBounds(145, 200, 110, 30);
        
        add(lStudNo);
        add(lStudName);
        add(lMaths);
        add(lScience);
        add(lEnglish);
        
        add(tStudNo);
        add(tStudName);
        add(tMaths);
        add(tScience);
        add(tEnglish);
     /*   
        setTitle("Java Swing Null Layout");
        setSize(300, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);   
        
     */   
        }
        
    public static void main(String[] args) {
        // TODO code application logic here
        
        JFrame Frame = new JFrame();
        Frame.getContentPane().add(new JPanelFormInJFrameForm());
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame.setSize(300, 300);
    }
}





在main()我尝试了但是没有用。



我无法弄清楚问题所以如果你能有人请帮助我。



谢谢!



Chiranthaka



At the main() I have tried that but it didn't worked.

I cannot figure out the problem so that if you could someone please help me.

Thank You!

Chiranthaka

推荐答案

- 不要在主要工作

- 做不要使用像Frame这样的关键字作为变量。

- 不要用大写字母开始变量名称 - 大写字母是对象名称,绝不是变量。



请将此链接加入书签: http://docs.oracle.com/javase/tutorial /uiswing/components/frame.html [ ^ ]



- Do NOT work in main
- Do NOT use keywords like "Frame" for variables.
- Do NOT start variable names with upper case - uppercase is an objects name, never a variable.

Please bookmark this link: http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html[^]

public class MyForm extends JFrame{
     
        public MyForm(){
        
        this.init();
       
        }

        private void init(){
        // put code here and in other methods - not constructor
        }
        
    public static void main(String[] args) {
        // TODO code application logic here
        
        JFrame myFrame = new MyForm();
        myFrame.getContentPane().add(new JPanelFormInJFrameForm());
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setSize(300, 300);
    }
}


你做了所有事情,但忘了显示框架。

You did everything but forget to display the frame.
frame.setVisible(true);





该行应该在最后。



And the line should come at the end.


这篇关于在JFrame表单中显示JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆