【Java-GUI】08 Swing02

2021-03-03 01:28

阅读:726

标签:布局   port   oid   dia   图片   演示   content   text   一个   

边框案例:

package cn.dzz.swing;

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

public class Demo02 {

    JFrame jFrame = new JFrame("边框测试");

    public void init() {

        // 修改为栅格布局
        jFrame.setLayout(new GridLayout(2,4));

        // 向单元格填充组件,设置边框和内容

        // 1斜角边框
        Border bevelBorder = BorderFactory.createBevelBorder(
                BevelBorder.RAISED,
                Color.RED,
                Color.GREEN,
                Color.BLUE,
                Color.GRAY
        );
        JPanel bevelBorderJPanel = getJPanelWithBorder(bevelBorder, "BevelBorder");

        // 2线条边框
        Border lineBorder = BorderFactory.createLineBorder(
                Color.ORANGE,
                10
        );
        JPanel lineBorderJPanel = getJPanelWithBorder(lineBorder, "LineBorder");

        // 3空边框
        Border emptyBorder = BorderFactory.createEmptyBorder(10, 5, 20, 10);
        JPanel emptyBorderJPanel = getJPanelWithBorder(emptyBorder, "EmptyBorder");

        // 4蚀刻边框
        Border etchedBorder = BorderFactory.createEtchedBorder(
                EtchedBorder.RAISED,
                Color.RED,
                Color.GREEN
        );
        JPanel etchedBorderJPanel = getJPanelWithBorder(etchedBorder, "EtchedBorder");

        // 5标题边框
        TitledBorder titledBorder = new TitledBorder(new LineBorder(
                Color.ORANGE,
                10
        ),
                "测试标题",
                TitledBorder.LEFT,
                TitledBorder.BOTTOM,
                new Font("StSong", Font.BOLD, 18),
                Color.BLUE
        );
        JPanel titleBorderJPanel = getJPanelWithBorder(titledBorder, "TitleBorder");

        // 6哑光边框
        MatteBorder matteBorder = new MatteBorder(10, 5, 20, 10, Color.GREEN);
        JPanel matteBorderJPanel = getJPanelWithBorder(matteBorder, "MatteBorder");

        // 7复合边框
        // CompoundBorder compoundBorder = new CompoundBorder(titledBorder, new LineBorder(Color.RED, 10));
        CompoundBorder compoundBorder = new CompoundBorder(new LineBorder(Color.RED, 10), titledBorder);
        JPanel compoundBorderJPanel = getJPanelWithBorder(compoundBorder, "CompoundBorder");

        // 组装
        jFrame.add(bevelBorderJPanel);
        jFrame.add(lineBorderJPanel);
        jFrame.add(emptyBorderJPanel);
        jFrame.add(etchedBorderJPanel);
        jFrame.add(titleBorderJPanel);
        jFrame.add(matteBorderJPanel);
        jFrame.add(compoundBorderJPanel);

        // 设置窗口最佳大小 和 可见 可关闭
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    // 要设置8个面板对象,就要写8次,所以进行了一个封装处理
    JPanel getJPanelWithBorder(Border border,String content) {
        JPanel jPanel = new JPanel();
        jPanel.add(new JLabel(content));
        jPanel.setBorder(border);
        return jPanel;
    }
    
    public static void main(String[] args) {
        new Demo02().init();
    }
}

  

边框设置的参数种类很多

技术图片

 

工具条案例JToolBar:

 

package cn.dzz.swing;

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

public class Demo03 {

    JFrame jFrame = new JFrame("工具条演示案例");
    JTextArea jTextArea = new JTextArea(6, 35);
    JToolBar jToolBar = new JToolBar("PlayerToolBar", SwingConstants.HORIZONTAL);

    // 3个action对象
    Action preAct = new AbstractAction("?? 上一首") {
        @Override
        public void actionPerformed(ActionEvent e) {
            jTextArea.append("上一曲\n");
        }
    };

    Action pauseAct = new AbstractAction("? 暂停") {
        @Override
        public void actionPerformed(ActionEvent e) {
            jTextArea.append("暂停播放\n");
        }
    };

    Action nextAct = new AbstractAction("?? 下一首") {
        @Override
        public void actionPerformed(ActionEvent e) {
            jTextArea.append("下一曲\n");
        }
    };

    public void init() {

        // 直接组装
        jToolBar.add(preAct);
        jToolBar.addSeparator();
        jToolBar.add(pauseAct);
        jToolBar.addSeparator();
        jToolBar.add(nextAct);

        jFrame.add(jToolBar,BorderLayout.NORTH);
        jFrame.add(jTextArea,BorderLayout.CENTER);

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        new Demo03().init();
    }
}

技术图片

 

颜色选择和文件选择:

ColorChooser & FileChooser

 

package cn.dzz.swing;

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

public class Demo04 {

    JFrame jFrame = new JFrame("颜色选择器");
    JTextArea jTextArea = new JTextArea("我爱Java",6,30);
    JButton changeBgColor = new JButton( new AbstractAction("更改编辑区背景色") {
        @Override
        public void actionPerformed(ActionEvent e) {
            Color color = JColorChooser.showDialog(
                    jFrame, // 父级组件
                    "选择颜色", // 对话框标题
                    Color.WHITE // 初始颜色
            );
            jTextArea.setBackground(color);
        }
    });

    public void init(){
        jFrame.add(jTextArea, BorderLayout.CENTER);
        jFrame.add(changeBgColor, BorderLayout.SOUTH);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        new Demo04().init();
    }
}

效果显示

技术图片技术图片

技术图片

 

【Java-GUI】08 Swing02

标签:布局   port   oid   dia   图片   演示   content   text   一个   

原文地址:https://www.cnblogs.com/mindzone/p/14399248.html


评论


亲,登录后才可以留言!