首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

批处理字体的大小和颜色

批处理字体的大小和颜色

(不修改源代码)

耿祥义

摘要:不修改学生写的容器源代码的前提下,修改容器中的按钮以及文本框的字体大小和颜色。

一、窗口里的按钮和文本框

1.学生编写的窗口(模拟)

Calculation win= new Calculation();

在文本框里输入数字,单击相应按钮,文本框显示数字的某种计算结果,效果如图(计算了5的平方根):

2.学生的需求

“耿老师,我想修改按钮和文本框的字体的大小和颜色。”

耿老师不想更改学生的关于按钮和文本框所在容器的源文件。于是编写了一个ChangeFontAndColor类。学生只要用ChangeFontAndColor类调用

changeJButton(Container con,Font f,Color c)

方法就可以把自己容器con中的按钮的字体更改为f,颜色更改为c。

调用

hangeJTextField(Container con,Font f,Color c)

方法就可以把自己容器con中的文本框的字体更改为f,颜色更改为c。

//学生写好的窗体:

Calculation win= new Calculation();

//学生使用耿老师的代码

Font f =

new Font("楷体",Font.BOLD,28);

ChangeFontAndColor.changeJButton

(win,f,Color.blue);

f =

new Font("宋体",Font.PLAIN,22);

ChangeFontAndColor.changeJTextField

(win,f,new Color(10,220,90));

效果如图:

二、视频讲解

复制

https://share.weiyun.com/C59pNM4x

到浏览器地址栏即可。

1.主类

import java.awt.Color;

import java.awt.Font;

public class MainClass {

public static void main(String arg[]){

Calculation win=

new Calculation();//学生的窗体

Font f = new Font("楷体",Font.BOLD,28);

//使用耿老师写的类修改字体和颜色:

ChangeFontAndColor.changeJButton

(win,f,Color.blue);

f = new Font("宋体",Font.PLAIN,22);

ChangeFontAndColor.changeJTextField

(win,f,new Color(10,220,90));

}

}

2.学生写的窗口(模拟)

import javax.swing.JFrame ;

import javax.swing.JButton;

import javax.swing.JTextField;

import java.awt.FlowLayout;

public class Calculation extends JFrame {

JButton sqrt;

JButton exp;

JTextField inputNumber;

public Calculation(){

setTitle("计算");

setLayout(new FlowLayout());

inputNumber = new JTextField("1",20);

sqrt = new JButton("平方根");

exp = new JButton("exp");

add(inputNumber);

add(sqrt);

add(exp);

sqrt.addActionListener((e)->

{ String s = inputNumber.getText();

try{

double x = Double.parseDouble(s);

double m = Math.sqrt(x);

inputNumber.setText(""+m);

}

catch(NumberFormatException exp){

inputNumber.setText("请输入数字");

}

});

exp.addActionListener((e)->

{ String s = inputNumber.getText();

try{

double x = Double.parseDouble(s);

double m = Math.exp(x);

inputNumber.setText(""+m);

}

catch(NumberFormatException exp){

inputNumber.setText("请输入数字");

}

});

setVisible(true);

setBounds(0,0,600,389);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

3.耿老师写的类(帮助批处理容器中按钮文本框的字体)

import javax.swing.*;

import java.awt.*;

public class ChangeFontAndColor {

public static void changeJButton(Container con,Font f,Color c){

Component[] com = null;

if(con instanceof JFrame ){

JFrame win =(JFrame)con;

com =

win.getContentPane().getComponents();

}

else if(con instanceof JDialog){

JDialog dialog =(JDialog)con;

com =

dialog.getContentPane().getComponents();

}

else if(con instanceof JComponent){

com = con.getComponents();

}

for(int i =0;i

if(com[i] instanceof JButton){

JButton b = (JButton)com[i];

b.setFont(f);

b.setForeground(c);

}

}

con.repaint();

con.validate();

}

public static void changeJTextField(Container con,Font f,Color c){

Component[] com = null;

if(con instanceof JFrame ){

JFrame win =(JFrame)con;

com =

win.getContentPane().getComponents();

}

else if(con instanceof JDialog){

JDialog dialog =(JDialog)con;

com =

dialog.getContentPane().getComponents();

}

else if(con instanceof JComponent){

com = con.getComponents();

}

for(int i =0;i

if(com[i] instanceof JTextField){

JTextField text = (JTextField)com[i];

text.setFont(f);

text.setForeground(c);

}

}

con.repaint();

con.validate();

}

}

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20200528A03B0D00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券
http://www.vxiaotou.com