2012-11-05 78 views
2

我不明白爲什麼這個Java代碼不工作。這是一個GUI的項目我的工作,我試圖讓JLabel以通過一些複選框更改爲粗體,斜體等:我的文字不變。我不明白爲什麼

import java.awt.BorderLayout; 
import java.awt.Font; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ButtonGroup; 
import javax.swing.JCheckBox; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 

public class FontViewer { 
static JCheckBox checkBoxBold; 
static JCheckBox checkBoxItalic; 
static JCheckBox checkBoxCenter; 
static JPanel textPanel; 
static JLabel textLabel; 
static JComboBox fontName; 
static JComboBox fontSize; 

static ActionListener listener; 

public static void main(String[] args) { 
    final int FRAME_SIZE_X = 250; 
    final int FRAME_SIZE_Y = 400; 

    JFrame frame = new JFrame(); 
    frame.setSize(FRAME_SIZE_X, FRAME_SIZE_Y); 

    JPanel face = new JPanel(); 
    face.setLayout(new GridLayout(2, 1)); 

    JPanel bottomFace = new JPanel(); 
    bottomFace.setLayout(new GridLayout(3, 1)); 

    textPanel = createTextPanel(); 

    JPanel checkBoxPanel = createCheckBoxPanel(); 

    JPanel comboPanel = createComboPanel(); 

    JPanel radioButtonsPanel = createButtonsPanel(); 

    face.add(textPanel); 

    bottomFace.add(checkBoxPanel); 
    bottomFace.add(comboPanel); 
    bottomFace.add(radioButtonsPanel); 

    face.add(bottomFace); 

    frame.add(face); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 

    class FontListener implements ActionListener { 
     public void actionPerformed(ActionEvent event) { 
      int fontStyle = 0; 
      if (checkBoxBold.isSelected()) 
       fontStyle = fontStyle + Font.BOLD; 
      if (checkBoxItalic.isSelected()) 
       fontStyle = fontStyle + Font.ITALIC; 
      if (checkBoxCenter.isSelected()) 
       textPanel.add(textLabel, BorderLayout.CENTER); 

      String textFont = (String) fontName.getSelectedItem(); 

      int textSize = Integer.parseInt((String) fontSize 
        .getSelectedItem()); 

      textLabel.setFont(new Font(textFont, fontStyle, textSize)); 
      textLabel.repaint(); 
     } 
    } 

    listener = new FontListener(); 
} 

private static JPanel createTextPanel() { 
    textPanel = new JPanel(); 

    textPanel.setLayout(new BorderLayout()); 
    textLabel = new JLabel("Java Text"); 
    textPanel.add(textLabel, BorderLayout.WEST); 

    return textPanel; 
} 

private static JPanel createCheckBoxPanel() { 
    JPanel checkBoxPanel = new JPanel(); 

    checkBoxBold = new JCheckBox("Bold"); 
    checkBoxItalic = new JCheckBox("Italic"); 
    checkBoxCenter = new JCheckBox("Center"); 

    checkBoxBold.addActionListener(listener); 
    checkBoxItalic.addActionListener(listener); 
    checkBoxCenter.addActionListener(listener); 

    checkBoxPanel.add(checkBoxBold); 
    checkBoxPanel.add(checkBoxItalic); 
    checkBoxPanel.add(checkBoxCenter); 

    return checkBoxPanel; 
} 

private static JPanel createComboPanel() { 
    JPanel comboPanel = new JPanel(); 

    fontName = new JComboBox(); 
    fontName.addItem("Serif"); 
    fontName.addItem("Courier"); 

    fontSize = new JComboBox(); 
    fontSize.addItem("12"); 
    fontSize.addItem("24"); 
    fontSize.addItem("36"); 

    comboPanel.add(fontName); 
    comboPanel.add(fontSize); 

    return comboPanel; 
} 

private static JPanel createButtonsPanel() { 
    JPanel radioButtonsPanel = new JPanel(); 

    JRadioButton redButton = new JRadioButton("Red"); 
    JRadioButton whiteButton = new JRadioButton("White"); 
    JRadioButton blueButton = new JRadioButton("Blue"); 

    ButtonGroup colors = new ButtonGroup(); 
    colors.add(redButton); 
    colors.add(whiteButton); 
    colors.add(blueButton); 

    radioButtonsPanel.add(redButton); 
    radioButtonsPanel.add(whiteButton); 
    radioButtonsPanel.add(blueButton); 

    return radioButtonsPanel; 
} 

}

當我按下任何複選框,JLabel對象不會更改。任何幫助非常感謝,並非常感謝你提前。

注意:截至目前,我只想知道爲什麼複選框不起作用。此代碼不完整,我意識到這一點。再一次感謝你。

+3

你確定所有的代碼是相關的嗎? – Vlad

+2

歡迎來到Stack Over Flow。請嘗試查找代碼的最小相關子集。即使100條代碼行出現問題,也很難理解。 – AlexR

+0

這甚至編譯? '類FontListener實現ActionListener {'在'main'方法內。 – jlordo

回答

4

當您將偵聽器添加到複選框時,listener的值爲null。直到主要方法的最後纔會初始化listener

+0

你們是救生員!非常感謝您的幫助!對於長碼我很抱歉:我對這個網站很陌生,並且認爲你們中的一些人可能想要自己試一試這些代碼,看看問題出在哪裏!再次謝謝你! – user1800967

+1

@ user1800967事實上,對我來說,代碼剛好夠用,它肯定是可以運行的,所以就出發了! – Jagger

+0

這是其中的一種情況,其中整個代碼允許沒有來回評論的明確答案。但是,如果問題可以簡化爲[SSCCE](http://sscce.org),那麼發佈數百行代碼通常不是一個好主意。 –

1

嘗試在創建JFrame之前初始化偵聽器。它爲我工作。當然,FontListener類的定義必須在相應的listener聲明之前進行。

//... 
listener = new FontListener(); 
JFrame frame = new JFrame(); 
//... 
相關問題