添加一个 class 将 JLabel 扩展到带有 gridlayout 的面板中
Adding a class which extends JLabel into a panel with gridlayout
我正在尝试用 Swing 创建一个电影院。因此,为此我创建了一个 class Seat
扩展 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;
}
}
我想用这个 class 绘制大厅中的座位,并通过动作监听器在单击时更改座位颜色。
但是当我尝试在带有 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);
}
对不起我的英语不好!
您正在覆盖 JComponent getX() getY() 方法,这会阻止 JPanel 正确布局标签
问题是您在 class Seat
.
中覆盖了 JComponent
方法 getX
和 getY
如果您出于预订目的需要坐标,请更改这些方法的名称。
我创建了这个简单的 GUI 来测试您的 class(使用不同的图像),如果对这两种方法进行了注释,它就会按预期工作。
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
我正在尝试用 Swing 创建一个电影院。因此,为此我创建了一个 class Seat
扩展 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;
}
}
我想用这个 class 绘制大厅中的座位,并通过动作监听器在单击时更改座位颜色。
但是当我尝试在带有 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);
}
对不起我的英语不好!
您正在覆盖 JComponent getX() getY() 方法,这会阻止 JPanel 正确布局标签
问题是您在 class Seat
.
JComponent
方法 getX
和 getY
如果您出于预订目的需要坐标,请更改这些方法的名称。
我创建了这个简单的 GUI 来测试您的 class(使用不同的图像),如果对这两种方法进行了注释,它就会按预期工作。
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