【问题标题】:text in textfield is displayed but wont change font when choosing in combobox显示文本字段中的文本,但在组合框中选择时不会更改字体
【发布时间】:2017-10-06 14:34:31
【问题描述】:

此代码是关于用户在文本字段中插入文本并将文本传输到标签,然后用户可以在JComboBox 中选择字体样式,如果用户选择字体,则显示的文本将更改字体。

package hw;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class HW {


public static void main(String[] args) {

/*from this code is adding the frame, label, textfield, panels, panel background color and the location of the labels and textfields*/

    String [] cb =  {"Comic Sans MS", "Times New Roman", "Arial Black"};
    JFrame frames = new JFrame();
    frames.setVisible(true);
    frames.setSize(700, 500);
    frames.setResizable(false);
    frames.setLocation(170, 100);
    JPanel panels = new JPanel();
    frames.add(panels);
    panels.setBackground(new Color(40, 136, 168));
    panels.setLayout(null);
    JTextField tf1 = new JTextField();
    panels.add(tf1);
    tf1.setBounds(90, 150, 100, 25);
    JLabel label1 =  new JLabel("ENTER TEXT");
    panels.add(label1);
    label1.setBounds(100, 30, 150, 100);

    JLabel label2 = new JLabel("FONT STYLE");
    panels.add(label2);
    label2.setBounds(400, 30, 150, 100);
    JComboBox combo = new JComboBox(cb);
    panels.add(combo);
    combo.setBounds(400, 150, 150, 25);

    JLabel label3 = new JLabel("");
    panels.add(label3);
    label3.setBounds(310, 250, 150, 100);
    label3.setText("");

 /* this part below is the itemlistener and itemevent, i dont know the if this part below is correct because the font in the inserted text wont change but the text being insert in textfield is showing up in the jlabel*/

    combo.addItemListener(new ItemListener() {

    @Override
    public void itemStateChanged(ItemEvent event){
       String word;

       if (event.getStateChange()==ItemEvent.SELECTED){

       label3.setText(word=tf1.getText());
       label3.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
       }

       else if (event.getStateChange()==ItemEvent.SELECTED) {
       label3.setText(word=tf1.getText());
       label3.setFont(new Font("Times New Roman", Font.PLAIN, 14));
       }

       else if (event.getStateChange()==ItemEvent.SELECTED) {
       label3.setText(word=tf1.getText());
       label3.setFont(new Font("Arial Black", Font.PLAIN, 14));
       }

   /* the else and else if statement is not working, i dont know how to correct this problem*/    
       }
    }
    });
}

}

我无法纠正这个问题,我不知道问题的主要根源在哪里,为什么在JComboBox 中选择字体不会改变。

【问题讨论】:

  • 您是否向侦听器添加了任何调试代码?向每个 if 条件添加一个简单的 System.out.println(...) 语句,以查看正在执行的代码块。我不知道为什么你有 3 个具有相同 if 条件的 if 语句。
  • 我将 joptionpane 放在每个 if else 语句的末尾。仅当语句执行时,其他语句不执行..
  • panels.setLayout(null); 1) Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 2) 为了尽快获得更好的帮助,请发帖minimal reproducible exampleShort, Self Contained, Correct Example

标签: java swing fonts jlabel jcombobox


【解决方案1】:

这修复了itemStateChanged 方法中的多个逻辑问题(并且适用于每种字体)。我通常会为组合框使用ActionListener,但 YMMV。

    combo.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent event) {
            String fontName = combo.getSelectedItem().toString();

            if (event.getStateChange() == ItemEvent.SELECTED) {
                label3.setText(tf1.getText());
                label3.setFont(new Font(fontName, Font.PLAIN, 14));
            } 
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多