2017-10-06 94 views
1

這個代碼是關於用戶在文本字段中插入文本傳輸文本標記,然後用戶可以選擇在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爲什麼選擇字體不會改變的主要來源。

+3

您是否向偵聽器添加了任何調試代碼?爲每個if條件添加一個簡單的System.out.println(...)語句以查看哪個代碼塊正在執行。我不知道爲什麼你有3條if條件相同的語句。 – camickr

+0

我把joptionpane放在每個if else語句的末尾。只有if語句執行,其他人不.. –

+0

'panels.setLayout(NULL);'1)Java的圖形用戶界面在不同的地區使用不同的PLAFs在不同的操作系統」,屏幕大小,屏幕分辨率等工作。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 2)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

回答

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)); 
      } 
     } 
    }); 
+0

我明白了。我的代碼有什麼問題..我記住這一點.. –

相關問題