2013-09-26 39 views
0

我目前遇到了我的最新項目的問題。 我是個初學者,所以請記住這一點:dItemListener複選框按預期工作

我想要什麼我的項目做的是改變一些文字的,如果複選框被選中的字體,並使其返回到正常的,如果它被取消選中。

我已經設法實現了。

但是,有多行復選框。如果選擇了2,我取消選擇其中一個,文本將變回正常狀態。只要列中的複選框仍處於選中狀態,我不希望文本變爲正常。

我該怎麼做? =(

希望你可以幫我!

import java.awt.Color; 

public class WindowBuilderTest extends JFrame 
{ 

private JPanel contentPane; 

//Launch the application. 
public static void main(final String[] args) 
{ 
    EventQueue.invokeLater(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      try 
      { 
       final WindowBuilderTest frame = new WindowBuilderTest(); 
       frame.setVisible(true); 
      } 
      catch (final Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

// Creating Frame 
public WindowBuilderTest() 
{ 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 1280, 720); 
    this.contentPane = new JPanel(); 
    this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(this.contentPane); 
    this.contentPane.setLayout(null); 

    // Create Font 
    final Font headfont = new Font("Serif", Font.PLAIN, 15); 
    final Font headfontRed = new Font("Serif", Font.PLAIN, 15); 

    // Role Headlines 
    final JTextArea txtTop = new JTextArea(); 
    txtTop.setEditable(false); 
    txtTop.setText("TOP"); 
    txtTop.setBounds(180, 95, 32, 23); 
    this.contentPane.add(txtTop); 

    final JTextArea txtMid = new JTextArea(); 
    txtMid.setEditable(false); 
    txtMid.setText("MID"); 
    txtMid.setBounds(252, 95, 32, 23); 
    this.contentPane.add(txtMid); 

    final JTextArea txtJng = new JTextArea(); 
    txtJng.setEditable(false); 
    txtJng.setText("JNG"); 
    txtJng.setBounds(320, 95, 32, 23); 
    this.contentPane.add(txtJng); 

    final JTextArea txtAdc = new JTextArea(); 
    txtAdc.setEditable(false); 
    txtAdc.setText("ADC"); 
    txtAdc.setBounds(389, 95, 32, 23); 
    this.contentPane.add(txtAdc); 

    final JTextArea txtSup = new JTextArea(); 
    txtSup.setEditable(false); 
    txtSup.setText("SUP"); 
    txtSup.setBounds(453, 95, 32, 23); 
    this.contentPane.add(txtSup); 

    // Checkbox 1st row 
    addCheckBox(183, 143, 23, 23, txtTop); 
    addCheckBox(255, 143, 23, 23, txtMid); 
    addCheckBox(322, 143, 23, 23, txtJng); 
    addCheckBox(393, 143, 23, 23, txtAdc); 
    addCheckBox(457, 143, 23, 23, txtSup); 

    // Checkbox 2nd row 
    addCheckBox(183, 200, 23, 23, txtTop); 
    addCheckBox(255, 200, 23, 23, txtMid); 
    addCheckBox(322, 200, 23, 23, txtJng); 
    addCheckBox(393, 200, 23, 23, txtAdc); 
    addCheckBox(457, 200, 23, 23, txtSup); 

    // Checkbox 3nd row 
    addCheckBox(183, 257, 23, 23, txtTop); 
    addCheckBox(255, 257, 23, 23, txtMid); 
    addCheckBox(322, 257, 23, 23, txtJng); 
    addCheckBox(393, 257, 23, 23, txtAdc); 
    addCheckBox(457, 257, 23, 23, txtSup); 
} 


private void addCheckBox(final int x, final int y, final int width, final int height, final JTextArea txtTop) 
{ 
    final JCheckBox checkBox = new JCheckBox(); 
    checkBox.setBounds(x, y, width, height); 
    checkBox.addItemListener(new FontChanger(txtTop)); 
    this.contentPane.add(checkBox); 
} 

class FontChanger implements ItemListener 
{ 
    private JTextArea textArea; 

    public FontChanger(final JTextArea textArea) 
    { 
     this.textArea = textArea; 
    } 

    @Override 
    public void itemStateChanged(final ItemEvent e) 
    { 
     if (e.getStateChange() == ItemEvent.SELECTED) 
     { 
      final Font boldFont = this.textArea.getFont().deriveFont(Font.BOLD); 
      this.textArea.setForeground(Color.RED); 
      this.textArea.setFont(boldFont); 
     } 
     else 
     { 
      final Font boldFont = this.textArea.getFont().deriveFont(Font.PLAIN); 
      this.textArea.setForeground(Color.BLACK); 
      this.textArea.setFont(boldFont); 
     } 
    } 
} 

}

回答

0

有實現你想要什麼嘗試做的,但我相信這個問題是比較概念的幾種方式。每個複選框要添加到您的面板有* 自己 * FontChanger。每個那些FontChangers的不知道其他FontChangers或複選框對這一問題。你可以做什麼,而不是,是有一個單一FontChanger添加到所有的複選框然後有一個letti的方式你的單個FontChanger知道itemStateChanged()方法中每個複選框的狀態。通過這種方式,您可以查看是否/選擇了多少/哪個複選框並對其執行操作。

編輯:這是一個完整的例子(注意我已經廣泛地修改您的代碼並沒有測試它,這只是給你一個總體思路我也擺脫了大部分的final關鍵字的代碼。可讀性,除此之外,如果您沒有具體的理由使用不同的線程來運行程序,則不需要在新的Runnable()中運行WindowBuilderTest,請參閱註釋部分)。

import java.awt.Color; 
import java.util.*; 

public class WindowBuilderTest extends JFrame 
{ 

private JPanel contentPane; 
private FontChanger fChanger; //<--NEW 
private JTextArea txtTop; //<--NEW 
private List<CheckBox> checkBoxes; //<--NEW 

//Launch the application. 
public static void main(final String[] args) 
{ 
    EventQueue.invokeLater(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      try 
      { 
       WindowBuilderTest frame = new WindowBuilderTest(); 
       frame.setVisible(true); 
      } 
      catch (final Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    }); 

    //just use this and comment out the whole Eventqueue block. 
    /* 
    WindowBuilderTest frame = new WindowBuilderTest(); 
    frame.setVisible(true); 
    */ 
} 

// Creating Frame 
public WindowBuilderTest() 
{ 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 1280, 720); 
    this.contentPane = new JPanel(); 
    this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(this.contentPane); 
    this.contentPane.setLayout(null); 

    // Create Font 
    Font headfont = new Font("Serif", Font.PLAIN, 15); 
    Font headfontRed = new Font("Serif", Font.PLAIN, 15); 

    // Role Headlines 
    txtTop = new JTextArea(); //<--NEW 
    fChanger = new FontChanger(txtTop); //<--NEW 
    checkBoxes = new ArrayList<CheckBox>(); //<-- NEW you will need to update your imports. 

    txtTop.setEditable(false); 
    txtTop.setText("TOP"); 
    txtTop.setBounds(180, 95, 32, 23); 
    this.contentPane.add(txtTop); 

    final JTextArea txtMid = new JTextArea(); 
    txtMid.setEditable(false); 
    txtMid.setText("MID"); 
    txtMid.setBounds(252, 95, 32, 23); 
    this.contentPane.add(txtMid); 

    final JTextArea txtJng = new JTextArea(); 
    txtJng.setEditable(false); 
    txtJng.setText("JNG"); 
    txtJng.setBounds(320, 95, 32, 23); 
    this.contentPane.add(txtJng); 

    final JTextArea txtAdc = new JTextArea(); 
    txtAdc.setEditable(false); 
    txtAdc.setText("ADC"); 
    txtAdc.setBounds(389, 95, 32, 23); 
    this.contentPane.add(txtAdc); 

    final JTextArea txtSup = new JTextArea(); 
    txtSup.setEditable(false); 
    txtSup.setText("SUP"); 
    txtSup.setBounds(453, 95, 32, 23); 
    this.contentPane.add(txtSup); 

    // Checkbox 1st row 
    addCheckBox(183, 143, 23, 23, txtTop); 
    addCheckBox(255, 143, 23, 23, txtMid); 
    addCheckBox(322, 143, 23, 23, txtJng); 
    addCheckBox(393, 143, 23, 23, txtAdc); 
    addCheckBox(457, 143, 23, 23, txtSup); 

    // Checkbox 2nd row 
    addCheckBox(183, 200, 23, 23, txtTop); 
    addCheckBox(255, 200, 23, 23, txtMid); 
    addCheckBox(322, 200, 23, 23, txtJng); 
    addCheckBox(393, 200, 23, 23, txtAdc); 
    addCheckBox(457, 200, 23, 23, txtSup); 

    // Checkbox 3nd row 
    addCheckBox(183, 257, 23, 23, txtTop); 
    addCheckBox(255, 257, 23, 23, txtMid); 
    addCheckBox(322, 257, 23, 23, txtJng); 
    addCheckBox(393, 257, 23, 23, txtAdc); 
    addCheckBox(457, 257, 23, 23, txtSup); 
} 


private void addCheckBox(int x, int y, int width, int height) 
{ 
    JCheckBox checkBox = new JCheckBox(); 
    checkBoxes.add(checkBox); // <-- NEW (add to checkboxes list) 
    checkBox.setBounds(x, y, width, height); 
    checkBox.addItemListener(fChanger); // <-- NEW (add the same listener to all) 
    this.contentPane.add(checkBox); 
} 

class FontChanger implements ItemListener 
{ 
    private JTextArea textArea; 

    public FontChanger(JTextArea textArea) 
    { 
     this.textArea = textArea; 
    } 

    @Override 
    public void itemStateChanged(ItemEvent e) 
    { 
     if (e.getStateChange() == ItemEvent.SELECTED) 
     { 
      Font boldFont = this.textArea.getFont().deriveFont(Font.BOLD); 
      this.textArea.setForeground(Color.RED); 
      this.textArea.setFont(boldFont); 
     } 
     else 
     { 
      //iterate through list of checkboxes to see if there's still one selected. 
      boolean stillOneCheckBoxSelected = false; 
      for(int i = 0; i < checkBoxes.size(); i++){ 
       if(checkBoxes.get(i).isSelected()){ // <- Verify the method here, not sure 
        stillOneCheckBoxSelected = true; 
        break; 
       } 
      } 
      if(!stillOneCheckBoxSelected){//if no check boxes are selected, reset 
       Font boldFont = this.textArea.getFont().deriveFont(Font.PLAIN); 
       this.textArea.setForeground(Color.BLACK); 
       this.textArea.setFont(boldFont); 
      } 
     } 
    } 
} 

再一次,我沒有測試過這個,你可能需要修補東西,但我認爲你可以從這裏繼續。 這裏是對的JavaDoc的ArrayList的鏈接,你可能想諮詢的是:ArrayList JavaDoc

第二個編輯: 我重讀你的問題,你真正想要這個邏輯,只有一組特定的列,所以您必須更改邏輯以將相關複選框添加到列表中,以便每次執行檢查時只檢查相關的複選框。

+0

感謝您的回答:)我瞭解一般概念,但我不確定自己能夠如何實現它。你能通過給我一個粗略的起點來幫助我嗎? – Gomox

+0

你的WindowBuilderTest類應該有一個'private FontChanger fChanger;'變量。在你的構造函數開始時,你實例化了這樣的變量: '..//角色標題 final JTextArea txtTop = new JTextArea(); fChanger = new FontChanger(txtTop); ..' 然後,您繼續添加您的複選框與相同的addCheckBox()方法,但相反,您的方法將看起來像: 'private void addCheckBox [..] final JCheckBox checkBox =新的JCheckBox(); 複選框。setBounds(x,y,width,height); checkBox.addItemListener(fChanger); ..' 然後改變你的itemStateChanged()。 – schwarz

+0

如果這仍然不清楚,我可以更新我以前的答案與更多的細節。 – schwarz