Reputation: 43
I'm trying to create a cinema hall with Swing. So for this purpose I've created a class Seat
which extends JLabel
:
public class Seat extends JLabel{
public boolean isVip() {
return vip;
}
public boolean isHandicap() {
return handicap;
}
public boolean isOccupato() {
return occupato;
}
public void setVip(boolean vip) {
this.vip = vip;
}
public void setHandicap(boolean handicap) {
this.handicap = handicap;
}
public void setOccupato(boolean occupato) {
this.occupato = occupato;
}
private int x;
private int y;
private boolean vip;
private boolean handicap;
private boolean occupato;
public Seat(int x, int y, ImageIcon img) {
this.x = x;
this.y = y;
this.setIcon(img);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
And I want to use this class to draw the seats in a hall, and with an action listener change seats color when they are clicked.
But when I try to add my seats in a panel with GridLayout
, my seats are shown one over the other.
public class PanelAddHall extends JPanel {
private int x=5;
private int y=5;
private ArrayList<Seat> seats = null;
public PanelAddHall(Controller_Gestore controller, final JLabel outputGrafico) {
seats = new ArrayList<>();
this.setBackground(Color.GRAY);
this.setLayout(new BorderLayout(10,30));
JPanel nord = new JPanel();
JPanel sud = new JPanel(new GridLayout(x,y,0,5));
JPanel west = new JPanel();
nord.setBackground(Color.GRAY);
sud.setBackground(Color.GRAY);
west.setBackground(Color.GRAY);
ImageIcon seat_icon = new ImageIcon("immagini/poltrone/seat_disponibile.png");
ImageIcon screen_icon = new ImageIcon("immagini/poltrone/screen.png");
JLabel screen = new JLabel(screen_icon);
nord.add(screen);
for(int i = 0; i<x; i++){
for(int j=0; j<y; j++){
seats.add(new Seat(i,j,seat_icon));
}
}
for(int i = 0; i<x*y ; i++) {
sud.add(seats.get(i));
}
this.add(nord, BorderLayout.NORTH);
this.add(sud, BorderLayout.SOUTH);
this.add(west, BorderLayout.WEST);
}
Sorry for my bad english!
Upvotes: 1
Views: 185
Reputation: 2468
The problem is that you're overriding JComponent
methods getX
and getY
in your class Seat
.
If you need that coordinates for reservation purposes change those methods names.
I've created this simple GUI to test your class (using a different image) and it works as intended if those two methods are commented.
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class SimpleFrameTest2 extends JFrame {
public SimpleFrameTest2() {
setSize(500, 500);
setTitle("Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);
initComponents();
setVisible(true);
}
private void initComponents() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 5, 0, 5));
ImageIcon seat_icon = new ImageIcon("C:\\Users\\Ricardo\\Pictures\\referencias\\seating-chart.png");
for (int i = 0; i < 25; i++) {
//panel.add(new JLabel("" + i, seat_icon, JLabel.CENTER));
panel.add(new Seat(i, i, seat_icon));
}
add(panel);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SimpleFrameTest2();
}
});
}
}
EDITED: This is the image that I've used http://www.marcuscenter.org/wp-content/uploads/2013/02/seating-chart.png
Upvotes: 3
Reputation: 3760
you are overriding the JComponent getX() getY() methods which prevent the JPanel to layout the Labels properly
Upvotes: 1