我想在两个JPanel之间绘制线,但是线未出现在layeredPane上。
这是我所做的,请仔细阅读,可编译。请尝试更正此代码。我已经尝试过以这种方式在内部框架上绘制线条,但不适用于JPanels。
package build;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class LinkLayerPane1 {
public static void main(String[] args) {
new LinkLayerPane1();
}
public LinkLayerPane1() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JLayeredPane layer = new JLayeredPane();
JPanel red = new JPanel();
red.setBackground(Color.RED);
JPanel blue = new JPanel();
blue.setBackground(Color.BLUE);
red.setBounds(50, 50, 50, 50);
blue.setBounds(125, 125, 50, 50);
layer.add(red);
layer.add(blue);
Point Red= new Point();
Red.x=red.getX()+(red.getWidth()/2);
Red.y=red.getY()-(red.getHeight()/2);
Point Blue= new Point();
Blue.x=blue.getX()+(blue.getWidth()/2);
Blue.y=blue.getY()-(blue.getHeight()/2);
handleDrag(red,Blue);
handleDrag(blue,Red);
layer.add(new LinkPane(red, blue), new Integer(JLayeredPane.DEFAULT_LAYER + 1));
layer.setPreferredSize(new Dimension(250, 250));
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(layer);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LinkPane extends JPanel {
private Point startPoint;
private Point endPoint;
public LinkPane(JPanel panel1, JPanel panel2) {
setOpaque(false);
Point p1 = panel1.getLocation();
Point p2 = panel2.getLocation();
startPoint = new Point();
endPoint = new Point();
Point from = new Point();
Point to = new Point();
if (p1.x < p2.x) {
from.x = p1.x + (panel1.getWidth() / 2);
to.x = p2.x + (panel2.getWidth() / 2);
} else {
from.x = p2.x + (panel2.getWidth() / 2);
to.x = p1.x + (panel1.getWidth() / 2);
startPoint.x = p2.x;
}
if (p1.y < p2.y) {
from.y = p1.y + (panel1.getHeight()/ 2);
to.y = p2.y + (panel2.getHeight()/ 2);
} else {
from.y = p2.y + (panel2.getHeight()/ 2);
to.y = p1.y + (panel1.getHeight()/ 2);
}
int width = Math.max(1, to.x - from.x);
int height = Math.max(1, to.y - from.y);
setLocation(from);
setSize(width, height);
System.out.println(getBounds());
if (p1.x < p2.x) {
startPoint.x = 0;
endPoint.x = getWidth();
} else {
startPoint.x = getWidth();
endPoint.x = 0;
}
if (p1.y < p2.y) {
startPoint.y = 0;
endPoint.y = getHeight();
} else {
startPoint.y = getHeight();
endPoint.y = 0;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
g2d.dispose();
}
}
//Dragging
public static void handleDrag(final JPanel Tpanel,final Point to)
{
final JPanel p = Tpanel;
Tpanel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent me) {
me.translatePoint(me.getComponent().getLocation().x, me.getComponent().getLocation().y);
//Handling Panel Dragging
p.setLocation(me.getX(), me.getY());
//Finding new point
Point p1 = Tpanel.getLocation();
Point p2=to;
Point startPoint = new Point();
Point endPoint = new Point();
Point from = new Point();
if (p1.x < p2.x) {
from.x = p1.x + (Tpanel.getWidth() / 2);
to.x = p2.x + (Tpanel.getWidth() / 2);
} else {
from.x = p2.x + (Tpanel.getWidth() / 2);
to.x = p1.x + (Tpanel.getWidth() / 2);
startPoint.x = p2.x;
}
if (p1.y < p2.y) {
from.y = p1.y + (Tpanel.getHeight()/ 2);
to.y = p2.y + (Tpanel.getHeight()/ 2);
} else {
from.y = p2.y + (Tpanel.getHeight()/ 2);
to.y = p1.y + (Tpanel.getHeight()/ 2);
}
int width = Math.max(1, to.x - from.x);
int height = Math.max(1, to.y - from.y);
if (p1.x < p2.x) {
startPoint.x = 0;
endPoint.x = Tpanel.getWidth();
} else {
startPoint.x = Tpanel.getWidth();
endPoint.x = 0;
}
if (p1.y < p2.y) {
startPoint.y = 0;
endPoint.y = Tpanel.getHeight();
} else {
startPoint.y = Tpanel.getHeight();
endPoint.y = 0;
}
}
});
}
}
我很困惑。您从扩展了类JDesktop
,但随后将其添加到JLayedPane
… JDesktop
扩展中。
默认情况下,如果没有布局管理器,JDesktopPane
则会将其添加到JLayeredPane
,大小为0x0。
我不确定您希望借此实现什么。
更新示例
您面临的问题是paintComponent始终在组件下绘制,而paint从理论上讲会在组件上绘制,因此可以
将其排除在更新之外(因为RepaintManager可以直接引导需要更新的组件)。
虽然我仍然认为使用玻璃面板是最简单的解决方案,展示了这里,你可以简单地利用
图层面板中加入了“连接”窗格中绘制一条直线从一个点到另一个,只需将它添加到LayeredPane…
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class LinkLayerPane {
public static void main(String[] args) {
new LinkLayerPane();
}
public LinkLayerPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JLayeredPane layer = new JLayeredPane();
JPanel red = new JPanel();
red.setBackground(Color.RED);
JPanel blue = new JPanel();
blue.setBackground(Color.BLUE);
red.setBounds(50, 50, 50, 50);
blue.setBounds(125, 125, 50, 50);
layer.add(red);
layer.add(blue);
layer.add(new LinkPane(red, blue), new Integer(JLayeredPane.DEFAULT_LAYER + 1));
layer.setPreferredSize(new Dimension(250, 250));
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(layer);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LinkPane extends JPanel {
private Point startPoint;
private Point endPoint;
public LinkPane(JPanel panel1, JPanel panel2) {
setOpaque(false);
Point p1 = panel1.getLocation();
Point p2 = panel2.getLocation();
startPoint = new Point();
endPoint = new Point();
Point from = new Point();
Point to = new Point();
if (p1.x < p2.x) {
from.x = p1.x + (panel1.getWidth() / 2);
to.x = p2.x + (panel2.getWidth() / 2);
} else {
from.x = p2.x + (panel2.getWidth() / 2);
to.x = p1.x + (panel1.getWidth() / 2);
startPoint.x = p2.x;
}
if (p1.y < p2.y) {
from.y = p1.y + (panel1.getHeight()/ 2);
to.y = p2.y + (panel2.getHeight()/ 2);
} else {
from.y = p2.y + (panel2.getHeight()/ 2);
to.y = p1.y + (panel1.getHeight()/ 2);
}
int width = Math.max(1, to.x - from.x);
int height = Math.max(1, to.y - from.y);
setLocation(from);
setSize(width, height);
System.out.println(getBounds());
if (p1.x < p2.x) {
startPoint.x = 0;
endPoint.x = getWidth();
} else {
startPoint.x = getWidth();
endPoint.x = 0;
}
if (p1.y < p2.y) {
startPoint.y = 0;
endPoint.y = getHeight();
} else {
startPoint.y = getHeight();
endPoint.y = 0;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
g2d.dispose();
}
}
}
请记住,鼠标事件是相对于MouseListener已注册组件的事件。也就是说,如果您将登记MosueListener到中的任何
组件JLayeredPane,则鼠标事件将转换为相对于那里的坐标空间。这会使生活有些混乱。
本示例进行了两个重大更改。
它增加了一个Mouse/MotionListener到JLayeredPane。然后,该侦听器负责确定被单击的内容并管理流程
将的“设置”代码LinkPane移至updateLinks方法。在LinkPane随后注册了一个ComponentListener在每次被链接到面板,并更新链接时出现的位置或大小的变化…
import java.awt.BasicStroke; import java.awt.BorderLayout; import
java.awt.Color; import java.awt.Component; import java.awt.Dimension; import
java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D;
import java.awt.Point; import java.awt.event.ComponentAdapter; import
java.awt.event.ComponentEvent; import java.awt.event.MouseAdapter; import
java.awt.event.MouseEvent; import javax.swing.JFrame; import
javax.swing.JLayeredPane; import javax.swing.JPanel; import
javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException;
public class LinkLayerPane {
public static void main(String[] args) {
new LinkLayerPane();
}
public LinkLayerPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JLayeredPane layer = new JLayeredPane();
JPanel red = new JPanel();
red.setBackground(Color.RED);
JPanel blue = new JPanel();
blue.setBackground(Color.BLUE);
red.setBounds(50, 50, 50, 50);
blue.setBounds(125, 125, 50, 50);
layer.add(red);
layer.add(blue);
layer.add(new LinkPane(red, blue), new Integer(JLayeredPane.DEFAULT_LAYER + 1));
layer.setPreferredSize(new Dimension(250, 250));
MouseAdapter ma = new MouseAdapter() {
private Point offset;
private Component dragComponent;
@Override
public void mousePressed(MouseEvent e) {
JLayeredPane layer = (JLayeredPane) e.getComponent();
dragComponent = layer.getComponentAt(e.getPoint());
if (dragComponent != null && !dragComponent.equals(layer) && !(dragComponent instanceof LinkPane)) {
offset = e.getPoint();
offset.x = dragComponent.getX() - offset.x;
offset.y = dragComponent.getY() - offset.y;
} else {
dragComponent = null;
}
}
@Override
public void mouseReleased(MouseEvent e) {
dragComponent = null;
offset = null;
}
@Override
public void mouseDragged(MouseEvent e) {
if (dragComponent != null) {
Point dragTo = e.getPoint();
dragTo.x += offset.x;
dragTo.y += offset.y;
dragComponent.setLocation(dragTo);
}
}
};
layer.addMouseListener(ma);
layer.addMouseMotionListener(ma);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(layer);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LinkPane extends JPanel {
private Point startPoint;
private Point endPoint;
private JPanel[] links;
public LinkPane(JPanel panel1, JPanel panel2) {
setOpaque(false);
links = new JPanel[]{panel1, panel2};
ComponentAdapter ca = new ComponentAdapter() {
@Override
public void componentMoved(ComponentEvent e) {
updateLinks();
}
@Override
public void componentResized(ComponentEvent e) {
updateLinks();
}
};
links[0].addComponentListener(ca);
links[1].addComponentListener(ca);
updateLinks();
}
protected void updateLinks() {
Point p1 = links[0].getLocation();
Point p2 = links[1].getLocation();
startPoint = new Point();
endPoint = new Point();
Point from = new Point();
Point to = new Point();
if (p1.x < p2.x) {
from.x = p1.x + (links[0].getWidth() / 2);
to.x = p2.x + (links[1].getWidth() / 2);
} else {
from.x = p2.x + (links[1].getWidth() / 2);
to.x = p1.x + (links[0].getWidth() / 2);
startPoint.x = p2.x;
}
if (p1.y < p2.y) {
from.y = p1.y + (links[0].getHeight() / 2);
to.y = p2.y + (links[1].getHeight() / 2);
} else {
from.y = p2.y + (links[1].getHeight() / 2);
to.y = p1.y + (links[0].getHeight() / 2);
}
int width = Math.max(1, to.x - from.x);
int height = Math.max(1, to.y - from.y);
setLocation(from);
setSize(width, height);
System.out.println(getBounds());
if (p1.x < p2.x) {
startPoint.x = 0;
endPoint.x = getWidth();
} else {
startPoint.x = getWidth();
endPoint.x = 0;
}
if (p1.y < p2.y) {
startPoint.y = 0;
endPoint.y = getHeight();
} else {
startPoint.y = getHeight();
endPoint.y = 0;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
g2d.dispose();
}
}
}
主要内容:1 Java JDesktopPane的介绍,2 Java JDesktopPane的字段,3 Java JDesktopPane的构造方法,4 Java JDesktopPane的案例1 Java JDesktopPane的介绍 JDesktopPane类可用于创建“多文档”应用程序。一个多文档应用程序可以包含许多窗口。我们通过在主窗口中将contentPane作为JDesktopPane类或子类的实例来实现。内部窗口将JInternalFrame实例添加到JdesktopPane实
我有表单,我想在我的上显示它们。主窗体是MDI容器,所以我只想知道如何显示主雇员窗体。 下面是创建目标代码的: 这里是调用,尽管它不起作用。 我该如何解决这个问题?我想让表单显示在Jdesktoppane上 它现在正在工作。我使用了以下参考。
最初,右下角的空白区域是一个面板,它旨在绘制标本、位置和旅行路径的视觉表示(并不重要)。但是,您将能够打开某种类型的窗口,允许您创建/编辑不同的项目(如物种、位置和旅行路径)。最初我计划让这些只是弹出窗口。但是,我在想,我可能会使用弹出窗口和视觉表示屏幕的J内部窗格。 所以在我的JFrames构造函数中: 在图形面板构造函数中:
我是robolectric新手,我只想运行简单测试。 我使用Android Studio=>Gradle 如何找到错误的真正源头?零点异常在哪里?
我有一个叫做“菜单项目”的框架。 我有另一个称为"Main_Menu"的框架,它有一个JButton,点击按钮后,我想在JDesktopPane中调用框架"Menu_Project"。我试图遵循一个教程YouTube在这个链接:https://www.youtube.com/watch?v=TUL3qEoZkR4 当我运行它时,我得到: 我的代码: 谁能指出我错在哪里。我下面的视频教程。
几个小时以来,我一直在尝试这段代码的不同变体。我是个新手。我似乎无法理解为什么phpMyAdmin数据库中没有插入任何内容。我已验证我已连接到数据库(使用下面的echo和error函数)。我知道mysql_uu正处于被弃用的过程中,但请帮助Bourga解决这个问题。我的问题又来了:为什么什么都不插?我使用的是PHP4.0.10.7(Godaddy)。 回显$insertemail返回 在用户(电子