【问题标题】:Creating panels with radio buttons that change text using arrays创建带有使用数组更改文本的单选按钮的面板
【发布时间】:2011-05-07 10:59:39
【问题描述】:

我目前对如何使用数组完成作业感到困惑。我浏览了论坛并使用了谷歌,但运气不佳。我真的很感激有人可以提供的任何建议。非常感谢!:

  1. 为我的 2 组单选按钮以及字体大小和字体创建数组

  2. 删除对单个 JRadioButtons(style1、style2、...size1、size2 等)的所有引用。相反,必须使用数组值。修改完所有代码后,我只需要在一个地方更改程序即可更改大小的所有方面。例如,更改字体数组中的值应更改字体大小选项和显示该字体大小的标签。

  3. JRadioButton 数组的大小应该与 int 和 String 数组的长度直接相关。因此,如果我有六种不同的尺寸可供选择,那么您的数组中应该有六个 JRadioButtons。

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

}

【问题讨论】:

  • 我还没有删除对 style1、style2、.... 按钮的引用,因为我不想破坏代码,尽管我知道我必须这样做。我总是看着它,希望能从旧代码中点击一些东西,我会知道用什么来替换它,但这永远不会发生..
  • 另外,这应该被标记为“作业”

标签: java arrays radio-button actionlistener


【解决方案1】:

首先,您可以对对象执行任何操作,您可以使用该对象类型的数组值执行操作。因此,如果您有一个 JRadio 按钮数组,并且 RadioButton.addActionListener(this) 是合法调用,则 Array[0].addActionListener(this) 也是合法调用(这是伪代码,而不是 Java)

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

鉴于此,您似乎可以使用诸如大小/字体类型之类的值数组并遍历它们来创建您的按钮。如果数组中有 10 个尺寸,则可以这样做

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

或者类似的东西。我认为迭代值以创建/修改对象是这里正在寻找的。​​p>

【讨论】:

  • 谢谢,这听起来像是开始编码的好地方。我会努力的。
【解决方案2】:

您可以像这样从字体数组创建它们:

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

大小数组的过程相同,我只是省略了它以保持简短。

【讨论】:

  • 这正是我需要做的,谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 2015-08-30
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
相关资源
最近更新 更多