本文介绍了在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

推荐答案

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);
    }
}


frame.setVisible(true);





该行应该在最后。



And the line should come at the end.


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

05-28 16:30
查看更多