简体   繁体   中英

Java GUI (SWING/AWT) - Empty Frame - Components not showing

I'm trying to create (hand-coded) a GUI similair to the GUI shown below, however, only an empty frame shows.

Mock GUI:在此处输入图像描述

I've used various layouts and SWING/AWT components to create the GUI and 4 JPanels which contain:

  • mainPanel: Contains all the panels in it.
  • listPanel: Contains the JTables, JLabels and the two JButtons
  • infoPanel: Contains the JLabels, JCheckBox and JTextBoxes.
  • addPanel: Contains the JLists and JButton

This is what I coded so far:

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


public class GUI extends JFrame {

    public void buildGui() {

        JFrame frame = new JFrame("Hotel TV Scheduler");

                JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

                JPanel listPanel = new JPanel();
                listPanel.setLayout(new GridLayout(3,3));

                JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayout(2,2));

                JPanel addPanel = new JPanel();
        addPanel.setLayout(new FlowLayout());

                mainPanel.add(listPanel, BorderLayout.LINE_START);
                mainPanel.add(infoPanel, BorderLayout.LINE_END);
                mainPanel.add(addPanel, BorderLayout.PAGE_END);

                JTable chOneTable = new JTable();
                JTable chTwoTable = new JTable();
                JTable listTable = new JTable();

                JLabel ch1Label = new JLabel("Channel 1");
                JLabel ch2Label = new JLabel("Channel 2");
                JLabel listLabel = new JLabel("List");

                JButton rmvChOneButton = new JButton("Remove Channel");
                JButton rmvChTwoButton = new JButton("Remove Channel");

                listPanel.add(ch1Label);
                listPanel.add(ch2Label);
                listPanel.add(listLabel);
                listPanel.add(chOneTable);                
                listPanel.add(chTwoTable);
                listPanel.add(listTable);
                listPanel.add(rmvChOneButton);
                listPanel.add(rmvChTwoButton);

                JLabel titleLabel = new JLabel("Title");
                JLabel genreLabel = new JLabel("Genre");
                JLabel durationLabel = new JLabel("Duration");
                JLabel actorLabel = new JLabel("Actor");
                JLabel directorLabel = new JLabel("Director");
                JLabel rentableLabel = new JLabel("Rentable");
                JLabel synLabel = new JLabel("Synopsis");

                JTextField txtTitle = new JTextField();          
                JTextField txtGenre = new JTextField();
                JTextField txtDuration = new JTextField();
                JTextField txtActor = new JTextField();
                JTextField txtDirector = new JTextField();
                JTextField txtSynopsis = new JTextField();

                JCheckBox rentCB = new JCheckBox();

                infoPanel.add(titleLabel);
                infoPanel.add(txtTitle);
                infoPanel.add(genreLabel);
                infoPanel.add(txtGenre);
                infoPanel.add(durationLabel);
                infoPanel.add(txtDuration);
                infoPanel.add(actorLabel);
                infoPanel.add(txtActor);
                infoPanel.add(directorLabel);
                infoPanel.add(txtDirector);
                infoPanel.add(rentableLabel);
                infoPanel.add(rentCB);
                infoPanel.add(synLabel);
                infoPanel.add(txtSynopsis);

                JButton btnAddProg = new JButton("Add Program");

                JList channelList = new JList();
                JList timeList = new JList();

                addPanel.add(btnAddProg);
                addPanel.add(channelList);
                addPanel.add(timeList);

                frame.setVisible(true);


    }


}

Anyone can tell me why only an empty frame is showing up?

Thanks and Regards, Brian

Yep just checked, you'll see something if you actually add the mainPanel to the frame, (looks nothing like the mock though!)

frame.setContentPane(mainPanel);
frame.pack();

You've not added mainPanel to the frame

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM