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