简体   繁体   English

限制所选JToggleButton的数量

[英]Limit the number of selected JToggleButton

I'm new on swing java, I maked an array of jtoggle buttons and my problem is that I want to limit the number of selected(toggled) buttons for 4 toggled buttons. 我是摇摆java的新手,我制作了一系列jtoggle按钮,我的问题是我想限制4个切换按钮的所选(切换)按钮的数量。 Is there any property that allows me to do that ? 是否有任何财产可以让我这样做? Here is my code example. 这是我的代码示例。

package adad;

import java.awt.*; import java.awt.event.*; 
import javax.swing.*;
public class essayer extends JFrame
{
 private JToggleButton jb_essai[] = new JToggleButton[6];

 JButton pressme = new JButton("Press Me");
 essayer()        // the frame constructor
 {
super("Toggle boutons");
setBounds(100,100,300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
JPanel pane = getContainer();
con.add(pane);    
setVisible(true);
 }

 class ToggleAction implements ActionListener
{
private JToggleButton jb_essai[];
public ToggleAction(JToggleButton a_jb_essai[])
{
  jb_essai = a_jb_essai;
}

public void actionPerformed(ActionEvent e)
{
  String etatBoutons = "";
  int size = jb_essai.length;

  for(int i=0;i<size;i++)

  {
    String tmp = "Bouton "+(i+1)+" : ";
    if(jb_essai[i].isSelected()==true  )
    {
      tmp+="enfonce";
    }
    else
    {
      tmp+="relache";
    }
    tmp+="\n";
    etatBoutons +=tmp; 
  }
  System.out.println(etatBoutons+"\n---------");
     }

   }
private JPanel getContainer() 
 {

GridLayout thisLayout = new GridLayout(6,2);
JPanel container = new JPanel();
ToggleAction tga = new ToggleAction(jb_essai);
container.setLayout(thisLayout);
int j=6;
for (int i=0;i<j;i++)
{
  String s = String.valueOf(i+1);

  container.add(jb_essai[i]= new JToggleButton(s)); // actuellement tt s'affiche sur un   même colone.
  jb_essai[i].addActionListener(tga);

}
return container;
}

 public static void main(String[] args) {new essayer();}
  }

Is there any property that allows me to do that ? 是否有任何财产可以让我这样做?

No. There is a ButtonGroup that allows 'one of many'. 一个ButtonGroup ,允许“的一处”。 But that is 1, not N of many. 但是,这是1,没有多少 Anything beyond that you'll need to code yourself. 除此之外,您还需要自己编码。

Is there any property that allows me to do that ? 是否有任何财产可以让我这样做?

No, you need to write your own code. 不,你需要编写自己的代码。

Add a common ItemListener to every toggle button. 为每个切换按钮添加一个公共ItemListener。 Then when a button is selected you loop though your toggle button array to count the number of selected toggle buttons. 然后,当选择一个按钮时,您可以通过切换按钮阵列循环来计算所选切换按钮的数量。

If the count is greater than 4 then you display a JOptionPane with an error message and your reset the last selected button to be unselected. 如果计数大于4,则显示带有错误消息的JOptionPane,并重置最后选择的按钮以取消选择。 You can use the getSource() method of the ItemListener to get the toggle button. 您可以使用ItemListener的getSource()方法来获取切换按钮。

Or maybe you can extend the ButtonGroup class to implement similar behaviour. 或者您可以扩展ButtonGroup类以实现类似的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM