Java窗口(JFrame)從零開始(5)——JPanel面板+JScrollPane面板

JPanel面板java

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

相關文章
相關標籤/搜索