代码之家  ›  专栏  ›  技术社区  ›  Yat

添加一个类,将JLabel扩展到具有网格布局的面板中

  •  1
  • Yat  · 技术社区  · 9 年前

    我正在尝试用Swing创建一个电影院大厅。为此,我创建了一个类 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;
    }
    }
    

    我想用这个类来绘制大厅中的座位,通过一个动作,当点击座位时,听众可以改变座位的颜色。

    但当我试图在一个有 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);
    
    
    
    }
    

    The result is this..

    对不起,我的英语不好!

    2 回复  |  直到 9 年前
        1
  •  3
  •   RubioRic Danail Tsvetanov    9 年前

    问题是你超越了 JComponent 方法 getX getY 在你的课堂上 Seat .

    如果出于保留目的需要该坐标,请更改这些方法的名称。

    我已经创建了这个简单的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();
                     }
                });   
    
            }  
    
         }
    

    enter image description here

    编辑时间: 这是我用过的图像 http://www.marcuscenter.org/wp-content/uploads/2013/02/seating-chart.png

        2
  •  1
  •   Chen    9 年前

    您正在重写JComponent getX()getY()方法,这些方法会阻止JPanel正确布局标签