0

我正在用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);
4

1 回答 1

2
friendsPanel.setLayout(null);

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

friendsPanel.add(friendsSplitPane);

要将 a 添加component到具有空布局的容器(friendsPanel)中,您必须使用component.setBounds()方法指定要添加的组件的边界。但说真的,为什么还要使用null布局?不要使用它。堆栈溢出的摇摆家族已经从一页到一页地给出了建议。尝试使用 Swing 开发人员为我们创建的布局管理器之一,日复一日地浪费时间,只是为了节省我们自己的时间

开始学习:课程:在容器中布置组件。让我们对他们为节省我们的时间所做的努力给予一些价值,我们花费这些时间只是为了找到答案:uhh! where is my component!?!

于 2013-11-08T14:56:52.947 回答