与this一样,我有一个带有JMenuBar和JToolBar的JFrame。然后使用MigLayout添加了两个JScrollPane,其宽度为15%和85%,并包含一个JTree&相应的JPanel。有什么方法可以动态地重新调整两个JScrollpanes的大小吗?有人可以分享任何资源吗?我是这样做的:
package ui;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import com.jtattoo.plaf.aluminium.AluminiumLookAndFeel;
import net.miginfocom.swing.MigLayout;
public class MainFrameView
{
private JFrame workingFrame;
private JPanel footerPanel;
private JScrollPane leftScrollPane,rightScrollPane;
private JMenuBar mainMenuBar;
private JToolBar headerToolBar;
private JTree exploreTree;
public void createMainFrame()
{
workingFrame = new JFrame("Demo");
workingFrame.setLayout(new MigLayout("","[grow]","[grow]"));
workingFrame.setJMenuBar(getMainMenuBar());
workingFrame.add(getHeaderToolbar(),"dock north");
workingFrame.add(getLeftScrollPane(),"dock west, width 15%!");
workingFrame.add(getRightScrollPane(),"dock east, width 85%!");
workingFrame.setSize(940, 500);
workingFrame.setMinimumSize(new Dimension(700,400));
workingFrame.setVisible(true);
workingFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JMenuBar getMainMenuBar()
{
mainMenuBar = new JMenuBar();
mainMenuBar.setLayout(new MigLayout("inset 0"));
JMenu[] menu = new JMenu[5];
for(int i=0;i<5;i++)
{
menu[i]=new JMenu("Menu-"+Integer.toString(i+1));
menu[i].setHorizontalTextPosition(SwingConstants.CENTER);
menu[i].setVerticalTextPosition(SwingConstants.BOTTOM);
mainMenuBar.add(menu[i]);
}
return mainMenuBar;
}
private JToolBar getHeaderToolbar()
{
headerToolBar = new JToolBar();
headerToolBar.setFloatable(false);
headerToolBar.setLayout(new MigLayout("inset 1px"));
headerToolBar.add(new JButton("B1"));
headerToolBar.add(new JButton("B2"));
headerToolBar.add(new JButton("B3"));
headerToolBar.add(new JButton("B4"));
headerToolBar.add(new JButton("B5"));
return headerToolBar;
}
private JScrollPane getLeftScrollPane()
{
leftScrollPane = new JScrollPane(getExploreTree());
leftScrollPane.setOpaque(false);
leftScrollPane.getViewport().setOpaque(false);
leftScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
leftScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return leftScrollPane;
}
private JTree getExploreTree()
{
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Menu");
DefaultMutableTreeNode firstNode = new DefaultMutableTreeNode("Node 1");
firstNode.add(new DefaultMutableTreeNode("Sub Node 1"));
firstNode.add(new DefaultMutableTreeNode("Sub Node 2"));
firstNode.add(new DefaultMutableTreeNode("Sub Node 3"));
firstNode.add(new DefaultMutableTreeNode("Sub Node 4"));
firstNode.add(new DefaultMutableTreeNode("Sub Node 5"));
DefaultMutableTreeNode secondNode = new DefaultMutableTreeNode("Node2");
secondNode.add(new DefaultMutableTreeNode("Sub Node 1"));
secondNode.add(new DefaultMutableTreeNode("Sub Node 2"));
secondNode.add(new DefaultMutableTreeNode("Sub Node 3"));
secondNode.add(new DefaultMutableTreeNode("Sub Node 4"));
secondNode.add(new DefaultMutableTreeNode("Sub Node 5"));
DefaultMutableTreeNode thirdNode = new DefaultMutableTreeNode("Node 3");
thirdNode.add(new DefaultMutableTreeNode("Sub Node 1"));
thirdNode.add(new DefaultMutableTreeNode("Sub Node 2"));
thirdNode.add(new DefaultMutableTreeNode("Sub Node 3"));
thirdNode.add(new DefaultMutableTreeNode("Sub Node 4"));
thirdNode.add(new DefaultMutableTreeNode("Sub Node 5"));
DefaultMutableTreeNode fourthNode = new DefaultMutableTreeNode("Node4");
fourthNode.add(new DefaultMutableTreeNode("Sub Node 1"));
fourthNode.add(new DefaultMutableTreeNode("Sub Node 2"));
fourthNode.add(new DefaultMutableTreeNode("Sub Node 3"));
fourthNode.add(new DefaultMutableTreeNode("Sub Node 4"));
fourthNode.add(new DefaultMutableTreeNode("Sub Node 5"));
DefaultMutableTreeNode fifthNode = new DefaultMutableTreeNode("Node 5");
fifthNode.add(new DefaultMutableTreeNode("Sub Node 1"));
fifthNode.add(new DefaultMutableTreeNode("Sub Node 2"));
fifthNode.add(new DefaultMutableTreeNode("Sub Node 3"));
fifthNode.add(new DefaultMutableTreeNode("Sub Node 4"));
fifthNode.add(new DefaultMutableTreeNode("Sub Node 5"));
DefaultMutableTreeNode sixthNode = new DefaultMutableTreeNode("Node6");
sixthNode.add(new DefaultMutableTreeNode("Sub Node 1"));
sixthNode.add(new DefaultMutableTreeNode("Sub Node 2"));
sixthNode.add(new DefaultMutableTreeNode("Sub Node 3"));
sixthNode.add(new DefaultMutableTreeNode("Sub Node 4"));
sixthNode.add(new DefaultMutableTreeNode("Sub Node 5"));
rootNode.add(firstNode);
rootNode.add(secondNode);
rootNode.add(thirdNode);
rootNode.add(fourthNode);
rootNode.add(fifthNode);
rootNode.add(sixthNode);
UIManager.put("Tree.rendererFillBackground", false);
exploreTree = new JTree(rootNode);
exploreTree.setOpaque(false);
exploreTree.setRootVisible(false);
return exploreTree;
}
private JScrollPane getRightScrollPane()
{
JPanel j = new JPanel(new MigLayout("width 1500px!, height 1500px!"));
j.setBackground(new Color(200,200,200));
rightScrollPane = new JScrollPane();
rightScrollPane.setViewportView(j);
rightScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
rightScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return rightScrollPane;
}
}
答案 0 :(得分:0)
您需要使用JSplitPane。参见
https://docs.oracle.com/javase/tutorial/uiswing/components/splitpane.html