2
我有問題;我試圖實現電影預訂系統,但是我無法將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,這是工作的JToggleButtons – mKorbel 2012-03-31 21:09:03
謝謝你,現在我已經決定去與JToggleButtons和我有過成功。如何切換按鈕並打印標籤。例如:如果我點擊L00,則打印出L00爲真,然後選擇它;如果我再次點擊它,則取消選擇它並打印L00 false。這很棒。謝謝。 – waltfy 2012-03-31 21:41:15