java之swing

Swing 是一个为Java设计的GUI工具包。

Swing是JAVA基础类的一部分。

Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。

Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。它们是JFC的一部分。它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用JAVA支持的任意面板。轻量级组件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。

下面是一个创建Swing窗口,并通过按钮改变背景颜色的案例

import javax.swing.*;


public class TestMain {
    public static void main(String[] args) {
        //创建及设置窗口
        FontFeame fontFeame = new FontFeame();
        fontFeame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示窗口
        fontFeame.setVisible(true);
    }
}
import javax.swing.*;
import java.awt.*;


/**
 * @ClassName FontFeame
 * @Description TODO
 * 继承JFrame
 */
public class FontFeame extends JFrame {
    public FontFeame(){
      //设置标题
      setTitle("北京");
      //设置窗口宽度、高度
      setSize(400,300);
      FontPanel fontPanel = new FontPanel();
      //获取内容面板
      Container contentPane = getContentPane();
      //添加内容
      contentPane.add(fontPanel);
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;

/**
 * @ClassName FontPanel
 * @Description TODO
 */
public class FontPanel extends JPanel {

    public FontPanel(){
        //创建按钮
        JButton yellowButton = new JButton("黄色");
        JButton blueButton = new JButton("绿色");
        JButton greenButton = new JButton("蓝色");
        add(yellowButton);
        add(blueButton);
        add(greenButton);
        //添加监听事件
        ColorAction yellowAction = new ColorAction(Color.YELLOW);
        ColorAction blueAction = new ColorAction(Color.BLUE);
        ColorAction greenAction = new ColorAction(Color.GREEN);
        yellowButton.addActionListener(yellowAction);
        blueButton.addActionListener(blueAction);
        greenButton.addActionListener(greenAction);

    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        String message = "这是内容";
        Font f = new Font("隶书", Font.BOLD, 27);
        g2.setFont(f);
        //计算出内容居中的横向坐标和纵向坐标
        FontRenderContext fontRenderContext = g2.getFontRenderContext();
        Rectangle2D bounds = f.getStringBounds(message, fontRenderContext);
        double x = (getWidth()- bounds.getWidth())/2;
        double y = (getHeight() - bounds.getHeight())/2;
        double ascent = -bounds.getY();
        double baseY = y+ascent;
        //设置内容颜色
        g2.setPaint(Color.RED);
        //设置内容、横向位置、纵向位置
        g2.drawString(message,(int)x,(int)baseY);


    }

    private class ColorAction implements ActionListener{

        private Color BackGroundColor;

        public ColorAction(Color c){
            BackGroundColor = c;
        }

        @Override
        //监听回调
        public void actionPerformed(ActionEvent e) {
            //设置背景颜色
            setBackground(BackGroundColor);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值