gpt4 book ai didi

java - 根据选定的 JComboBox 项目更改 JTextArea 颜色

转载 作者:行者123 更新时间:2023-11-30 05:01:04 25 4
gpt4 key购买 nike

除了基本的主线程之外,这还将显示一个带有句子的窗口,并在从下拉菜单中选择某些内容后立即将字体更改为粗体。

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

public class Gui extends JFrame {

private JComboBox box;
private JTextField tf;
private static String [] filename = {"button.png", "x.png"};

public Gui(){
super("The title is");
setLayout(new FlowLayout());
box = new JComboBox(filename);
tf = new JTextField("This is a sentence", 14);

box.addItemListener( new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(event.getStateChange()==ItemEvent.SELECTED) {
tf.setFont(new Font("Serif", Font.BOLD, 14));
}
}
});

add(box);
add(tf);
}
}

如果我从下拉菜单中选择其他成员,有什么方法可以让它改回来吗?

最佳答案

尝试:

tf.setFont(new Font("Serif", Font.PLAIN, 14));

要检查项目是否已选择,请使用 box.getSelectedItem()box.getSelectedIndex()

例如:

public void itemStateChanged(ItemEvent event){
if(event.getStateChange()==ItemEvent.SELECTED){
if (box.getSelectedIndex()==0)
tf.setFont(new Font("Serif", Font.BOLD, 14)); //first item selected
else
tf.setFont(new Font("Serif", Font.PLAIN, 14)); //second item selected
}
}

关于java - 根据选定的 JComboBox 项目更改 JTextArea 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6710087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com