gpt4 book ai didi

java - 两个分割 Pane 放置在一个选项卡上

转载 作者:行者123 更新时间:2023-12-01 09:03:05 26 4
gpt4 key购买 nike

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;


public class MenuPage extends JFrame{

final static int extraWindowWidth = 100;

JSplitPane jSplitPane, jSplitPane2,jSplitPane3,jSplitPane4;
JPanel jPanel2a, jPanel2b, jPanel3;

public static JFrame frame;

public MenuPage(){
}

private final JList<String> list1=new JList<>(new String[]{"A","B"});
private final JList<String> list2 = new JList<>();
private final List<DefaultListModel> models = new ArrayList<>();



public void addComponentToPane(Container pane) {
JTabbedPane tabbedPane = new JTabbedPane();



// Create the "cards".
JPanel card1 = new JPanel() {
// Make the panel wider than it really needs, so
// the window's wide enough for the tabs to stay
// in one row.
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width += extraWindowWidth;
return size;
}
};


DefaultListModel<String> model1 = new DefaultListModel<>();
model1.addElement("A1");
model1.addElement("A2");
model1.addElement("A3");
models.add(model1);
DefaultListModel<String> model2 = new DefaultListModel<>();
model2.addElement("B1");
model2.addElement("B2");
models.add(model2);
list2.setModel(model1);
list1.addListSelectionListener((ListSelectionEvent e) -> {
if (!e.getValueIsAdjusting()) {
list2.setModel(models.get(list1.getSelectedIndex()));
}
});






jPanel2a = new JPanel();
jPanel2b = new JPanel();
jPanel3 = new JPanel();







jSplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel2a, jPanel2b);





jSplitPane3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list1, list2);
jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jSplitPane3,jPanel3);




// panelin icerisine component eklendi
JLabel lblHelloWorld = new JLabel("Hello World!");
jPanel2a.add(lblHelloWorld);

jSplitPane2.setOneTouchExpandable(true);
jSplitPane2.setDividerLocation(100);


jSplitPane3.setOneTouchExpandable(true);
jSplitPane3.setDividerLocation(150);


jSplitPane4.setOneTouchExpandable(true);
jSplitPane4.setDividerLocation(300);



JPanel card3 = new JPanel();
JPanel card4 = new JPanel();
JPanel card5 = new JPanel();
JPanel card6 = new JPanel();
JPanel card7 = new JPanel();
JPanel card8 = new JPanel();



card3.add(jSplitPane3,jSplitPane4);


tabbedPane.addTab("Main", jSplitPane2);

tabbedPane.addTab("Leagues",jSplitPane4);


tabbedPane.addTab("Teams",card3 );
tabbedPane.addTab("Ratios", card4);
tabbedPane.addTab("Competitions", card5);
tabbedPane.addTab("Analyze", card6);
tabbedPane.addTab("Help", card7);
tabbedPane.addTab("About", card8);

pane.add(tabbedPane, BorderLayout.CENTER);


}

/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event dispatch thread.
*/

public static void createAndShowGUI() {
// Create and set up the window.
frame = new JFrame("BetAnalyzer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(594, 310);

// Create and set up the content pane.
MenuPage demo = new MenuPage();
demo.addComponentToPane(frame.getContentPane());

// Display the window.
frame.pack();
frame.setVisible(true);
frame.setBounds(100, 100, 635, 251);

}
}

我希望将第 2 部分 JSplitPane 中的 list1、list2 和 jpanel3 放在 league 选项卡中 我的代码有问题。只有 jSplitPane3 或 jSplitPane4 可以出现在布局中。

此外,我尝试将两个 JSplitPane 添加到 Box 中,然后将 Box 放入选项卡 Pane 中,但这也不起作用。

最佳答案

您的代码有几个问题。

不要无故继承

您的类继承自 JFrame,但它不会扩展其行为(它不会覆盖 JFrame 的公共(public)或 protected 方法)。

组件只能添加一次

与其他点相比,这本身不是问题,而是问题的一部分。

 jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jSplitPane3,jPanel3);
// ...
card3.add(jSplitPane3,jSplitPane4);

执行第二条语句时,jSplitPane3 将从 jSplitPane4 中删除,并且仅显示为 card3 的内容。

使用标准布局

面板的(虚拟)内容相当小,因此它们的 preferedSize 小于父容器中的可用空间。

JPanels 默认布局是 FlowLayout,它将每个组件缩小到其 preferedSize

您应该将 JPanel 的布局设置为 BorderLayout,以允许其内容延伸到整个空间。

反过来,您不能使用 JPanel 的 varag 方法 .add(Component, component)不再了。

<小时/>

以下是相关更改:

    jPanel2a = new JPanel(new BorderLayout());
jPanel2b = new JPanel(new BorderLayout());
jPanel3 = new JPanel(new BorderLayout());

jSplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel2a, jPanel2b);

jSplitPane3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list1, list2);
jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jSplitPane3, jPanel3);

// panelin icerisine component eklendi
JLabel lblHelloWorld = new JLabel("Hello World!");
jPanel2a.add(lblHelloWorld);

// jSplitPane2.setOneTouchExpandable(true);
jSplitPane2.setDividerLocation(100);
//
// jSplitPane3.setOneTouchExpandable(true);
jSplitPane3.setDividerLocation(150);
//
// jSplitPane4.setOneTouchExpandable(true);
jSplitPane4.setDividerLocation(300);

JPanel card3 = new JPanel(new BorderLayout());
JPanel card4 = new JPanel(new BorderLayout());
JPanel card5 = new JPanel(new BorderLayout());
JPanel card6 = new JPanel(new BorderLayout());
JPanel card7 = new JPanel(new BorderLayout());
JPanel card8 = new JPanel(new BorderLayout());

// card3.add(jSplitPane3);
card3.add(jSplitPane4,BorderLayout.SOUTH);

tabbedPane.addTab("Main", jSplitPane2);

关于java - 两个分割 Pane 放置在一个选项卡上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41522889/

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