gpt4 book ai didi

java - JSwing 拆分 Pane 不会显示

转载 作者:行者123 更新时间:2023-12-02 06:33:42 26 4
gpt4 key购买 nike

我正在用Java创建一个简单的信使(仅用于学习目的),我试图有一个 friend 选项卡,左侧是 friend 列表,右侧是您与 friend 的消息单击,但每当我尝试在选项卡中添加 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 具有空布局,并且您将拆分 Pane 添加为:

friendsPanel.add(friendsSplitPane);

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

开始学习:Lesson: Laying Out Components Within a Container 。让我们珍惜他们为节省我们时间所做的努力,我们花费这些时间只是为了找到答案:呃!我的组件在哪里!?!

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com