显示文本字段中的文本,但在组合框中选择时不会更改字体

时间:2017-10-06 14:34:31

标签: java swing fonts jlabel jcombobox

此代码是关于用户在文本字段中插入文本并将文本传输到标签,然后用户可以在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中选择字体,为什么字体不会改变。

1 个答案:

答案 0 :(得分: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));
            } 
        }
    });