0

4 つのボタンがあり、ユーザーがこれらを一致させる必要があるプログラムを作成したいと思います。

したがって、ボタン 1、ボタン 2、ボタン 3、ボタン 4 の 4 つのボタンがあり、ユーザーがボタン 1 とボタン 3 を押すと、ボタンの色が変わります。それ以外は同じままです。

アクション リスナーとアクション リスナー内の if ステートメントを使用してみましたが、両方のボタンが押されているかどうかを確認する方法がよくわかりません。

ありがとう。

これが私のコードです:

import javax.swing.*;
import java.awt.*;
import java.awt.Color.*;
import javax.swing.Box;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.border.LineBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.BorderFactory;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;


public class test3 extends JPanel {

    JFrame frame;
    JPanel panel;

    public test3() {

    /*Frame and panel */

    frame = new JFrame("Keyboard");
    panel = new JPanel();

    /* Buttons fot letters*/
    final JButton button1 =new JButton("button1");
    final JButton button2 =new JButton("button2");
    final JButton button3 =new JButton("button3");
    final JButton button4 =new JButton("button4");

    frame.setVisible(true);
    frame.setSize(800,600);
    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); 

    panel.setLayout(null);
    panel.setBackground(Color.WHITE);
    panel.setCursor( new Cursor(Cursor.HAND_CURSOR) ); // set the cursor to a hand 

    frame.add(panel);

    Insets insets = panel.getInsets();

    button1.setLayout(null);
    button1.setBounds(130 + insets.left, 300 + insets.top, 50,50);
    button1.setBackground(Color.WHITE);
    button1.setBorder(BorderFactory.createEmptyBorder());

    button2.setLayout(null);
    button2.setBounds(180 + insets.left, 300 + insets.top,  50,50);
    button2.setBackground(Color.WHITE);
    button2.setBorder(BorderFactory.createEmptyBorder());

    button3.setLayout(null);
    button3.setBounds(230 + insets.left, 300 + insets.top,  50,50);
    button3.setBackground(Color.WHITE);
    button3.setBorder(BorderFactory.createEmptyBorder());

    button4.setLayout(null);
    button4.setBounds(280 + insets.left, 300 + insets.top,  50,50);
    button4.setBackground(Color.WHITE);
    button4.setBorder(BorderFactory.createEmptyBorder());

    panel.add(button1);
    panel.add(button2); 
    panel.add(button3);
    panel.add(button4);
    }


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

2 に答える 2

4

一度に 1 つの ActionEvent のみが発生するため、button1 または button3 が初めて押されるたびにフラグを設定する必要があります。ボタンの色、それ以外は変更しないでください。また、色の変更が完了したら、フラグを false に設定することを忘れないでください。コードは次のとおりです。

import javax.swing.*;
import java.awt.*;
import java.awt.Color.*;
import javax.swing.Box;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.border.LineBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.BorderFactory;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;

public class test3 extends JPanel implements ActionListener {

    JFrame frame;
    JPanel panel;
    boolean flag;
    String buttonPressed;
    final JButton button1;
    final JButton button2;
    final JButton button3;
    final JButton button4;
    Color color1, color2;

    public test3() {

    /*Frame and panel */

    frame = new JFrame("Keyboard");
    panel = new JPanel();
    color1 = new Color(0,0,0);

    /* Buttons fot letters*/
    button1 =new JButton("button1");
    button2 =new JButton("button2");
    button3 =new JButton("button3");
    button4 =new JButton("button4");

    color2 = button1.getBackground();

    frame.setVisible(true);
    frame.setSize(800,600);
    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); 

    panel.setLayout(null);
    panel.setBackground(Color.WHITE);
    panel.setCursor( new Cursor(Cursor.HAND_CURSOR) ); // set the cursor to a hand 

    frame.add(panel);

    Insets insets = panel.getInsets();

    button1.setLayout(null);
    button1.setBounds(130 + insets.left, 300 + insets.top, 50,50);
    button1.setBackground(Color.WHITE);
    button1.setBorder(BorderFactory.createEmptyBorder());

    button2.setLayout(null);
    button2.setBounds(180 + insets.left, 300 + insets.top,  50,50);
    button2.setBackground(Color.WHITE);
    button2.setBorder(BorderFactory.createEmptyBorder());

    button3.setLayout(null);
    button3.setBounds(230 + insets.left, 300 + insets.top,  50,50);
    button3.setBackground(Color.WHITE);
    button3.setBorder(BorderFactory.createEmptyBorder());

    button4.setLayout(null);
    button4.setBounds(280 + insets.left, 300 + insets.top,  50,50);
    button4.setBackground(Color.WHITE);
    button4.setBorder(BorderFactory.createEmptyBorder());

    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);

    panel.add(button1);
    panel.add(button2); 
    panel.add(button3);
    panel.add(button4);
    flag = false;
    buttonPressed = "";
    }

    public void actionPerformed(ActionEvent ae)
    {
    JButton b = (JButton)ae.getSource();
    if( b.equals(button1) )
        {
        flag = true;
        if( buttonPressed.equals("button3") )
            {
            if( button1.getBackground().equals(color2) )
                {
                button1.setBackground(color1);
                button3.setBackground(color1);
                }
            else
                {
                button1.setBackground(color2);
                button3.setBackground(color2);
                }
            flag = false;
            }
        buttonPressed = "button1";
        }
    else if( b.equals(button3) ) 
        {
        flag = true;
        if( buttonPressed.equals("button1") )
            {
            if( button3.getBackground().equals(color2) )
                {
                button1.setBackground(color1);
                button3.setBackground(color1);
                }
            else
                {
                button1.setBackground(color2);
                button3.setBackground(color2);
                }
            flag = false;
            }
        buttonPressed = "button1";
        }
    else
        {
        flag = false;
        }

    }

    public static void main(String[] args) {
    new test3();
    }
}
于 2013-04-13T14:03:06.183 に答える