我的Swing应用程序包含一个内部有多个JPanel的JFrame。我需要在这样的布局中安排那些JPanel:
==============================================
| MenuBar Panel
|=============================================
| |Header Panel
| JTree Panel |===============================
| having Jtree|Description Panel
| |===============================
| |TabbedPane Panel having two JPanels
| |named Edit and Results
| |
| |
| |
| |
| |
根据JTree节点的选择,将在TabbedPane中打印不同的编辑面板。编辑面板必须是CardLayout,因为它一次只显示一张卡。但我的问题是,如果我的应用程序FullScreen
我应该使用LayoutManager
,我该如何实现此布局?如果我使用GridLayout
或BorderLayout
,如何在Jtree valueChanged()
上的JPanel之间切换?
答案 0 :(得分:1)
对于布局,我可以向您推荐MigLayout(http://miglayout.com)。
与其他布局管理器相比,它更易于阅读和编写更快。
要实现您的特定布局,您可以这样做:
JPanel panel = new JPanel(new MigLayout());
panel.add(menuBarPanel, "wrap");
panel.add(jTreePanel, "dock west");
panel.add(headerPanel, "wrap");
panel.add(descriptionPanel, "wrap");
panel.add(tabbedPane, "wrap");
这应该可以解决布局问题。其余的由dic19回答!
答案 1 :(得分:1)
我创建了一个MigLayout
的示例。 CardLayout
经理是
没必要。
package com.zetcode;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import net.miginfocom.swing.MigLayout;
public class DynamicEditPanel extends JFrame {
private JPanel editPanel;
public DynamicEditPanel() {
initUI();
setTitle("Dynamic Edit panel");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
createMenuBar();
JTree tree = new JTree();
TreeSelectionModel smod = tree.getSelectionModel();
smod.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
smod.addTreeSelectionListener(new MySelectionListener());
JPanel pnl = new JPanel(new MigLayout("wrap"));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
tree, pnl);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
editPanel = getPanel("Edit");
tabbedPane.add("Edit", editPanel);
tabbedPane.add("Results", getPanel("Results"));
pnl.add(getPanel("Header"), "w 250, h 100, pushx, growx");
pnl.add(getPanel("Description"), "w 250, h 100, growx");
pnl.add(tabbedPane, "grow, pushy");
add(splitPane);
pack();
}
private void createMenuBar() {
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
menubar.add(file);
menubar.add(edit);
setJMenuBar(menubar);
}
private JPanel getPanel(String text) {
JPanel pnl = new JPanel(new BorderLayout());
JLabel label = new JLabel(text, JLabel.CENTER);
pnl.add(label);
pnl.setBorder(BorderFactory.createEtchedBorder());
return pnl;
}
private class MySelectionListener
implements TreeSelectionListener {
@Override
public void valueChanged(TreeSelectionEvent e) {
TreeSelectionModel model
= (TreeSelectionModel) e.getSource();
TreePath path = model.getSelectionPath();
if (path != null) {
Object item = path.getLastPathComponent();
editPanel.removeAll();
editPanel.add(new JLabel(item.toString(), JLabel.CENTER));
editPanel.doLayout();
editPanel.repaint();
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
DynamicEditPanel ex = new DynamicEditPanel();
ex.setVisible(true);
}
});
}
}
单击树节点将在“编辑”面板中创建新标签。老人
使用removeAll()
方法删除组件。
我还实现了一个拆分窗格,因为我认为这是预期的设计。