Java更改所有按钮的字体 [英] Java Changing Font For All Buttons

查看:240
本文介绍了Java更改所有按钮的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序正在使用大量的JButton,我想知道如何才能更改特定面板中所有现有按钮的字体,而不必分别更改每个按钮的字体.

My Program is using an immense amount of JButton's, I was wondering how I would be able to change the font of all existing buttons within a specific panel, without having to individually change the font for each button.

推荐答案

我想知道如何更改所有现有字体 特定面板中的按钮,而无需单独更改 每个按钮的字体

I was wondering how I would be able to change the font of all existing buttons within a specific panel, without having to individually change the font for each button

从技术上讲,您不能,您需要能够迭代容器并分别更改每个按钮...

Technically, you can't, you need to be able to iterate the container and change each button individually...

假设所有按钮都在单个容器中(并且不包含在多个子容器中),则可以简单地遍历给定容器中的所有组件,测试以查看它们是否为JButton并应用新按钮.字体.

Assuming that all the buttons are on a single container (and not contained within multiple sub containers), you could simply iterate through all the components in the given container, test to see if they are a JButton and apply the new font.

Font font = new Font("Arial", Font.BOLD, 48);
for (Component comp : getComponents()) {
    if (comp instanceof JButton) {
        ((JButton)comp).setFont(font);
    }
}

例如...

这篇关于Java更改所有按钮的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