ボタンが2つあるプログラムの作成には助けが必要です。ボタンをクリックするたびに「n回クリックしました!」というメッセージが表示されます。各ボタンには個別のクリック数が必要です。ボタンカウンターJava
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ButtonViewer
{
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 360;
public static void main(String[] args)
{
int counter1 = 0;
int counter2 = 0;
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
frame.add(button);
JFrame frame2 = new JFrame();
JButton button2 = new JButton("Click me too!");
frame2.add(button2);
ActionListener listener = new ClickListener();
button.addActionListener(listener);
button2.addActionListener(listener);
counter1++;
counter2++;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame2.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
}
}
これは宿題のようです... –
あなたの宿題はしませんが、お手伝いします。物事を試して、あなたが立ち往生している場所を教えてください。そして、あなたがそれを明確にしていれば、あなたを正しい方向に押し進めます。 – switz
問題を解決するために何か試しましたか? –