Java:剧院座位表

时间:2011-11-22 09:52:00

标签: java swing jface

我正在寻找一个解决方案(库)来使用Java(和Swing / JFace)生成和/或显示动态座位表。

应该有可能为单个座位添加颜色和定义事件。 有没有现成的库或标准程序?

提前致谢

1 个答案:

答案 0 :(得分:3)

没有必要寻找一些解决方案或库(确保google可以返回),但可以通过实现JToggleButton& amp;图标(或颜色),非常简单的例子

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TheaterJToggle {

    private int rows = 6;
    private int columns = 8;
    private Icon res = (UIManager.getIcon("OptionPane.errorIcon"));

    public TheaterJToggle() {
        JPanel panel = new JPanel(new GridLayout(columns, rows));
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                final JToggleButton button = new JToggleButton("Row " + row + " seat " + column);
                button.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent actionEvent) {
                        AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
                        boolean selected = abstractButton.getModel().isSelected();
                        if (selected) {
                            button.setIcon(res);
                        } else {
                            button.setIcon(null);
                        }
                    }
                });
                panel.add(button);
            }
        }
        final JFrame frame = new JFrame("JToggleButton Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TheaterJToggle theaterJToggle = new TheaterJToggle();
            }
        });
    }
}