关闭

JPanel (GridLayout(),FlowLayout(),BorderLayout(),null)

标签: AWTJAVASwingJPanelLayout
195人阅读 评论(0) 收藏 举报
分类:
package jyh;

import java.awt.*;
import javax.swing.*;

public class ComplexLayoutDemo extends JFrame{
    private JPanel panel1=new JPanel();
    private JPanel panel2=new JPanel();
    private JPanel panel3=new JPanel();
    private JPanel panel4=new JPanel();
    public ComplexLayoutDemo()
    {
        setSize(500,500);
        setLocation(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        LayoutPanel1();
        LayoutPanel2();
        LayoutPanel3();
        LayoutPanel4();
        setLayout(new GridLayout(2, 2));
        add(panel1);
        add(panel2);
        add(panel3);
        add(panel4);
    }
    private void LayoutPanel1(){
         JButton north=new JButton("北");
         JButton south=new JButton("南");
         JButton east=new JButton("东");
         JButton west=new JButton("西");
         JButton center=new JButton("中");
         panel1.setLayout(new BorderLayout());
         panel1.add(north, BorderLayout.NORTH);
         panel1.add(south, BorderLayout.SOUTH);
         panel1.add(west, BorderLayout.WEST);
         panel1.add(east, BorderLayout.EAST);
         panel1.add(center, BorderLayout.CENTER);
    }
    private void LayoutPanel2(){
        JButton button1=new JButton("按钮 1");
        JButton button2=new JButton("按钮 2");
        JButton button3=new JButton("按钮 3");
        JButton button4=new JButton("按钮 4");
        panel2.setLayout(new FlowLayout());
        panel2.add(button1);
        panel2.add(button2);
        panel2.add(button3);
        panel2.add(button4);
    }
    private void LayoutPanel3(){
        JButton button1=new JButton("按钮 1");
        JButton button2=new JButton("按钮 2");
        JButton button3=new JButton("按钮 3");
        JButton button4=new JButton("按钮 4");
        panel3.setLayout(new GridLayout(2, 2));
        panel3.add(button1);
        panel3.add(button2);
        panel3.add(button3);
        panel3.add(button4);
    }
    private void LayoutPanel4(){
        JButton button=new JButton("按钮");
        JTextField textField=new JTextField("文本框");
        panel4.setLayout(null);
        button.setLocation(20,20);
        button.setSize(100,20);
        textField.setBounds(20, 50, 200, 100);
        panel4.add(button);
        panel4.add(textField);
    }
    public static void main(String[] args) {
        ComplexLayoutDemo frame=new ComplexLayoutDemo();
        frame.setVisible(true);
    }

}
1
0

  相关文章推荐
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:75201次
    • 积分:3165
    • 等级:
    • 排名:第11285名
    • 原创:248篇
    • 转载:0篇
    • 译文:0篇
    • 评论:14条
    最新评论