Java Swing:管理多个SpringLayout
Java Swing: manage multiple SpringLayout
我想管理一个复杂结构的容器作为 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);
});
}
}
我忘记的部分代码是:
SpringLayout.Constraints c = LayOut_Frame.getConstraints(panel);
c.setWidth(Spring.constant(200));
c.setHeight(Spring.constant(100));
似乎每次在主框架中导入组件时都必须施加一个Constraints维度
在我的示例中,当我导入
最终代码为:
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Spring;
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_panel.putConstraint(SpringLayout.NORTH, contentPane,30,SpringLayout.NORTH, Button);
LayOut_panel.putConstraint(SpringLayout.WEST, contentPane,30,SpringLayout.WEST , Button);
SpringLayout.Constraints c = LayOut_Frame.getConstraints(panel);
c.setWidth(Spring.constant(200));
c.setHeight(Spring.constant(100));
LayOut_Frame.putConstraint(SpringLayout.NORTH, panel,30,SpringLayout.NORTH, contentPane);
LayOut_Frame.putConstraint(SpringLayout.WEST, panel,30,SpringLayout.WEST , contentPane);
frame.setVisible(true);
}
}
我想管理一个复杂结构的容器作为 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);
});
}
}
我忘记的部分代码是:
SpringLayout.Constraints c = LayOut_Frame.getConstraints(panel);
c.setWidth(Spring.constant(200));
c.setHeight(Spring.constant(100));
似乎每次在主框架中导入组件时都必须施加一个Constraints维度
在我的示例中,当我导入 最终代码为:
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Spring;
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_panel.putConstraint(SpringLayout.NORTH, contentPane,30,SpringLayout.NORTH, Button);
LayOut_panel.putConstraint(SpringLayout.WEST, contentPane,30,SpringLayout.WEST , Button);
SpringLayout.Constraints c = LayOut_Frame.getConstraints(panel);
c.setWidth(Spring.constant(200));
c.setHeight(Spring.constant(100));
LayOut_Frame.putConstraint(SpringLayout.NORTH, panel,30,SpringLayout.NORTH, contentPane);
LayOut_Frame.putConstraint(SpringLayout.WEST, panel,30,SpringLayout.WEST , contentPane);
frame.setVisible(true);
}
}