2010-11-03 50 views
1

我正在製作3個面板的框架。 1面板上有一些文字,1有單選按鈕可以改變字詞出現的字體,最後一個面板有單選按鈕來改變字體的大小。我已經完成了一切,但我對如何爲大小單選按鈕添加動作偵聽器感到困惑。誰能幫我這個? actionlistener的代碼接近底部。非常感謝你!如何爲2組單選按鈕添加動作列表

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Layouts extends JPanel implements ActionListener 
{ 
    int style = Font.PLAIN; 
    String font = "Arial"; 
    private JLabel saying, overAll, top, sizeP, fontP; 
    private JPanel panel1, panel2, panel3, panel4; 
    private JRadioButton style1, style2, style3, style4, size1, size2, size3, size4; 
    //----------------------------------------------------------------- 
    // Sets up a panel with a label and some check boxes that 
    // control the style of the label's font. 
    //----------------------------------------------------------------- 
    public Layouts() 
    { 
     style1 = new JRadioButton ("Arial", true); 
     style2 = new JRadioButton ("Times New Roman", false); 
     style3 = new JRadioButton ("Verdana", false); 
     style4 = new JRadioButton ("Thonburi", false); 

     size1 = new JRadioButton ("36", false); 
     size2 = new JRadioButton ("24", false); 
     size3 = new JRadioButton ("20", false); 
     size4 = new JRadioButton ("16", true); 

     panel1 = new JPanel(); 
     panel2 = new JPanel(); 
     panel3 = new JPanel(); 

     saying = new JLabel ("Say it with style!"); 
     saying.setFont (new Font ("Helvetica", Font.PLAIN, 36)); // we'll need this later 

     ButtonGroup group = new ButtonGroup(); //use this code for the homework 
     group.add (style1); 
     group.add (style2); 
     group.add (style3); 
     group.add (style4); 

     style1.addActionListener (this); 
     style2.addActionListener (this); 
     style3.addActionListener (this); 
     style4.addActionListener (this); 

     ButtonGroup group2 = new ButtonGroup(); 
     group.add(size1); 
     group.add(size2); 
     group.add(size3); 
     group.add(size4); 

     size1.addActionListener (this); 
     size2.addActionListener (this); 
     size3.addActionListener (this); 
     size4.addActionListener (this); 

     setBackground (Color.red); 
     setPreferredSize (new Dimension(500, 100)); 

     setLayout(new BorderLayout()); 
     add(panel1, BorderLayout.NORTH); 
     add(panel2, BorderLayout.EAST); 
     add(panel3, BorderLayout.WEST); 

     //add(panel2, East); 
     //add(panel3, West); 

     panel1.add(saying); 
     panel1.setBackground(Color.yellow); 

     panel2.setLayout(new GridLayout()); 
     panel2.setBackground(Color.red); 
     panel2.add(style1); 
     panel2.add(style2); 
     panel2.add(style3); 
     panel2.add(style4); 

     panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS)); 
     panel3.setBackground(Color.red); 
     panel3.add(size1); 
     panel3.add(size2); 
     panel3.add(size3); 
     panel3.add(size4); 
    } 
    //***************************************************************** 
    // Represents the listener for both check boxes and the radio boxes. 
    //***************************************************************** 
    public void actionPerformed(ActionEvent e) // this is our bread and butter, it is basically what we will be changing 
    { 
     Object source = e.getSource(); 

     if (style1.isSelected()) 
      font = "Arial"; 
     else 
      if (style2.isSelected()) 
       font = "Times New Roman"; 
      else 
       if (style3.isSelected()) 
        font = "Verdana"; 
       else 
        if (style4.isSelected()) 
         font = "Thonburi"; 
     saying.setFont(new Font (font, style, 36)); 
    } 
    public static void main (String[] args) 
    { 
     JFrame frame = new JFrame ("Layouts"); 
     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

     Layouts panel = new Layouts(); 
     frame.getContentPane().add(panel); 

     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
+0

有沒有一種方法可以使用actionListener通過擴展我現有的if else語句來更改文本的大小?在看到答覆後,我認爲這就是我應該如何去做的,我還沒有足夠了解一些被稱爲能夠做到這一點的事情。 – Josh 2010-11-04 14:42:55

回答

1

您不必將style1-4硬編碼到偵聽器中。你看到你是如何得到行動的源頭的,但你從未使用過它?該源是觸發此事件的單選按鈕對象。您可以在單選按鈕上獲取該值的標籤。然後,只需根據剛剛觸發事件的單選按鈕的標籤值設置字體即可。你也可能想把它分成兩個事件處理程序,每個單選按鈕一個。使讀起來更容易,並更容易辨別出正在被點擊的集合。

1

代替

saying.setFont(new Font (font, style, 36)); 

在actionPerformed方法試試這個:

saying.setFont(new JLabel().getFont()); // get the default font from a new JLabel 
this.repaint(); 

所以似乎有什麼東西不對的字體,你想顯示。這些字體是否在你的機器上? System.out.println(new Font("Arial",style,36).getFamily());是否打印出Arial或者某物。其他?

+0

字體實際上工作,我只是不知道如何實現一個動作偵聽器的大小。當我單擊某個樣式的四個單選按鈕之一時,它會更改頂部面板上「說」的樣式。 – Josh 2010-11-03 21:32:18