首先,看一下用d3.js写的这个令人难以置信的例子:http://mbostock.github.io/d3/talk/20111018/partition.html
我试图模仿Java中的东西。我没有成功。我已经能够创建GUI,但动画证明是困难的。在玩了一些相当不成功的事情之后,我想我需要一些指导。
编辑:我不确定如何描述问题,但我会尝试。单击嵌套的JPanel时,我想关注此面板并展开其中的内容以填充整个窗口。非常类似于您在示例中看到的内容,如果您可以查看它。例如,当您单击标记为“vis”的蓝色块时,此块将聚焦其下方的所有内容。它非常像一棵树,但动画。我想我只是停留在如何按照描述的方式设计系统。我正在寻找如何解决问题的策略。如果我需要更具体,请告诉我。
package animation;
import java.awt.BorderLayout;
public class TestAnimation extends JFrame {
private boolean buttonActive = false;
private boolean boxActive = false;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestAnimation frame = new TestAnimation();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestAnimation() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setExtendedState(JFrame.MAXIMIZED_BOTH);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
ParentObj one = new ParentObj("Vblock", this);
}
}
以下是实例化的父对象的类:
package animation;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class ParentObj extends JComponent{
public boolean isActive = false;
public final JFrame frame;
public Obj1 compute;
public Obj1 storage;
public String name;
public Obj1 test;
public ParentObj(String n, JFrame f){
this.frame = f;
this.name = n;
final JPanel panelL = new JPanel();
panelL.setBackground(Color.blue);
panelL.setLayout(new BorderLayout());
panelL.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
System.out.println(name);
//startAnimations(box);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
JLabel title = new JLabel(name);
title.setForeground(Color.white);
title.setHorizontalAlignment(SwingConstants.CENTER);
panelL.add(title, BorderLayout.CENTER);
final JPanel panelR = new JPanel();
panelR.setLayout(new GridLayout(0, 1, 0, 0));
panelR.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
System.out.println(name);
//startAnimations(box);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
final JSplitPane panel = new JSplitPane(1, true, panelL, panelR);
panel.setDividerSize(0);
panel.setResizeWeight(0.3);
panel.setBorder(null);
panel.setBorder(new LineBorder(new Color(0, 0, 0)));
frame.add(panel, BorderLayout.CENTER);
compute = new Obj1("Compute", frame ,panelR);
storage = new Obj1("Storage", frame, panelR);
test = new Obj1("Network", frame, panelR);
}
}
最后,个别作品的小班:
package animation;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class Obj1 extends JComponent{
public String name;
public JComponent parent;
public JFrame frame;
final JSplitPane panel;
public Obj1(String n, JFrame f, JComponent p) {
this.name = n;
this.parent = p;
this.frame = f;
final JPanel panelL = new JPanel();
panelL.setBackground(Color.blue);
panelL.setLayout(new BorderLayout());
panelL.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
System.out.println(name);
startAnimations();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
JLabel title = new JLabel(name);
title.setForeground(Color.white);
title.setHorizontalAlignment(SwingConstants.CENTER);
panelL.add(title, BorderLayout.CENTER);
final JPanel panelR = new JPanel();
panelR.setLayout(new GridLayout(0, 1, 0, 0));
panelR.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
System.out.println(name);
//startAnimations(box);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
panel = new JSplitPane(1, true, panelL, panelR);
panel.setDividerSize(0);
panel.setResizeWeight(0.3);
panel.setBorder(new LineBorder(new Color(0, 0, 0)));
p.add(panel, BorderLayout.CENTER);
}
public void startAnimations(){
//panel.setSize(frame.getWidth(), frame.getHeight());
///JRootPane test = panel.getRootPane();
}
}
如果您对如何产生正确结果或是否有其他方法可以解决此问题,请告诉我。非常感谢你!