2010-11-09 43 views
1

我目前難以掌握如何使用數組來完成任務。我已經瀏覽了論壇,並使用谷歌運氣不錯。我真的很感謝有人可以提供的任何建議。非常感謝你!:使用單選按鈕創建使用數組更改文本的面板

  1. 我的2臺單選按鈕,以及對字體大小,字型

  2. 刪除個人JradioButton將(STYLE1,藍紫魅力的所有引用創建陣列。 ... size1,size2等)。相反,必須使用數組值。一旦你完成了所有的代碼的修改,我只需要在一個地方改變程序來改變大小的所有方面。例如,更改字體數組中的值應該更改字體大小選項和顯示該字體大小的標籤。

  3. JRadioButton數組的大小應該與int和String數組的長度直接相關。所以如果我有六種不同的尺寸可供選擇,你應該在你的陣列中有六個JRadioButton。

  4. 通過向您的樣式數組添加兩個附加字體樣式來測試您的代碼。

  5. 通過在您的大小數組中添加兩個額外的大小來測試您的代碼。

  6. 您可能需要修改actionPerformed方法以與第5章中檢查特定事件的JRadioButtons示例相匹配。

因此,在我的兩個數組中,我還沒有初始化,我將它們的大小設置爲7,以允許我添加兩個大小和兩個字體。我難以理解如何在actionListener中調用數組的索引。用單選按鈕和數組工作會是一個很好的教程?

這裏是我當前的代碼:

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

