2

我正在尝试创建(手动编码)一个类似于下面显示的 GUI 的 GUI,但是,只显示一个空框架。

模拟图形用户界面: 在此处输入图像描述

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

  • mainPanel:包含其中的所有面板。
  • listPanel:包含 JTable、JLabels 和两个 JButton
  • infoPanel:包含 JLabels、JCheckBox 和 JTextBoxes。
  • 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);


    }


}

谁能告诉我为什么只显示一个空框架?

谢谢和问候, 布赖恩

4

2 回答 2

5

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

frame.setContentPane(mainPanel);
frame.pack();
于 2012-04-04T14:49:39.813 回答
3

您尚未将 mainPanel 添加到框架中

于 2012-04-04T14:48:20.717 回答