Domanda

Attualmente sto perplesso su come utilizzare le matrici per completare un incarico. Ho guardato attraverso il forum e ho usato google con poca fortuna. Vorrei davvero apprezzare qualsiasi consiglio che qualcuno può risparmiare. Grazie mille!

  1. Crea gli array per i miei 2 set di pulsanti di opzione, nonché per le dimensioni dei caratteri e per i font

  2. Rimuovi tutti i riferimenti ai singoli JRadioButtons (style1, style2, .... size1, size2, etc.). Invece, devono essere utilizzati valori di matrice. Una volta che hai finito di modificare tutto il codice dovrei avere solo cambiare il programma in un unico luogo per cambiare tutti gli aspetti della dimensione. Ad esempio, la modifica di un valore nella propria matrice carattere deve cambiare l'opzione dimensione carattere E l'etichetta che mostra che la dimensione del carattere.

  3. Le dimensioni dei vostri array JRadioButton dovrebbe essere direttamente proporzionale alla lunghezza del vostro int e array di stringhe. Quindi, se ho sei formati diversi tra cui scegliere, si dovrebbe avere sei JRadioButtons nella propria matrice.

  4. Prova il tuo codice con l'aggiunta di due stili di font aggiuntivi per l'array stili.

  5. Prova il tuo codice con l'aggiunta di due grandezze per l'array dimensioni.

  6. Si potrebbe essere necessario modificare il metodo actionPerformed che corrisponda a quello dall'esempio JRadioButtons nel Capitolo 5, dove si controlla per un evento specifico.

Quindi, nei miei due array che non ho già inizializzazione, ho fatto loro dimensioni 7 per permettermi di aggiungere altri due dimensioni e due tipi di carattere. Stumped su come chiamare l'indice della matrice nel ActionListener. Quale sarebbe un buon tutorial per lavorare con i pulsanti di opzione e gli array?

Ecco il mio codice corrente:

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);
 }

}
È stato utile?

Soluzione

È possibile crearli dalla matrice di carattere in questo modo:

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);
 }
}

Stessa procedura per l'array dimensioni, ho appena lasciato fuori per essere breve.

Altri suggerimenti

Per cominciare, qualsiasi cosa si può fare con un oggetto, si potrebbe fare con un valore di matrice di quel tipo di oggetto. Quindi, se si dispone di una serie di pulsanti JRadio, e RadioButton.addActionListener (questo) è una chiamata legale, Array [0] .addActionListener (questo) è una chiamata legale, come sarebbe (questo è psuedocodarlo, non Java)

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

Dato che, sembra che si potrebbe usare array di valori come la dimensione del testo / font e iterare attraverso di loro per creare i pulsanti. Se si dispone di 10 formati in una matrice, si potrebbe fare

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

O qualcosa del genere. Penso che scorrendo i valori al fine di creare / modificare gli oggetti è ciò che viene cercato qui.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top