JToggleButton 未在 GUI 中显示

JToggleButton not showing in GUI

我试图在 JPanel totalGUI 中添加一个矩形和 30 JButton,但只显示了矩形。

public class SelectSeat {

    static JFrame frame;
    int a;

    public JPanel createContentPane() throws IOException
    {

        JPanel totalGUI = new JPanel();
        RectDraw rect= new RectDraw();
        rect.setPreferredSize(new Dimension(330,35)); 
        totalGUI.setLayout(null);
        totalGUI.setBackground(Color.WHITE);
        totalGUI.add(rect);
        Dimension d = rect.getPreferredSize();
        rect.setBounds(100, 20, d.width, d.height);

        JToggleButton[] ButtonList = new JToggleButton[30];

        for(int i = 0; i < 30; i++) {
            a=i+1;
            ButtonList[i]= new JToggleButton(""+a);
            totalGUI.add(ButtonList[i]);
        }
        totalGUI.add(rect);

        return totalGUI;
    }

    void setVisible(boolean b) {
        // TODO Auto-generated method stub
    }

    static void createAndShowGUI() throws IOException
    {

        JFrame.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame("Seat Selection");

        //Create and set up the content pane.
        SelectSeat demo = new SelectSeat();
        frame.setContentPane(demo.createContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(535, 520);
        frame.setLocation(500,220);
        frame.setVisible(true);
    }

    private static class RectDraw extends JPanel
    {
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);  
             g.setColor(Color.GRAY); 
             g.fillRect(0,0,getWidth(),getHeight());
             g.setColor(Color.BLUE);
             g.drawString("Movie Sceen", 130, 20);   
            }


    }

}

最新输出


买票

next.addActionListener(new ActionListener()  // go to SelectSeat class
        {
             public void actionPerformed(ActionEvent e)
                {
                 SelectSeat back = new SelectSeat();
                 back.setVisible(true);
                    setVisible(false);
                    frame.dispose();
                }
        });

选择座位

public class SelectSeat {

    public static void main(String[] atrg) throws IOException {
    JFrame meinJFrame = new JFrame();
    meinJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    meinJFrame.setSize(800, 600);
    meinJFrame.setVisible(true);
    meinJFrame.add(new RectDraw());
    JPanel over = new JPanel(new GridLayout(2, 1));
    over.add(new RectDraw());
    meinJFrame.setTitle("Cinemax-Beispiel");
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(6, 5));
    for (int i = 1; i <= 30; i++) {
        panel.add(new JToggleButton("Seat :" + i));
    }
    over.add(panel);
    meinJFrame.add(over);
    }

    public void setVisible(boolean b) {  // auto-generated
        // TODO Auto-generated method stub

    }



}

class RectDraw extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.WHITE);
    g.drawString("Movie Sceen", 130, 20);
    }
}       

问题是你没有很好地组织jpanel 您应该以这种形式组织面板:

----JPanel----------------------------|
| ---Screen el----------------------  |
| |             txt                 | |
| |                                 | |
| ----------------------------------- |
| ---seats -------------------------- |
| |seat1  seat2 .. ...  ...  ...    | |
| |seat11 seat12 .. ...  ...  ..    | |
| |... ... ...                      | |
| ----------------------------------- |
---------------------------------------


public class Mango {

    public static void main(String[] atrg) throws IOException {
    JFrame meinJFrame = new JFrame();
    meinJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    meinJFrame.setSize(800, 600);
    meinJFrame.setVisible(true);
    meinJFrame.add(new RectDraw());
    JPanel over = new JPanel(new GridLayout(2, 1));
    over.add(new RectDraw());
    meinJFrame.setTitle("Cinemax-Beispiel");
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(6, 5));
    for (int i = 1; i <= 30; i++) {
        panel.add(new JToggleButton("Seat :" + i));
    }
    over.add(panel);
    meinJFrame.add(over);
    }
}

class RectDraw extends JPanel {
    private static final long serialVersionUID = -6507364980760764676L;

    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.WHITE);
    g.drawString("Movie Sceen", 130, 20);
    }
}