8.19~8.25 每周总结

本周学习了Java的JFrame容器,对于此类知识点的简单使用如下:

JFrame的使用

package JFrame容器;

import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;

public class JFrameTest {
   
    public static void main(String[] args) {
        JFrame jFrame=new JFrame("JFrame窗体");  //创建并命名
        jFrame.setLocation(300, 200); //设置容器位置
        /*Container c=jFrame.getContentPane(); //设置背景色
        c.setBackground(Color.red);*/
        jFrame.getContentPane().setBackground(Color.blue);
        jFrame.setSize(500,200);  //设置容器大小
        jFrame.setVisible(true); //让容器显示
    }
}

创建一个容器如下:

其次还可以设置按钮JButton

package JFrame容器;

import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;

public class JFrameTest {
   
    public static void main(String[] args) {
        JFrame jFrame=new JFrame("JFrame窗体");  //创建并命名
        jFrame.setLocation(300, 200); //设置容器位置
        /*Container c=jFrame.getContentPane(); //设置背景色
        c.setBackground(Color.red);*/
        jFrame.getContentPane().setBackground(Color.blue);
        jFrame.setSize(500,200);  //设置容器大小
        jFrame.setVisible(true); //让容器显示
    }


表示如下:

其次还有三种布局方式:

绝对布局: AbsouluateLayout

package JFrame容器;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class AbsoluteLayoutTest绝对布局 {
    public static void main(String[] args) {
    JFrame jFrame=new JFrame("绝对布局测试");
        jFrame.setLayout(null);
    JButton jb1=new JButton("按钮一");
    JButton jb2=new JButton("按钮二");
    jb1.setBounds(50, 10, 100, 20);  //大小固定 无法跟随放大
    jb2.setBounds(70, 40, 200, 30);
        jFrame.add(jb1);
        jFrame.add(jb2);
        
        
        jFrame.setSize(500,200);  //设置容器大小
        jFrame.setLocation(200, 200); //设置容器位置
        jFrame.setVisible(true); //让容器显示
        
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

流式布局:FlowLayout

package JFrame容器;

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
/**
 * FlowLayout 流式布局
 * @author cuixingyu
 *
 */

public class FlowLayoutTest流式布局 {

    public static void main(String[] args) {
        JFrame jFrame=new JFrame("Flowlayout测试");
        
        //jFrame.setLayout(new FlowLayout()); //居中
        //jFrame.setLayout(new FlowLayout(FlowLayout.LEFT)); //左对齐
        jFrame.setLayout(new FlowLayout(FlowLayout.LEFT,15,15)); //水平竖直间隙
        JButton jb=null;
        for(int i=0;i<9;i++)
        {
            jb=new JButton("JButton"+i);
            jFrame.add(jb);
            }
        
        
        
        jFrame.setSize(500,200);  //设置容器大小
        jFrame.setLocation(200, 200); //设置容器位置
        jFrame.setVisible(true); //让容器显示
        
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

网格布局:GridLayout

package JFrame容器;

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GridLayoutTest网格布局 {
    public static void main(String[] args) {
JFrame jFrame=new JFrame("Flowlayout测试");
        
        jFrame.setLayout(new GridLayout(4, 5, 5, 5)); //四行五列    
        
        JButton jb=null;
        for(int i=0;i<19;i++) {
            jb=new JButton("jubtton"+i);
            jFrame.add(jb);
        }
        
        jFrame.setSize(500,200);  //设置容器大小
        jFrame.setLocation(200, 200); //设置容器位置
        jFrame.setVisible(true); //让容器显示
        
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

其他还有JPanel 轻量级容器

package JFrame容器;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class JPanelTest轻量级容器 {

    public static void main(String[] args) {
        JFrame jFrame=new JFrame("JPanel面板测试");
        JPanel jPanel=new JPanel();
        jPanel.setLayout(new GridLayout(3,2,10,10));
        jPanel.setBorder(new EmptyBorder(10,10,10,10));
        jFrame.add(jPanel);
        
        JLabel ji=new JLabel("用户名:");
        JTextField jtf=new JTextField();
        JLabel ji2=new JLabel("密码:");
        JPasswordField jpf=new JPasswordField();
        JButton jb1=new JButton("登陆");
        JButton jb2=new JButton("重置");
        jPanel.add(ji);
        jPanel.add(jtf);
        jPanel.add(ji2);
        jPanel.add(jpf);
        jPanel.add(jb1);
        jPanel.add(jb2);
        
        jFrame.setLocation(200, 200); //设置容器位置
        jFrame.setSize(500,200);  //设置容器大小
        jFrame.setVisible(true); //让容器显示
        
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

呈现结果如下:

猜你喜欢

转载自www.cnblogs.com/cxy0210/p/11405811.html