gpt4 book ai didi

java - JSplitPane - 将应用程序分成两个?

转载 作者:行者123 更新时间:2023-12-01 17:28:08 25 4
gpt4 key购买 nike

我制作了一个 Java swing 应用程序。我的主类完成了整个 SwingUtilities.invokeLater(new Runnable() 的工作。

我的第二堂课,里面的一切都是我用过的:

JPanel container = (JPanel) getContentPane();

然后通过调用添加所有位..

container.add([name of component]

我现在想将整个“应用程序”放入 JSplitPane 中。因此,我希望我的应用程序位于一侧,而其他内容位于右侧。

我该怎么做?

    public class one{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new App("main");
f.setSize(1920,1080);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}


public class App extends JFrame {
SuppressWarnings("empty-statement")
public App(String title) {
super(title);
JPanel container = (JPanel) getContentPane();

container.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton new button = new JButton("new");
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
//ENTER LOTS OF CONSTRAINTS
container.add(new, c);

JButton next to button = new JButton("next to");
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
//ENTER LOTS OF CONSTRAINTS
container.add(new, c);

//I HAVE LOTS OF SWING COMPONENTS AND BUTTONS about 30

}

我想将所有这些都放在分割 Pane 的左侧吗?

我该怎么做?

最佳答案

  1. 不要从 JFrame 扩展,您不会添加任何功能,而是将所有应用程序组件和逻辑移动到单独的 JPanel
  2. 创建 JSplitPane 的实例,向其中添加“主”面板,并向其中添加辅助 Pane
  3. 创建 JFrame 的实例,向其中添加分割 Pane ...

已更新

enter image description here

public class TestSplitPane {

public static void main(String[] args) {
new TestSplitPane();
}

public TestSplitPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
pane.setLeftComponent(new MainPane());
pane.setRightComponent(new JLabel("On the right"));

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(pane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class MainPane extends JPanel {

public MainPane() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton newButton = new JButton("new");
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
//ENTER LOTS OF CONSTRAINTS
add(newButton, c);

JButton next = new JButton("next to");
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
//ENTER LOTS OF CONSTRAINTS
add(next, c);
}
}
}

关于java - JSplitPane - 将应用程序分成两个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13446324/

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