java课题--day5基础回顾

java课题–day5学习JFRAME

第一个弹窗

import javax.swing.*;

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 9:37
 * 生成弹窗
 */
public class JfDemo {
    public static void main(String[] args) {
        JFrame jf=new JFrame("可爱的弹窗");
        //设置尺寸位置
        jf.setBounds(500,300,900,700);
        jf.setIconImage(new ImageIcon("src/W0.png").getImage());
        //关闭时退出虚拟机
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }
}

弹窗居中–去除任务栏

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

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 10:33
 * 窗口居中
 */
public class JfDemo3 {
    private static final int WIDTH=500;
    private static final int HEIGHT=400;

    public static void main(String[] args) {
        JFrame jFrame=new JFrame("窗口居中");
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle rectangle=ge.getMaximumWindowBounds();
        int width=rectangle.width;
        int height=rectangle.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}

带任务栏居中

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

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 10:09
 * 窗口居中
 */
public class JfDemo2 {
    private static final int WIDTH=500;
    private static final int HEIGHT=400;

    public static void main(String[] args) {
        JFrame jFrame=new JFrame("窗口居中");
        Toolkit toolkit=Toolkit.getDefaultToolkit();
        Dimension screenSize=toolkit.getScreenSize();
        int width=screenSize.width;
        int height=screenSize.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}

按钮–流式布局

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

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 10:48
 * 流式布局
 */
public class JfButtonDemo {
    private static final int WIDTH=500;
    private static final int HEIGHT=400;

    public static void main(String[] args) {
        JFrame jframe = JfButtonDemo.init();
        jframe.setLayout(new FlowLayout(FlowLayout.LEFT));
        for (int i = 0; i <100 ; i++) {
            JButton jButton1 = new JButton("button"+i);//生成按钮
            jframe.add(jButton1);//加入到窗口
        }
        jframe.setVisible(true);
    }
    public static JFrame init() {
        JFrame jFrame=new JFrame("窗口居中");
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle rectangle=ge.getMaximumWindowBounds();
        int width=rectangle.width;
        int height=rectangle.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jFrame;
    }
}

按钮–边界布局

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

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 11:06
 * 边界布局
 */

public class JfBouttonDemo1 {
    private static final int WIDTH=1000;
    private static final int HEIGHT=800;

    public static void main(String[] args) {
        JFrame jframe = JfButtonDemo.init();
        jframe.setLayout(new BorderLayout(10,10));
        JButton jButton1 = new JButton("button1");//生成按钮
        JButton jButton2 = new JButton("button2");//生成按钮
        JButton jButton3 = new JButton("button3");//生成按钮
        JButton jButton4 = new JButton("button4");//生成按钮
        JButton jButton5 = new JButton("button5");//生成按钮
        jframe.add(jButton1,BorderLayout.NORTH);
        jframe.add(jButton2,BorderLayout.SOUTH);
        jframe.add(jButton3,BorderLayout.EAST);
        jframe.add(jButton4,BorderLayout.WEST);
        jframe.add(jButton5,BorderLayout.CENTER);
        jframe.setVisible(true);
    }
    public static JFrame init() {
        JFrame jFrame=new JFrame("窗口居中");
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle rectangle=ge.getMaximumWindowBounds();
        int width=rectangle.width;
        int height=rectangle.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jFrame;
    }
}

按钮–网格布局

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

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 11:30
 * 网格布局
 */
public class JfButtonDemo2 {
    private static final int WIDTH=1000;
    private static final int HEIGHT=800;

    public static void main(String[] args) {
        JFrame jframe = JfButtonDemo.init();
        jframe.setLayout(new GridLayout(5,5));
        for (int i = 0; i <25 ; i++) {
            JButton jButton1 = new JButton("button"+i);//生成按钮
            jframe.add(jButton1);//加入到窗口
        }
        jframe.setVisible(true);
    }
    public static JFrame init() {
        JFrame jFrame=new JFrame("窗口居中");
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle rectangle=ge.getMaximumWindowBounds();
        int width=rectangle.width;
        int height=rectangle.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jFrame;
    }
}

面板

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

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 11:37
 * jpanel面板
 */
public class JPanelDemo {
    private static final int WIDTH=500;
    private static final int HEIGHT=400;

