[英]Adding a class which extends JLabel into a panel with gridlayout
我正在嘗試用Swing創建一個電影院大廳。 所以為此我創建了一個擴展JLabel
的類Seat
:
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;
}
}
我想用這個班級在大廳里畫座位,並且動作聽眾在點擊時改變座位的顏色。
但是當我嘗試在GridLayout
的面板中添加我的座位時,我的座位會顯示在另一個上面。
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);
}
對不起,我的英語不好!
問題是你在類Seat
重寫了JComponent
方法getX
和getY
。
如果您需要該坐標以進行預留,請更改這些方法名稱。
我已經創建了這個簡單的GUI來測試你的類(使用不同的圖像),如果評論了這兩個方法,它就會按預期工作。
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();
}
});
}
}
編輯:這是我用過的圖片http://www.marcuscenter.org/wp-content/uploads/2013/02/seating-chart.png
你重寫了JComponent getX()getY()方法,這些方法阻止JPanel正確布局標簽
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.