Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 动态按钮单击更新其他按钮_Java_Swing_Netbeans_Jbutton - Fatal编程技术网

Java 动态按钮单击更新其他按钮

Java 动态按钮单击更新其他按钮,java,swing,netbeans,jbutton,Java,Swing,Netbeans,Jbutton,如何动态创建8*8 JButton板 . 还有3个按钮(A、B、C、D) 然后我如何使用ActionListNet,以便用户在单击“按钮”时,板中的某些按钮将更改其背景 我只知道如何更改我单击的按钮,但不知道如何更改其他按钮!!!?? 我怎么做到的 package test; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.aw

如何动态创建8*8 JButton板 . 还有3个按钮(A、B、C、D)

然后我如何使用ActionListNet,以便用户在单击“按钮”时,板中的某些按钮将更改其背景

我只知道如何更改我单击的按钮,但不知道如何更改其他按钮!!!?? 我怎么做到的

    package test;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Random;

    public class test2 extends javax.swing.JFrame implements ActionListener {


     public JPanel contentPane;
     public  int  GameBoardWidth = 800;
     public  int  GameBoardHeight = 900;
     public  int  ReservedHeight = 100;

     public final int  ButtonRows = 5;
     public final int  ButtonColumns = 5;
     private int ButtonCount = 0;

    private JButton[] GameControlButtons = new JButton[4];
    public test2() {
        initComponents();
              this.contentPane = new JPanel();
        this. contentPane.setOpaque(true);
        this.contentPane.setBackground(Color.LIGHT_GRAY);
        this.contentPane.setLayout(null);
        this.makeGameBoard();
         this.setContentPane(contentPane);
        this.setSize(GameBoardWidth, GameBoardHeight);
        this.setLocationByPlatform(true);
        this.setVisible(true);            
    }

     public void makeGameBoard()
    {
         // Splatter all the buttons
         for (int i = 0; i < this.ButtonRows; i++)
         {
              for (int j = 0; j < this.ButtonColumns; j++)
              {    
                     this.MakeGameButton(i, j);
                     //this.MakeGameButton3(i, j);
              }
         }             
         // Make  Game Control Buttons
          for (int i = 0; i < 4; i++)
          {
                this.MakeGameControlButton(i);
          }
    }
     public void MakeGameButton(int X, int Y)
    {
             int ButtonWidth = this.GameBoardWidth/this.ButtonColumns;
             int ButtonHeight = this.GameBoardHeight/this.ButtonRows;
             ButtonWidth -= 3;
             ButtonHeight -= 25;                 
            JButton button = new JButton();
            button.setName("GameButton," + X +"," + Y);
            button.setSize(ButtonWidth, ButtonHeight);
            Font myFont = new Font("Serif", Font.BOLD, 36);
            button.setFont(myFont);
            // Generate Random Number for button
             Random Rn = new Random();                 
             button.setText("");
             button.setToolTipText(Integer.toString(Rn.nextInt(11)));                 
            // Compute Button Location
            int XCoor =  X * ButtonWidth;
            int YCoor = (this.ReservedHeight/2) + Y * ButtonHeight;                
            button.setLocation(XCoor, YCoor);    
            //Add action listener to button
            button.addActionListener(this);
            this.contentPane.add(button);
    }
      public void MakeGameControlButton(int X)
    {
             int ButtonWidth = 2 * this.GameBoardWidth/this.ButtonColumns;
             int ButtonHeight = this.GameBoardHeight/this.ButtonRows;
             ButtonWidth -= 5;
             ButtonHeight -= 25;                 
            JButton button = new JButton();
            button.setName("GameControlButton" + X);
            button.setSize(ButtonWidth, ButtonHeight);                
            button.setBackground(Color.cyan);                
            Font myFont = new Font("Serif", Font.BOLD, 48);
            button.setFont(myFont);                
            // Compute Button Location
            int XCoor =  X * ButtonWidth;
            int YCoor =  this.GameBoardHeight - (ButtonHeight + 60);                
            button.setLocation(XCoor, YCoor);    
            //Add action listener to button
            button.addActionListener(this);                
            GameControlButtons[ButtonCount] = button;
            ButtonCount++;    
            this.contentPane.add(button);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new test2().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
封装测试;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Random;
公共类test2扩展了javax.swing.JFrame,实现了ActionListener{
公共JPanel内容窗格;
公共int GameBoardWidth=800;
公共游戏板高度=900;
公共int ReservedHight=100;
公共最终int按钮箭头=5;
公共最终int按钮列=5;
私有int按钮计数=0;
私有JButton[]游戏控制按钮=新JButton[4];
公共测试2(){
初始化组件();
this.contentPane=new JPanel();
this.contentPane.setOpaque(true);
此.contentPane.setBackground(颜色.浅灰色);
this.contentPane.setLayout(null);
这个.makeGameBoard();
此.setContentPane(contentPane);
此.setSize(游戏板宽度、游戏板高度);
此.setLocationByPlatform(true);
此.setVisible(true);
}
公开作废makeGameBoard()
{
//飞溅所有的按钮
for(int i=0;ipublic JButton MakeGameButton(int X, int Y) {
    JButton button = new JButton();

    ....

    return button;
}
JButton button1 = MakeGameButton(...);
JButton button2 = MakeGameButton(...);

JButton buttonA = new JButton("A Button");

buttonA.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
         button1.setBackground(...);
    }
});