gpt4 book ai didi

Java Swing : manage multiple SpringLayout

转载 作者:太空宇宙 更新时间:2023-11-04 10:31:54 24 4
gpt4 key购买 nike

我想将复杂的容器结构作为 SpringLayouts 的组合来管理(一系列独立的 SpringLayout 相互嵌套)

特别是,由包含 JPanel 的主 SpringLayout 管理的 JFrame:每个 JPanel 都应由其自己的 SpringLayout 管理。

不幸的是,如果我尝试应用两个不同的 SpringLayout,框架上什么也不会出现

我编写了这个简单的代码来向您展示问题。代码包含:

  • JFrame 及其 SprilgLayout
  • JPanel 及其 SpringLayout
  • JPanel 内的简单 JButton

代码如下:

import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

public class SpringLayout_cobination {

// THE OBJECTS
static JFrame frame;// The frame
static JPanel panel; // The container
static JButton Button; // Object

// THE LayOuts
static SpringLayout LayOut_Frame;
static SpringLayout LayOut_panel;
static Container contentPane;

public static void main(String[] args) {

// Start frame and componets
frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(830, 420);

panel=new JPanel();
Button=new JButton("ok");

panel.add(Button);
panel.setSize(new Dimension(100,100));
frame.add(panel);

// Set the layouts
LayOut_Frame=new SpringLayout(); // Frame LayOut
frame.setLayout(LayOut_Frame);
contentPane=frame.getContentPane(); // Main Container of the frame

LayOut_panel=new SpringLayout();
panel.setLayout(LayOut_panel);

LayOut_Frame.putConstraint(SpringLayout.NORTH, panel,30,SpringLayout.NORTH, contentPane);
LayOut_Frame.putConstraint(SpringLayout.WEST, panel,30,SpringLayout.WEST , contentPane);

LayOut_panel.putConstraint(SpringLayout.NORTH, contentPane,30,SpringLayout.NORTH, Button);
LayOut_panel.putConstraint(SpringLayout.WEST, contentPane,30,SpringLayout.WEST , Button);

frame.setVisible(true);
}

}

最佳答案

apply two different SpringLayout

如果我理解您的要求,我认为可以将 SpringLayout 组合如下:

import java.awt.*;
import javax.swing.*;

public class SpringLayoutCobinationTest {
private static Component makeSubPanel() {
JButton button = new JButton("ok");

SpringLayout layout = new SpringLayout();
JPanel pnl = new JPanel(layout);

layout.putConstraint(SpringLayout.NORTH, button, 10, SpringLayout.NORTH, pnl);
layout.putConstraint(SpringLayout.WEST, button, 10, SpringLayout.WEST, pnl);

pnl.add(button);
pnl.setBorder(BorderFactory.createLineBorder(Color.RED));

return pnl;
}
public Component makeUI() {
Component sub = makeSubPanel();

SpringLayout layout = new SpringLayout();
SpringLayout.Constraints c = layout.getConstraints(sub);
c.setWidth(Spring.constant(100));
c.setHeight(Spring.constant(100));

JPanel panel = new JPanel(layout);
layout.putConstraint(SpringLayout.NORTH, sub, 30, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, sub, 30, SpringLayout.WEST, panel);
panel.add(sub);

return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.add(new SpringLayoutCobinationTest().makeUI());
f.setSize(420, 420);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}

关于Java Swing : manage multiple SpringLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49895275/

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