JSwing Split Panes将不会显示

时间:2013-11-08 14:50:48

标签: java swing jpanel null-layout-manager

我正在用Java创建一个简单的信使(仅用于学习目的),我想要一个朋友标签,左侧是朋友列表,右侧是与朋友的消息,你单击,但每当我尝试在选项卡中添加JSplitPane时,它都不会显示。我做了完全相同的代码(除了我只做了JSplitPane的东西及其组件,而不是其他标签和菜单等。

我的所有代码

package Client;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ClientMain extends JFrame {

int WIDTH = 640;
int HEIGHT = 480;

JTabbedPane tabbedPane;
JMenuBar topMenuBar;
JMenu userMenu, helpMenu, settingsMenu;
JRadioButtonMenuItem menuItem;
JPanel mainPanel, friendsPanel, groupsPanel, testPanel;
JLabel title;
JScrollPane consoleScrollPane, friendChatScrollPane, friendListScrollPane;
JSplitPane friendsSplitPane;
JTextArea friendChatArea, testArea;
JTextField friendChatField, testField;
JList friendList;
Box box;

DefaultListModel friends;

public ClientMain() {
    super("Messenger Client");

    //Networking();


    /** MAIN PANEL **/


        title = new JLabel("Client!");
        title.setFont(new Font("Impact", Font.PLAIN, 32));

        mainPanel = new JPanel();
        mainPanel.setLayout(null);
        mainPanel.add(title);

    /** TEST PANEL **/


        groupsPanel = new JPanel();
        groupsPanel.setLayout(null);


    /** FRIENDS PANEL **/


        friendsPanel = new JPanel();
        friendsPanel.setLayout(null);

        friends = new DefaultListModel();
        //method here that retrieves users' friends from the server and adds them to the friends DefaultListModel
        friends.addElement("Person1");
        friends.addElement("Person2");

        friendList = new JList(friends);
        friendList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        friendList.setLayoutOrientation(JList.VERTICAL_WRAP);
        friendList.setVisibleRowCount(3);
        friendList.setSize(50, 50);

        friendChatArea = new JTextArea();
        friendChatArea.setSize(50, 50);
        friendChatArea.setFont(new Font("", Font.PLAIN, 13 + 1/2));
        friendChatArea.append("TEST");

        Dimension minimumSize = new Dimension(100, 50);
        friendListScrollPane = new JScrollPane(friendList);
        friendListScrollPane.setMinimumSize(minimumSize);

        friendChatScrollPane = new JScrollPane(friendChatArea);
        friendChatScrollPane.setMinimumSize(minimumSize);

        friendsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, friendListScrollPane, friendChatScrollPane);
        friendsSplitPane.setOneTouchExpandable(false);
        friendsSplitPane.setDividerLocation(50);

        friendsPanel.add(friendsSplitPane);

    /** TEST PANEL **/


        testPanel = new JPanel();
        testPanel.setLayout(null);

        testArea = new JTextArea();
        testArea.setFont(new Font("", Font.PLAIN, 13 + 1/2));
        testArea.setBounds(0, 0, WIDTH, HEIGHT - 100);
        testArea.setEditable(false);
        testArea.setLineWrap(true);

        testField = new JTextField(20);
        testField.setBounds(0, 380, 640, 25);
        //testField.setLocation(0, HEIGHT - 50);
        testField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ClientNet net = new ClientNet();
                String input = null;
                input = testField.getText();
                testArea.append(input);
                testField.setText("");

                if(input.equalsIgnoreCase("/sendmessage")) {
                    testArea.append("\n Input who you would like to send the message to:");
                    input = null;

                    if(input.equalsIgnoreCase("Hello")) {
                        net.userEntry = input;
                    }
                }
            }
        });

        testPanel.add(testArea);
        testPanel.add(testField);


    /** SET UP **/


        tabbedPane = new JTabbedPane();

        tabbedPane.add(mainPanel, "Main");
        tabbedPane.add(friendsPanel, "Friends");
        tabbedPane.add(groupsPanel, "Groups");
        tabbedPane.add(testPanel, "Test");

        topMenuBar = new JMenuBar();

        userMenu = new JMenu("User");

        settingsMenu = new JMenu("Settings");

        helpMenu = new JMenu("Help");

        menuItem = new JRadioButtonMenuItem("Something here");

        userMenu.add(menuItem);

        topMenuBar.add(userMenu, "User");
        topMenuBar.add(settingsMenu, "Settings");
        topMenuBar.add(helpMenu, "Help");

        add(topMenuBar);
        add(tabbedPane);
}

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    } catch(UnsupportedLookAndFeelException e) {
        e.printStackTrace();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();

    } catch (InstantiationException e) {
        e.printStackTrace();

    } catch (IllegalAccessException e) {
        e.printStackTrace();

    }

    ClientMain frame = new ClientMain();
    Insets insets = frame.getInsets();
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setSize(frame.WIDTH, frame.HEIGHT);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setJMenuBar(frame.topMenuBar);

}