public class StyleArrays extends JPanel implements ActionListener 
{ 
//declare the labels, panels, variables and radio buttons 

private JLabel saying; 
private JRadioButton style1, style2, style3, style4; 
private JRadioButton size1, size2, size3, size4; 
private JPanel top, right, left; 

JRadioButton[] size = new JRadioButton[7]; 
JRadioButton[] font = new JRadioButton[7]; 
String[] fonts = {"Arial", "Thonburi", "Rockwell", "Century Gothic"}; 
int[] sizes = {18, 22, 26, 30}; 

//declare the variables used later in the code the set the font and style 
//private String myFont = "Arial"; 
//private int size = 18; 

//Constructor 
//----------------------------------------------------------------- 
public StyleArrays() 
{ 

    //set the layout of the Layouts panel that will contain all of the other panels 
    setLayout (new BorderLayout()); 
    setBackground (Color.red); 

    //create the new panels that will go inside the Layouts panel 
    top= new JPanel(); 
    right= new JPanel(); 
    left= new JPanel(); 

    saying = new JLabel ("Say it with style!"); 
// saying.setFont (new Font (myFont, Font.PLAIN, size)); 

    //set the layout and color of the top panel, and add the saying 
    add(top, BorderLayout.NORTH); 
    top.setBackground (Color.yellow); 
    top.add(saying); 


    //create size radio buttons 
    size1 = new JRadioButton ("18", true); 
    size1.setBackground (Color.red); 
    size2 = new JRadioButton ("22"); 
    size2.setBackground (Color.red); 
    size3 = new JRadioButton ("26"); 
    size3.setBackground (Color.red); 
    size4 = new JRadioButton ("30"); 
    size4.setBackground (Color.red); 

    //add listeners for each size buttons 

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

    //add these buttons to this button group 
    ButtonGroup group1 = new ButtonGroup(); 
    group1.add (size1); 
    group1.add (size2); 
    group1.add (size3); 
    group1.add (size4); 


    //set the layout and color of the left panel 
    add(left, BorderLayout.WEST); //add the left panel to the border layout 
    left.setLayout (new BoxLayout (left, BoxLayout.Y_AXIS)); //set the layout of left to box layout 
    left.setBackground (Color.red); //set the color to red 

    //display the buttons on the panel 
    left.add (size1); 
    left.add (size2); 
    left.add (size3); 
    left.add (size4); 

    //This section deals with the panel that contains the STYLE information 

    add(right, BorderLayout.EAST); //add the right panel to the border layout 
    right.setLayout (new GridLayout (2, 2)); //set the layout of right panel to grid layout 
    right.setBackground (Color.red); // set the background color of the panel to red 

    //create style radio buttons 
    style1 = new JRadioButton ("Arial", true); 
    style1.setBackground (Color.red); 
    style2 = new JRadioButton ("Thonburi"); 
    style2.setBackground (Color.red); 
    style3 = new JRadioButton ("Rockwell"); 
    style3.setBackground (Color.red); 
    style4 = new JRadioButton ("Century Gothic"); 
    style4.setBackground (Color.red); 


    //add listeners for each style button 
    style1.addActionListener (this); 
    style2.addActionListener (this); 
    style3.addActionListener (this); 
    style4.addActionListener (this); 

    //add these buttons to this button group 
    ButtonGroup group2 = new ButtonGroup(); 
    group2.add (style1); 
    group2.add (style2); 
    group2.add (style3); 
    group2.add (style4); 

    //display the buttons on the panel 
    right.add (style1); 
    right.add (style2); 
    right.add (style3); 
    right.add (style4); 


} 

//***************************************************************** 
// Represents the listener for both check boxes. 
//***************************************************************** 


//***************************************************************** 
// Represents the listener for the radio buttons. 
//***************************************************************** 
public void actionPerformed (ActionEvent event) 
{ 

    Object source = event.getSource(); 
    if (source == size1) //if the event is that the size1 button is selected 
    size = 18; //assign 18 to the variable 

    if (source == size2) 
    size = 22; 

    if (source == size3) 
    size = 26; 

    if (source == size4) 
    size = 30; 

    if (source == style1) 
    myFont = "Arial"; 

    if (source == style2) 
    myFont = "Thonburi"; 

    if (source == style3) 
    myFont = "Rockwell"; 

    if (source == style4) 
    myFont = "Century Gothic"; 

    saying.setFont (new Font (myFont, Font.PLAIN, size)); //display the saying with the current value of 'myFont' and 'style' 

} 

public static void main (String[] args) 
{ 
    JFrame frame = new JFrame ("Style Arrays"); 
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

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

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

} 
+0

我還沒有刪除對style1,style2,....按鈕的引用,因爲我不想破壞代碼,儘管我知道我必須這樣做。我總是看着它希望從舊代碼中點擊一下,我就知道該用什麼來代替它,但從來沒有發生過.. – Josh 2010-11-09 19:24:41

+0

此外,這應該被標記'家庭作業' – philosodad 2010-11-10 04:24:45

+1

@philosodad:[「家庭作業標籤,像其他所謂的'元'標籤,現在不鼓勵。」](http://meta.stackexchange.com/q/10812 ) – 2010-11-10 16:38:53

回答

1

您可以從字型陣列像這樣創建它們:

public class StylePanel extends JPanel implements ActionListener { 
String[] fontArray = {"Arial", "Serif", "Courier", "Consolas"}; 
JLabel label; 

JRadioButton[] fontButtons = new JRadioButton[fontArray.length]; 
ButtonGroup fontGroup = new ButtonGroup(); 

public StylePanel() { 
    setLayout(new FlowLayout()); 
    label = new JLabel("Hello"); 
    add(label); 
    for(int i = 0; i < fontButtons.length; i++) { 
    fontButtons[i] = new JRadioButton(); 
    fontButtons[i].setText(fontArray[i]); 
    fontButtons[i].addActionListener(this); 
    fontGroup.add(fontButtons[i]); 
    add(fontButtons[i]); 
    } 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    label.setFont(new Font(
    ((JRadioButton)e.getSource()).getText(), Font.PLAIN, 15)); 
} 

public static void main(String[] args) { 
    StylePanel panel = new StylePanel(); 
    JFrame frame = new JFrame(); 
    frame.getContentPane().add(panel); 
    frame.setSize(400, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
} 
} 

的大小的數組的相同的過程,我剛剛離開它,以保持它短。

+0

這正是我需要做的,謝謝你的幫助! – Josh 2010-11-10 15:57:25

3

對於初學者來說,什麼都可以用一個對象做,你可以做與該對象類型的數組值。所以如果你有一個JRadio按鈕的數組,並且RadioButton.addActionListener(this)是一個合法的調用,Array [0] .addActionListener(this)是一個合法的調用,這將是(這是psuedocode,而不是Java)

for each index i of Array 
    Array[i].addActionListener(this) 

鑑於此,它看起來像你可以使用大小/字體類型的值的數組,並遍歷它們來創建您的按鈕。如果你有一個陣列10個尺寸,你可以做

for each index i of Sizes 
    Array[i] = new JRadioButton(i) 

或沿着這些線的東西。我認爲爲了創建/修改對象而循環訪問值是在這裏查找的內容。

+0

謝謝,這聽起來像是一個開始編碼的好地方。我會在這方面努力。 – Josh 2010-11-09 19:59:42