    public static void main(String[] args) {
        JPanel jPanel=new JPanel(new GridLayout(5,1));
        JPanel jPane2=new JPanel(new BorderLayout());
        JFrame jframe = JfButtonDemo.init();
        jframe.setLayout(new BorderLayout(10,10));
        jframe.add(jPanel,BorderLayout.WEST);
        JButton jb=new JButton("center");
        jPane2.add(jb);
        jframe.add(jPane2,BorderLayout.CENTER);
        for (int i = 0; i <5 ; i++) {
            JButton jButton1 = new JButton("button"+i);//生成按钮
            jPanel.add(jButton1);//加入到窗口
        }
        jframe.setVisible(true);
    }
    public static JFrame init() {
        JFrame jFrame=new JFrame("窗口居中");
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle rectangle=ge.getMaximumWindowBounds();
        int width=rectangle.width;
        int height=rectangle.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jFrame;
    }
}

文本输入框

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

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 14:09
 * 文本输入框
 */
public class JLableDemo {
        private static final int WIDTH=1000;
        private static final int HEIGHT=800;
        public static void main(String[] args) {
            JFrame jframe = init();
            jframe.setLayout(new GridLayout(2,1));
            JPanel jPanel=new JPanel();
            JLabel lable=new JLabel("请输入用户名:",new ImageIcon("src/W0.png"),JLabel.CENTER);
            JTextField jtf=new JTextField(40);//文本框
            jPanel.add(lable);
            jPanel.add(jtf);
            JPanel jPane2=new JPanel();
            JLabel jlable2=new JLabel("请输入密码:",new ImageIcon("src/W30.png"),JLabel.CENTER);
            JTextField jtf2=new JTextField(40);//文本框
            jPanel.add(jlable2);
            jPanel.add(jtf2);
            jframe.add(jPanel);
            jframe.add(jPane2);
            jframe.setVisible(true);
        }
    public static JFrame init() {
        JFrame jFrame=new JFrame("窗口居中");
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle rectangle=ge.getMaximumWindowBounds();
        int width=rectangle.width;
        int height=rectangle.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jFrame;
        }
}

选择框

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

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 14:44
 * 选择框
 */
public class JComboBoxDemo {
    private static final int WIDTH=1000;
    private static final int HEIGHT=800;

    public static void main(String[] args) {
        JFrame jframe = init();
        jframe.setLayout(new GridLayout(5,1));
        JPanel jPanel=new JPanel();
        JLabel jlb1=new JLabel("请输入您的学习语言:");
        JComboBox box=new JComboBox();//复选框
        JCheckBox jcb1=new JCheckBox("足球");
        JCheckBox jcb2=new JCheckBox("篮球");
        JCheckBox jcb3=new JCheckBox("乒乓球");
        box.addItem("Java");
        box.addItem("c");
        box.addItem("C++");
        box.addItem("sql");
        jPanel.add(jlb1);
        jPanel.add(box);
        jframe.add(jPanel);
        JPanel jPanel2 = new JPanel();
        JLabel jlb2=new JLabel("请输入你的爱好:");
        jframe.add(jPanel2);
        jPanel2.add(jlb2);
        jPanel2.add(jcb1);
        jPanel2.add(jcb2);
        jPanel2.add(jcb3);
        JPanel jpanel3 =new JPanel();
        JLabel jlb3=new JLabel("您的性别:");
        JRadioButton jrb1=new JRadioButton("男");
        JRadioButton jrb2=new JRadioButton("女");
        JRadioButton jrb3=new JRadioButton("中性");
        JRadioButton jrb4=new JRadioButton("保密");
        ButtonGroup bg=new ButtonGroup();//bg包裹后,只能单选
        bg.add(jrb1);
        bg.add(jrb2);
        bg.add(jrb3);
        bg.add(jrb4);
        jpanel3.add(jlb3);
        jpanel3.add(jrb1);
        jpanel3.add(jrb2);
        jpanel3.add(jrb3);
        jpanel3.add(jrb4);
        jframe.add(jpanel3);
        jframe.setVisible(true);
    }
    public static JFrame init() {
        JFrame jFrame=new JFrame("窗口居中");
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle rectangle=ge.getMaximumWindowBounds();
        int width=rectangle.width;
        int height=rectangle.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jFrame;
    }
}

文件选择

import com.sun.org.apache.bcel.internal.generic.NEW;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 15:25
 * 文件选择
 */
public class JFileChooserDemo {
    private static final int WIDTH=1000;
    private static final int HEIGHT=800;

