[英]Hide left/right component of a JSplitPane (or different layout)
At the moment I write a little client application. 目前,我编写了一个小的客户端应用程序。 I have a window with a JTextArea (Display area for the server output) and a user-list.
我有一个带有JTextArea(服务器输出的显示区域)和用户列表的窗口。
My plan is to show/hide this user-list over a menu item, but I don't know how. 我的计划是在菜单项上显示/隐藏此用户列表,但我不知道如何。 My ideas:
我的想法:
Use a BorderLayout: Without a JScrollPane for the list. 使用BorderLayout:列表不使用JScrollPane。 It works, but I cannot change the width of the user-list (Because BorderLayout.WEST and BorderLayout.EAST ignore the width)
它可以工作,但是我不能更改用户列表的宽度(因为BorderLayout.WEST和BorderLayout.EAST忽略了宽度)
Use a BorderLayout with a JScrollPane for the user list and show/hide the JScrollPane -> Does not work, don't ask me why...anyway, this way is not a nice solution 对用户列表使用带有JScrollPane的BorderLayout并显示/隐藏JScrollPane->不起作用,不要问我为什么...无论如何,这种方式不是一个好的解决方案
Use a JSplitPane, set the resize weight to 0.9. 使用JSplitPane,将调整大小的权重设置为0.9。 When the user-list should disappear, I minimize the right component (aka the user list) -> How ?
当用户列表消失时,我最小化了正确的组件(又名用户列表)->如何?
My code at the moment: 我目前的代码:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
public class SplitPaneTest extends JFrame implements ActionListener
{
private JSplitPane splitPane;
private JTextArea textDisplay;
private JList<String> listUser;
private JScrollPane scrollTextDisplay;
private JScrollPane scrollListUser;
private JCheckBox itemShowUser;
public static void main(String[] args)
{
new SplitPaneTest();
}
public SplitPaneTest()
{
setTitle("Chat Client");
setMinimumSize(new Dimension(800, 500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textDisplay = new JTextArea();
listUser = new JList<>();
DefaultListModel<String> modelUser = new DefaultListModel<>();
listUser.setModel(modelUser);
modelUser.addElement(new String("User 1"));
modelUser.addElement(new String("User 2"));
modelUser.addElement(new String("User 3"));
scrollTextDisplay = new JScrollPane(textDisplay);
scrollListUser = new JScrollPane(listUser);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(scrollTextDisplay);
splitPane.setRightComponent(scrollListUser);
splitPane.setResizeWeight(0.9);
setContentPane(splitPane);
JMenuBar menubar = new JMenuBar();
JMenu menuWindow = new JMenu("Window");
itemShowUser = new JCheckBox("Show user list");
itemShowUser.addActionListener(this);
itemShowUser.setSelected(true);
menuWindow.add(itemShowUser);
menubar.add(menuWindow);
setJMenuBar(menubar);
setVisible(true);
}
public boolean isUserListEnabled()
{
return itemShowUser.isSelected();
}
public void setUserListEnabled(boolean status)
{
scrollListUser.setVisible(status);
}
@Override
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == itemShowUser)
{
boolean status = isUserListEnabled();
setUserListEnabled(status);
}
}
}
And the result is: 结果是:
And with hidden JScrollPane scrollListUser: 并使用隐藏的JScrollPane scrollListUser:
Can anybody give me a tipp ? 有人可以给我小费吗? The user-list is still visible ( I thought the JSplitPane would repaint..) .I come from Qt (C++) and in Qt I could use a dock widget - but Swing does not have one and to use third libs....I don't know - maybe there is a solution.
用户列表仍然可见(我以为JSplitPane会重新粉刷..)。我来自Qt(C ++),在Qt中我可以使用Dock小部件-但是Swing没有一个部件,并且使用了第三个库。我不知道-也许有解决方案。
Looks like the splitPane can't handle invisible components well - a way out is to add/remove the scrollPane as appropriate: 看起来splitPane无法很好地处理不可见的组件-一种解决方法是适当地添加/删除scrollPane:
public void setUserListEnabled(boolean status)
{
splitPane.setRightComponent(status ? scrollListUser : null);
splitPane.revalidate();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.