問題があります。私は映画予約システムを実装しようとしていますが、別のグリッド上の特定のボタンにactionListenerを設定することはできません。私はそれが働きたいと思う方法は、各ユーザーは、彼が座席と学生料金、老齢年金レートなどのチケット価格を選ぶことができるセッションを持つことになります。とにかく、私はactionListenerを追加することができないように見えるので、彼が座席を選んだとき、それらはプログラムが動いている間利用できなくなります。ありがとうございました。特定のボタンを実装するグリッドレイアウトActionListener
// Load Libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class cinemaSystem {
// Global Variables
JFrame frame = new JFrame(); // Creates JFrame
JLabel title;
JButton l[][], m[][], r[][]; // Names grid of JButtons
JPanel panel1, panel2, panel3;
// Constructor
public cinemaSystem(){
title = new JLabel("Cinema Booking");
title.setFont(new Font("Helvetica", Font.BOLD, 30));
title.setLocation(12,5);
title.setSize(600, 60);
frame.setLayout(null); // Setting Grid Layout
// panel1.setLayout(new GridLayout(seat,row));
l = new JButton[4][4]; // Allocating Size of Grid
panel1 = new JPanel(new GridLayout(4,4));
panel1.setBackground(Color.black);
panel1.setBounds(20, 70, 200, 140);
for(int y = 0; y <4 ; y++){
for(int x = 0; x < 4; x++){
l[x][y] = new JButton("L" + y + x); // Creates New JButton
// l[x][y].addActionListener(this);
panel1.add(l[x][y]); //adds button to grid
}
}
m = new JButton[4][2]; // Allocating Size of Grid
panel2 = new JPanel(new GridLayout(2,4));
panel2.setBackground(Color.black);
panel2.setBounds(240, 140, 200, 70);
for(int y = 0; y <2 ; y++){
for(int x = 0; x < 4; x++){
m[x][y] = new JButton("M" + y + x); // Creates New JButton
panel2.add(m[x][y]); //adds button to grid
}
}
r = new JButton[4][4]; // Allocating Size of Grid
panel3 = new JPanel(new GridLayout(4,4));
panel3.setBackground(Color.black);
panel3.setBounds(460, 70, 200, 140);
for(int y = 0; y <4 ; y++){
for(int x = 0; x < 4; x++){
r[x][y] = new JButton("R" + y + x); // Creates New JButton
panel3.add(r[x][y]); //adds button to grid
}
}
frame.add(title);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.setPreferredSize(new Dimension(680, 280));
frame.setTitle("Cinema Booking");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
// Main Class
public static void main(String[] args) {
new cinemaSystem(); //makes new ButtonGrid with 2 parameters
}
}
+1、これはありがとう – mKorbel
JToggleButtonsのための仕事で、私は今JToggleButtonsで行くことに決めたと私は成功を収めています。どのようにボタンをトグルし、そのラベルを印刷するか。例:L00をクリックすると、L00を出力してからそれを選択します。もう一度クリックすると選択が解除され、L00 falseが表示されます。それは素晴らしいです。ありがとうございました。 – waltfy