public void Networking() {
    ClientNet net;
    try {
        net = new ClientNet();
        net.start();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

public void createMenuBar() {
    topMenuBar = new JMenuBar();

    userMenu = new JMenu("User");
    settingsMenu = new JMenu("Settings");
    helpMenu = new JMenu("Help");

    menuItem = new JRadioButtonMenuItem("MenuItem");
    menuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

        }
    });

    topMenuBar.add(userMenu, "User");
    topMenuBar.add(settingsMenu, "Settings");
    topMenuBar.add(helpMenu, "Help");
}

public void createSplitPane() {
    friendListScrollPane = new JScrollPane();
    friendListScrollPane.add(new JLabel("Hello"));

    friendChatArea = new JTextArea();
    friendChatArea.setBounds(0, 150, HEIGHT, HEIGHT);
    friendChatArea.setFont(new Font("", Font.PLAIN, 13 + 1/2));

    Dimension minimumSize = new Dimension(WIDTH/2 , HEIGHT);
    friendListScrollPane.setMinimumSize(minimumSize);

    //friendsSplitPane.setLeftComponent()
    friendsSplitPane.add(friendChatArea);
    friendsSplitPane.setRightComponent(friendChatScrollPane);
}

}

特定的JSplitPane代码(上面代码的一部分)

friendsPanel = new JPanel();
        friendsPanel.setLayout(null);

        friends = new DefaultListModel();
        //method here that retrieves users' friends from the server and adds them to the friends DefaultListModel
        friends.addElement("Person1");
        friends.addElement("Person2");

        friendList = new JList(friends);
        friendList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        friendList.setLayoutOrientation(JList.VERTICAL_WRAP);
        friendList.setVisibleRowCount(3);
        friendList.setSize(50, 50);

        friendChatArea = new JTextArea();
        friendChatArea.setSize(50, 50);
        friendChatArea.setFont(new Font("", Font.PLAIN, 13 + 1/2));
        friendChatArea.append("TEST");

        Dimension minimumSize = new Dimension(100, 50);
        friendListScrollPane = new JScrollPane(friendList);
        friendListScrollPane.setMinimumSize(minimumSize);

        friendChatScrollPane = new JScrollPane(friendChatArea);
        friendChatScrollPane.setMinimumSize(minimumSize);

        friendsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, friendListScrollPane, friendChatScrollPane);
        friendsSplitPane.setOneTouchExpandable(false);
        friendsSplitPane.setDividerLocation(50);

        friendsPanel.add(friendsSplitPane);

1 个答案:

答案 0 :(得分:2)

friendsPanel.setLayout(null);

您的friendsPanel具有空布局,并且您要将拆分窗格添加为:

friendsPanel.add(friendsSplitPane);

要将component添加到具有null布局的容器(friendsPanel),您必须使用component.setBounds()方法指定要添加的组件的边界。但是,为什么甚至使用null布局呢?不要使用它。堆栈溢出的回转族已经从页面到页面的建议。尝试使用Swing开发人员为我们创建的布局管理器之一,努力日复一日浪费时间,以节省我们自己的时间

开始学习:Lesson: Laying Out Components Within a Container。让我们为节省时间付出一些努力,我们只是为了找到答案而花费的时间:uhh! where is my component!?!