Java GUI(SWING / AWT) - 空框架 - 未显示的组件

时间:2012-04-04 14:43:40

标签: java swing user-interface awt jpanel

我正在尝试为下面显示的G​​UI创建(手工编码)GUI similair,但是,只显示一个空框架。

模拟GUI: enter image description here

我使用了各种布局和SWING / AWT组件来创建GUI和4个JPanels,其中包含:

  • mainPanel:包含其中的所有面板。
  • listPanel:包含JTable,JLabel和两个JButtons
  • infoPanel:包含JLabel,JCheckBox和JTextBox。
  • addPanel:包含JLists和JButton

这是我到目前为止的编码:

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);


    }


}

任何人都可以告诉我为什么只出现一个空框架?

谢谢和问候, 布赖恩

2 个答案:

答案 0 :(得分:5)

是的,刚刚检查过,如果你真的将mainPanel添加到框架中,你会看到一些东西(虽然看起来不像模拟!)

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

答案 1 :(得分:3)

您尚未将mainPanel添加到框架