2012-08-22 67 views
1

我做一個簡單的文本編輯器,你可以設置字體樣式,字體大小,清除所有等設置,我添加的JComboBox和實施的ItemListener字體大小。這是我的主窗口類:JComboBox的getSelectedIndex()方法

import javax.swing.*; 

public class MainWindow extends JFrame{ 
Editor e = new Editor(); 

public MainWindow(){ 
    super(".:My Text Editor:."); 
    getContentPane().add(e); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    pack(); 
    setVisible(true); 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable(){ 
     public void run() { 
      new MainWindow(); 
     } 
    }); 

} 

} 

這裏是我的編輯器類:

import javax.swing.*; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 

public class Editor extends JPanel{ 
JPanel optionPanel = new JPanel(); 
JTextArea editArea = new JTextArea(); 
JButton boldBtn = new JButton("Bold"); 
JButton italicBtn = new JButton("Italic"); 
JButton plainBtn = new JButton("Plain"); 
JButton clearBtn = new JButton("Clear all"); 
String [] fontSizes = {"10","11","12","13","14","15","16","17","18","19","20"}; 
int fontSize; 
JComboBox combo = new JComboBox(fontSizes); 

public Editor(){ 
    createUI(); 
    addEvents(); 
} 

public void createUI(){ 
    optionPanel.add(boldBtn); 
    optionPanel.add(italicBtn); 
    optionPanel.add(plainBtn); 
    optionPanel.add(combo); 
    optionPanel.add(clearBtn); 

    setLayout(new BorderLayout()); 
    add(optionPanel,BorderLayout.NORTH); 
    add(new JScrollPane(editArea),BorderLayout.CENTER); 
    setPreferredSize(new Dimension(640,480)); 
} 

public void addEvents(){ 

    boldBtn.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      editArea.setFont(new Font("Sans Serif",Font.BOLD,fontSize)); 
     } 
    }); 

    italicBtn.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      editArea.setFont(new Font("Sans Serif",Font.ITALIC,fontSize)); 

     } 
    }); 

    plainBtn.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize)); 
     } 
    }); 

    combo.addItemListener(new ItemListener(){ 

     public void itemStateChanged(ItemEvent e){ 
      int ind = combo.getSelectedIndex(); 
      System.out.println(ind); 
      fontSize = Integer.parseInt(fontSizes[ind]); 
      editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize)); 
     } 
    }); 

    clearBtn.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      editArea.setText(""); 
     } 
    }); 
} 

} 

現在,奇怪的事情發生了什麼事的時候,我把的System.out.println(IND);行只是爲了看看指數getSelectedIndex()方法返回了我。根據我點擊的項目,它返回給我這個:

1 
1 
0 
0 
2 
2 
3 
3 

爲什麼會發生這種情況?不應該只返回我1 0 2 3?提前致謝。

回答

3

的JComboBox火itemStateChanged兩次,選擇並撤消選擇您使用ItemEvent.getStateChanged區分()。如果是這樣那麼換你的代碼在:

public void itemStateChanged(ItemEvent event) { 
    if(event.getStateChanged() == ItemEvent.SELECTED) { 
     // code here 
    } 
} 
+0

非常感謝,現在返回物有所值。 –

3

每當你改變的JComboBox選擇,觸發itemStateChanged事件兩次,一次爲老所選項目的取消選擇和一次選擇新選擇的項目。

如果您只想執行一次你的代碼,只是做:

if (e.getStateChange() == ItemEvent.SELECTED) { 
... 
} 
2

似乎itemStateChanged被觸發兩次。我認爲ItemEvent參數每次都不一樣。也許你應該在做某事之前檢查事件類型。

抱歉,我現在不能檢查,但以後我會做,如果你仍然需要幫助。