JPanel面板

package jframe;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.*;

public class jframe extends JFrame{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public jframe(){
        this.setTitle("test");
        Container con = this.getContentPane();
        JPanel p1 = new JPanel(new GridLayout(1,2,5,5));                        //实例化两个JPanel面板
        JPanel p2 = new JPanel(new GridLayout(2,1,5,5));
        p1.add(new JButton("1"));                                                //加入按钮
        p1.add(new JButton("1"));
        p2.add(new JButton("2"));
        p2.add(new JButton("2"));
        con.setLayout(new GridLayout(2,1,10,10));
        con.add(p1);
        con.add(p2);
        this.setVisible(true);
        this.setBounds(50, 50, 200, 200);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);            //设置关闭方式,可以选择多种关闭玄子选项
    }
    public static void main(String[] args) {
        new jframe();
    }
}

clipboard.png

JScrollPane面板(带滚动条的面板)

package jframe;
import java.awt.Container;
import javax.swing.*;

public class jframe extends JFrame{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public jframe(){
        this.setTitle("test");
        Container con = this.getContentPane();
        JScrollPane jp = new JScrollPane(new JTextArea(50,20));        //实例化一个面板,加入一个文字编辑器
        con.add(jp);
        this.setVisible(true);
        this.setBounds(50, 50, 200, 200);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);            //设置关闭方式,可以选择多种关闭玄子选项
    }
    public static void main(String[] args) {
        new jframe();
    }
}

clipboard.png


Cheney
95 声望8 粉丝

初学者