[英]Putting multiple JPanels inside a JTabbed pane
我瀏覽了一段時間,還嘗試將多個面板添加到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和manualicEditorPane來保持更新。 對於嵌入有codePanel和drawPanels的2個splitPanes,我也必須這樣做。 這將使保持所有面板同步變得更加困難。
有什么建議么?
"Is it possible to add the same Jpanel to multiple TabbedPanes."
-不 您一次只能將一個組件添加到一個容器中。 您的JPanels應該共享模型,但使用唯一的組件。 該模型可能是您創建的非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();
}
});
}
}
最好是創建一個正式的模型類,將其注入每個視圖(每個選項卡式窗格的單獨窗格)。
編輯
您發表評論:
是的,我可以通過調用實例來解決此問題,但是我又回到了原來的問題,即必須調用每個實例以影響所有面板中的更改。 比如說我有一個繪圖面板,我需要調用repaint(),那么我必須調用2個不同的實例才能同時更新兩個tabbedPanes。 有沒有辦法解決?
是的,解決方案是使用MVC或模型視圖控件結構。 您的模型擁有整個程序的邏輯,視圖是用戶看到的,控件在兩者之間進行交互。
考慮讓模型通知控件或視圖其更改,然后激發所有觀察者視圖的重新繪制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.