Java 爪哇学习(七) 基础

Java GUI

Container — Windows

Frame —>JFrame

Dialog —> JDialog

JComponet

Jpaner

JscrollPane

具体组件JLable

​ JButton

* 同时带一个方法的引用
public class App {

    private static  void createShowGUI(){
        JFrame frame = new JFrame("");   //创建设置JFrame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //设置关闭窗口操作
        frame.setSize(250,150);         //设置窗口尺寸
        //展示JFrame容器窗口
        frame.setVisible(true);
    }
    public static void main(String[] args){
//        SwingUtilities.invokeLater(new Runnable() {   //()->App.createShowGUI();
//            @Override
//            public void run() {
//                App.createShowGUI();
//            }
//        });
        SwingUtilities.invokeLater(App::createShowGUI);

    }

}
子窗口

⚠️:子窗口都能能一起动。子窗口超过父的都会被隐藏

private static  void createShowGUI(){
    JFrame frame = new JFrame("");   //创建设置JFrame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //设置关闭窗口操作
    frame.setSize(250,150);         //设置窗口尺寸
    //展示JFrame容器窗口
    frame.setVisible(true);

    //JDialog  设置子窗口
    JDialog dialog = new JDialog(frame,"JDialog对话框",true);
    dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);  //关闭隐藏
    dialog.setSize(200,100);
    dialog.setVisible(true);
}

布局管理器

⚠️总共有五个方位 代码里面实现两个

private static  void createShowGUI(){
    JFrame frame = new JFrame("");   //创建设置JFrame
    frame.setLayout(new BorderLayout());
    frame.setSize(300,300);
    frame.setLocation(3000,200);   //设置窗体显示位置

    JButton but1 = new JButton("1");
    JButton but2 = new JButton("2");


    frame.add(but1,BorderLayout.PAGE_START);
    frame.add(but2,BorderLayout.CENTER);

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

流 管理布局

private static  void createShowGUI(){
    JFrame frame = new JFrame("");   //创建设置JFrame
    frame.setLayout(new FlowLayout(FlowLayout.LEFT,20,30));  //左到右 +间隔设置水平20  垂直30
    frame.setSize(300,300);
    frame.setLocation(3000,200);   //设置窗体显示位置

    frame.add(new Button("btn"));
    frame.add(new Button("btn"));
    frame.add(new Button("btn"));
    frame.add(new Button("btn"));
    frame.add(new Button("btn"));
    frame.add(new Button("btn"));

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

网格 布局

GridLayout

private static  void createShowGUI(){
    JFrame frame = new JFrame("");   //创建设置JFrame
    frame.setLayout(new GridLayout(3,3));
    frame.setSize(600,600);
    frame.setLocation(3000,200);   //设置窗体显示位置

    for (int i=0;i<20;i++){
        Button btn = new Button("Btn"+i);
        frame.add(btn);
    }


    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

其他可用

SWT 可视化设计工具

JavaFX

事件处理机制

事件源 事件 处理 (方法)

MouseListener

KeyListener

WindowsListener

ActionListener —》函数式接口

e.g.:

class MyListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("用户点击了JButton按钮组件");
    }
}

public class App {
    private static  void createShowGUI(){
        JFrame frame = new JFrame("");   //创建设置JFrame
        frame.setLayout(new GridLayout(3,3));
        frame.setSize(600,600);
        frame.setLocation(3000,200);   //设置窗体显示位置

        for (int i=0;i<20;i++){
            Button btn = new Button("Btn"+i);
            btn.addActionListener(new MyListener());
            frame.add(btn);
        }

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public static void main(String[] args){
        SwingUtilities.invokeLater(App::createShowGUI);
    }

}

窗口

private static  void createShowGUI(){
    JFrame frame = new JFrame("");   //创建设置JFrame
    frame.setLayout(new GridLayout(3,3));
    frame.setSize(600,600);
    frame.setLocation(3000,200);   //设置窗体显示位置
    frame.addWindowListener(new WindowListener() {   //窗口事件
        @Override
        public void windowOpened(WindowEvent e) {
            System.out.println("win open");
        }

        @Override
        public void windowClosing(WindowEvent e) {
            System.out.println("windowClosing");
        }

        @Override
        public void windowClosed(WindowEvent e) {
            System.out.println("windowClosed");
        }

        @Override
        public void windowIconified(WindowEvent e) {
            System.out.println("windowIconified");
        }

        @Override
        public void windowDeiconified(WindowEvent e) {
            System.out.println("windowDeiconified");
        }

        @Override
        public void windowActivated(WindowEvent e) {
            System.out.println("windowActivated");
        }

        @Override
        public void windowDeactivated(WindowEvent e) {
            System.out.println("windowDeactivated");
        }
    });
    for (int i=0;i<20;i++){
        Button btn = new Button("Btn"+i);
        btn.addActionListener(new MyListener());
        frame.add(btn);
    }

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

keyPressed

总结

事件源事件监听器接口/适配器注册方法
windowWindowEventWindowsListner/AdapteraddWindowListner
MouseEventMouseListner/AdapteraddMouseListner
keyEvent
ActionEventaddActionListner

添加滚动条

private static  void createShowGUI(){
    JFrame frame = new JFrame("");   //创建设置JFrame
    frame.setLayout(new BorderLayout());
    frame.setSize(300,300);
    frame.setLocation(3000,200);   //设置窗体显示位置

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);  //需要时 滚动条出.
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);   //总是有
    JPanel panel  =new JPanel();
    panel.add(new Button("btn"));
    panel.add(new Button("btn"));
    panel.add(new Button("btn"));
    panel.add(new Button("btn"));
    panel.add(new Button("btn"));

    //滚动条放 panel的东西
    scrollPane.setViewportView(panel);
    //JFrame 添加 滚动条
    frame.add(scrollPane, BorderLayout.CENTER);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值