    public static void main(String[] args) {
        JFrame jframe = init();
        JPanel jPanel=new JPanel();
        JLabel jlb1=new JLabel("请选择头像:");
        JButton jb=new JButton("选择头像");
        JTextField jtf=new JTextField(30);
        jPanel.add(jlb1);
        jPanel.add(jtf);
        jPanel.add(jb);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfilehelpers = new JFileChooser();
                int returnVal = jfilehelpers.showOpenDialog(null);
                if (returnVal==JFileChooser.APPROVE_OPTION){
                    jtf.setText(jfilehelpers.getSelectedFile().getAbsolutePath());
                }
            }
        });
        jframe.add(jPanel);
        jframe.setVisible(true);
    }
    public static JFrame init() {
        JFrame jFrame=new JFrame("窗口居中");
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle rectangle=ge.getMaximumWindowBounds();
        int width=rectangle.width;
        int height=rectangle.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jFrame;
    }
}

滚动条

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

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 15:43
 */
public class JScrollPaneDemo {
    private static final int WIDTH=1000;
    private static final int HEIGHT=800;

    public static void main(String[] args) {
        JFrame jframe = init();
        JPanel jPanel=new JPanel();
        //JPanel jPanel2=new JPanel();
        jPanel.setLayout(new GridLayout(500,1));
        for (int i = 0; i <500 ; i++) {
            JButton jButton1 = new JButton("button"+i);//生成按钮
            jPanel.add(jButton1);//加入到面板
        }
        JScrollPane jsp = new JScrollPane(jPanel);
        jframe.add(jsp);
        //jframe.add(jPanel2);
        jframe.setVisible(true);
    }
    public static JFrame init() {
        JFrame jFrame=new JFrame("窗口居中");
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle rectangle=ge.getMaximumWindowBounds();
        int width=rectangle.width;
        int height=rectangle.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jFrame;
    }
}

消息弹窗

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Shangkejie
 * @Class BigData182
 * @date 2019/12/13 - 14:44
 * 选择框
 */
public class JComboBoxDemo {
    private static final int WIDTH=1000;
    private static final int HEIGHT=800;

    public static void main(String[] args) {
        JFrame jframe = init();
        jframe.setLayout(new GridLayout(5,1));
        JPanel jPanel=new JPanel();
        JLabel jlb1=new JLabel("请输入您的学习语言:");
        JComboBox box=new JComboBox();//复选框
        JCheckBox jcb1=new JCheckBox("足球");
        JCheckBox jcb2=new JCheckBox("篮球");
        JCheckBox jcb3=new JCheckBox("乒乓球");
        box.addItem("Java");
        box.addItem("c");
        box.addItem("C++");
        box.addItem("sql");
        jPanel.add(jlb1);
        jPanel.add(box);
        jframe.add(jPanel);
        JPanel jPanel2 = new JPanel();
        JLabel jlb2=new JLabel("请输入你的爱好:");
        jframe.add(jPanel2);
        jPanel2.add(jlb2);
        jPanel2.add(jcb1);
        jPanel2.add(jcb2);
        jPanel2.add(jcb3);
        JPanel jpanel3 =new JPanel();
        JLabel jlb3=new JLabel("您的性别:");
        JRadioButton jrb1=new JRadioButton("男");
        JRadioButton jrb2=new JRadioButton("女");
        JRadioButton jrb3=new JRadioButton("中性");
        JRadioButton jrb4=new JRadioButton("保密");
        ButtonGroup bg=new ButtonGroup();//bg包裹后,只能单选
        bg.add(jrb1);
        bg.add(jrb2);
        bg.add(jrb3);
        bg.add(jrb4);
        jpanel3.add(jlb3);
        jpanel3.add(jrb1);
        jpanel3.add(jrb2);
        jpanel3.add(jrb3);
        jpanel3.add(jrb4);
        jframe.add(jpanel3);
        JPanel jPanel4=new JPanel();
        JButton jButton4=new JButton("---注册---");
        jButton4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //消息框
                JOptionPane.showMessageDialog(null,"注册成功");
                //确认框
                int i=JOptionPane.showConfirmDialog(null,"确认删除吗");
                System.out.println(i);
                //输入框
                JOptionPane.showInputDialog(null);
            }
        });
        jPanel4.add(jButton4);
        jframe.add(jPanel4);
        jframe.setVisible(true);
    }
    public static JFrame init() {
        JFrame jFrame=new JFrame("窗口居中");
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle rectangle=ge.getMaximumWindowBounds();
        int width=rectangle.width;
        int height=rectangle.height;
        jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);//窗口居中
        jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jFrame;
    }
}
发布了83 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43759910/article/details/103527876
今日推荐