我正在用Java创建一个简单的Messenger(仅出于学习目的),并且尝试有一个“朋友”标签,该标签的左侧是朋友列表,右侧是您单击的与好友的消息,但是每当我尝试在选项卡中添加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);

最佳答案

friendsPanel.setLayout(null);


您的friendsPanel具有null布局,并且要添加拆分窗格,如下所示:

friendsPanel.add(friendsSplitPane);


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

开始学习:Lesson: Laying Out Components Within a Container。让我们对他们为节省时间而付出的努力给予一些价值,我们正在花费这些努力来寻找答案:uhh! where is my component!?!

关于java - JSwing拆分 Pane 不会显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19861882/

10-14 07:41