gpt4 book ai didi

java - 将多个 JPanel 放入 JTabbedpane 中

转载 作者:行者123 更新时间:2023-11-30 11:26:28 26 4
gpt4 key购买 nike

我查看了一段时间,还试过向 JTabbedPane 添加多个面板。

我的问题是:是否可以将同一个 Jpanel 添加到多个 TabbedPanes。我尝试过的所有方法,似乎都无法正常工作。这就是它的工作原理。

public MainGUI() {

JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
getContentPane().add(tabbedPane, BorderLayout.CENTER);

JEditorPane instructionalEditorPane = new JEditorPane();
tabbedPane.addTab("Instructional", instructionalEditorPane);

JPanel codePanel = new JPanel();
JPanel drawPanel = new JPanel();

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, codePanel, drawPanel);
splitPane.setResizeWeight(0.75);

tabbedPane.addTab("Code Panel", splitPane);

JEditorPane unifiedInstPane = new JEditorPane();
JPanel unifiedCodePanel = new JPanel();
JPanel unifiedDrawPanel = new JPanel();
JSplitPane unifiedSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, unifiedCodePanel, unifiedDrawPanel);
unifiedSplitPane.setResizeWeight(0.75);

JSplitPane unifiedPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT,unifiedInstPane, unifiedSplitPane);
unifiedPanel.setResizeWeight(0.40);
tabbedPane.addTab("Unified Tab", unifiedPanel);
}

我想做的只是将 instructionalEditorPane 和 splitPane 添加到多个 tabbedPanes,但是当我这样做时,我松开了原来的 Individual tabbedPanes。如果必须的话,我可以这样做,但我必须同时写信给 unifiedInstPane 和 instructionalEditorPane 以保持它们更新。我还必须为嵌入了 codePanel 和 drawPanel 的 2 个 splitPanes 执行此操作。这将使所有面板保持同步变得更加困难。

有什么建议吗?

最佳答案

“是否可以将同一个 Jpanel 添加到多个 TabbedPanes。” - 否。您一次只能将一个组件添加到一个容器中。您的 JPanel 应该共享模型 但使用独特的组件。该模型可能是您创建的非 GUI 类。

例如,这是我的建议的非常简单化的呈现:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class MainGui2 extends JPanel {
private static final int TAB_COUNT = 3;
private JTabbedPane tabbedPane = new JTabbedPane();
private PlainDocument doc = new PlainDocument();
private Action btnAction = new ButtonAction("Button");

public MainGui2() {
for (int i = 0; i < TAB_COUNT; i++) {
tabbedPane.add("Tab " + (i + 1), createPanel(doc, btnAction));
}
setLayout(new BorderLayout());
add(tabbedPane);
}

private JPanel createPanel(PlainDocument doc, Action action) {
JTextArea textArea = new JTextArea(doc);
textArea.setColumns(40);
textArea.setRows(20);

JPanel panel = new JPanel();
panel.add(new JScrollPane(textArea));
panel.add(new JButton(action));
return panel;
}

private class ButtonAction extends AbstractAction {
public ButtonAction(String title) {
super(title);
}

@Override
public void actionPerformed(ActionEvent evt) {
try {
String text = "Button Pressed!\n";
doc.insertString(doc.getLength(), text, null);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}

private static void createAndShowGui() {
JFrame frame = new JFrame("MainGui2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainGui2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

更好的方法是创建一个正式的模型类,将其注入(inject)每个 View ,每个选项卡式 Pane 的各个 Pane 。


编辑
您在评论中声明:

Yes I can fix that by making calls to the instances but then I'm back to my original problem of having to make calls to each instance to affect a change in all the panel. Say for example I have a drawing panel and I need to call repaint(), I would have to make a call to 2 different instances to get both tabbedPanes to update. Is there any way around this?

是的,解决方案是使用 MVC,即模型- View -控制结构。您的模型包含您的整体程序逻辑, View 是用户所看到的,控件在两者之间进行交互。

考虑让您的模型通知控件或 View 其已更改,然后这会刺激重新绘制所有观察者 View 。

关于java - 将多个 JPanel 放入 JTabbedpane 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19800762/

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