如何使单个jButton激活整行jButton?

时间:2018-05-03 22:47:52

标签: java swing jbutton

目前我可以在点击它们时激活我的所有jButton 但是我的教授要求我只点击1个jButtons来激活最后两行jButtons。

e.g 如果按下“11”jButton,则应激活整行jButton,并在最后一行显示“SUMMARY”,在最后一行显示“12”,“15”....“32”。

请使用OUTPUT图像和我的代码作为参考。提前谢谢!

OUTPUT

import java.util.List;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Collectors;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;

public class A3 extends JFrame implements ActionListener 
{
private final JButton [] jbArray;
private final JButton [] jbArray2;
private final JButton [] jbArray3;
private final GridLayout gl;

public A3 ()
{
   super ("Good Luck To You");
   gl = new GridLayout (9,7);
   setLayout(gl);

   jbArray = new JButton [49];
   jbArray2 = new JButton [7];
   jbArray3 = new JButton [7];

   for (int i = 0; i < 49; i++)
   {
       jbArray [i] = new JButton ("SP");
       add(jbArray[i]);
       jbArray[i].addActionListener(this);
   }

    String s = "SUMMARY";
    for (int j = 0; j < 7; j++)
    {
        jbArray2 [j] = new JButton (String.valueOf(s.charAt(j)));
        add (jbArray2[j]);
        jbArray2[j].addActionListener(this);
    }

    for (int l = 0; l < 7; l++)
   {
       jbArray3 [l] = new JButton ("SP");
       add(jbArray3[l]);
       jbArray3[l].addActionListener(this);
   }

   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setSize(700,700);
   setVisible(true);    

 }

 static List <Integer> aList;
 public static void forList ()
 {
   int num [] = new int [49];

   for (int j = 0; j < num.length; j++)
        num[j] = j + 1;

   aList = Arrays.stream (num)
                 .boxed ()
                 .collect (Collectors.toList());

    Collections.shuffle(aList);
 }

 static List <Integer> rList;
 public static void forRandom()
 {
   int r [] = new int [7];

    for (int i = 0; i < 7; i++)
        r [i] = (int)(Math.random() * 49);

    rList = Arrays.stream(r)
                 .boxed()
                 .distinct()
                 .sorted()
                 .collect (Collectors.toList());

 }

 @Override
 public void actionPerformed (ActionEvent e)
 {
   String first = String.valueOf(rList.get(0));

   int n = 0;  
   {
        for (JButton k : jbArray)
        {

            if (e.getSource() == k)
            {
                k.setText(String.valueOf(aList.get(n)));
                k.getText();

                //Winning prize
                for ( int i = 0; i < 7; i++)
                {   
                    String rFirst = String.valueOf(rList.get(i));
                    if(k.getText().equals(rFirst))
                    {
                        k.setBackground (Color.YELLOW);
                        k.setFont(new Font ("Serif", Font.BOLD, 19));
                    }
                }

                //Top prize
                if (k.getText().equals(first))
                {
                    k.setBackground (Color.GREEN);
                    k.setFont (new Font ("Italic", Font.BOLD, 22));
                }
            }
            ++n;
        }   
   }

   int a = 0;
   {
        for (JButton p : jbArray3)
        {
            if (e.getSource() == p)
            {
                p.setText(String.valueOf(rList.get(a)));
                p.setBackground (Color.YELLOW);
                p.setForeground(Color.BLUE);
                p.setFont(new Font ("Serif", Font.BOLD, 19));
            }  
            ++a;

            if (p.getText().equals(first))
            {
                p.setBackground(Color.GREEN);
            }

            for (JButton m : jbArray2)
            {
                if (e.getSource() == m)
                {
                    m.setForeground(Color.RED);
                    m.setFont (new Font ("Serif", Font.BOLD, 19));
                }
            }
        }
   }
  }

  public static void main (String [] args)
  {
     A3 a3 = new A3 ();
     forList ();
     forRandom();  
  }

}

1 个答案:

答案 0 :(得分:1)

所以,通过一些猜测工作,基本上如果你点击&#34;赢得&#34;按钮,这将突出显示rList中所有值的最后一行按钮。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        private List<String> rList = new ArrayList<>(7);
        private List<JButton> summary = new ArrayList<>(7);

        public TestPane() {
            setLayout(new GridLayout(0, 7));

            List<String> values = new ArrayList<String>(7 * 7);
            for (int index = 0; index < 7 * 7; index++) {
                values.add(Integer.toString(index + 1));
            }

            Collections.shuffle(values);
            rList.addAll(values.subList(0, 7));
            Collections.shuffle(values);

            System.out.println(rList);

            for (String value : values) {
                JButton btn = makeButton(value);
                btn.addActionListener(new ActionHandler(btn));
                add(btn);
            }

            String text = "SUMMARY";
            for (String value : text.split("")) {
                add(makeButton(value));
            }
            for (int index = 0; index < 7; index++) {
                JButton btn = makeButton("");
                summary.add(btn);
                add(btn);
            }
        }

        public JButton makeButton(String text) {
            JButton btn = new JButton(text);
            btn.setFocusPainted(false);
            btn.setOpaque(true);
            btn.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(4, 8, 4, 8)));
            return btn;
        }

        public class ActionHandler implements ActionListener {

            private JButton button;

            public ActionHandler(JButton button) {
                this.button = button;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                String first = rList.get(0);
                String cmd = e.getActionCommand();
                JButton btn = (JButton) e.getSource();
                System.out.println("cmd = " + cmd);
                System.out.println("first = " + first);
                if (cmd.equals(first)) {
                    btn.setBackground(Color.GREEN);
                    for (int index = 0; index < rList.size(); index++) {
                        JButton b = summary.get(index);
                        String text = rList.get(index);
                        b.setText(text);
                        if (index == 0) {
                            b.setBackground(Color.GREEN);
                        } else {
                            b.setBackground(Color.YELLOW);
                        }
                    }
                } else if (rList.contains(cmd)) {
                    btn.setBackground(Color.YELLOW);
                }
            }

        }

    }
}

相反,如果您真的需要选择所有其他值&#34;首先,你需要保持一个成功的数字&#34;匹配并比较当&#34;获胜&#34;点击了数值。