gpt4 book ai didi

java - JPanel 与 JScrollPane 内的 JComponent 和自定义 LayoutManager

转载 作者:行者123 更新时间:2023-11-30 03:11:59 26 4
gpt4 key购买 nike

我编写了 JComponent 的子类,它在 PaintComponent 方法中绘制自身。在构造函数中,我使用 setBounds 设置边界。这很好用。我将此子类添加到 JPanel 中。该 JPanel 位于 JScrollPane 内。此 JPanel 是 JPanel 的子类并实现 MouseInputListener 和 KeyListener。因此,每当用户在 JPanel 中单击时,都会根据鼠标点检查每个 JComponent 的边界。如果匹配,焦点将设置为该 JComponent。如果焦点位于 JComponent 并且用户拖动鼠标,则 JComponent 将移动到 JPanel 内。我想要实现的是,每当 JPanel 内的 JComponent 位置发生变化并离开 JScrollPane 的视口(viewport)时,滚动条就会变得可见,并且 JPanel 会调整大小,以便用户可以滚动到可视区域之外。为了实现这一点,我编写了一个自定义 LayoutManager,除了绝对位置之外,它不会以任何方式布局 JComponent。但是每当我将 JComponent 移动到 JPanel 内部和可视区域外部时,滚动条都不会更新。我怎样才能实现这个目标?

这是 JComponent 的子类:

public abstract class Gate extends JComponent {

/**
*
*/
private static final long serialVersionUID = 1L;

public Gate() {
super();
setBounds(new Rectangle(0, 0, 100, 100));
setBorder(BorderFactory.createLineBorder(Color.black));
setFocusable(true);
setOpaque(false);
setBackground(Color.white);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(hasFocus()) {
setBorder(BorderFactory.createLineBorder(Color.green));
} else {
setBorder(BorderFactory.createLineBorder(Color.black));
}
Graphics g2 = g.create();
g2.setColor(getBackground());
g2.fillRect(0, 0, getWidth(), getHeight());
g2.dispose();
}
}

这是 JPanel 的子类:

public class GatesPanel extends JPanel implements MouseInputListener, KeyListener {

/**
*
*/
private static final long serialVersionUID = 1L;

public GatesPanel() {
super();
setLayout(new GateLayout());
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
setFocusable(true);
}

@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
for(int i = 0; i < getComponentCount(); i++) {
Component c = getComponent(i);
if(c instanceof JComponent) {
JComponent j = (JComponent)c;
if(j.getBounds().contains(e.getPoint())) {
if(j.hasFocus()) {
grabFocus();
} else {
j.grabFocus();
}
}
}
}
}
}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseDragged(MouseEvent e) {

}

@Override
public void mouseMoved(MouseEvent e) {
for(int i = 0; i < getComponentCount(); i++) {
Component c = getComponent(i);
if(c.hasFocus()) {
c.setLocation(e.getPoint());
}
}
}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {

}

@Override
public void keyTyped(KeyEvent e) {

}
}

这是实现 LayoutManager 的类:

public class GateLayout implements LayoutManager {

private int minWidth;
private int minHeight;
private int preferedWidth;
private int preferedHeight;

public GateLayout() {
minWidth = 0;
minHeight = 0;
preferedWidth = 0;
preferedHeight = 0;
}

@Override
public void addLayoutComponent(String name, Component component) {

}

@Override
public void layoutContainer(Container parent) {
setSize(parent);
}

private void setSize(Container parent) {
minWidth = 0;
minHeight = 0;
preferedWidth = 0;
preferedHeight = 0;
for(int i = 0; i < parent.getComponentCount(); i++) {
Component c = parent.getComponent(i);
Dimension d = c.getPreferredSize();
minWidth = Math.max(d.width, minWidth);
minHeight = Math.max(d.height, minHeight);
preferedWidth += d.width;
preferedHeight += d.height;
}
}

@Override
public Dimension minimumLayoutSize(Container parent) {
setSize(parent);
Insets insets = parent.getInsets();
return new Dimension(minWidth + insets.left + insets.right,
minHeight + insets.top + insets.bottom);
}

@Override
public Dimension preferredLayoutSize(Container parent) {
setSize(parent);
Insets insets = parent.getInsets();
return new Dimension(preferedWidth + insets.left + insets.right,
preferedHeight + insets.top + insets.bottom);
}

@Override
public void removeLayoutComponent(Component component) {

}

}

在名为 GatesFrame 的 JFrame 子类中,我调用以下代码:

gatesPanel = new GatesPanel();
JScrollPane scrollPane = new JScrollPane(gatesPanel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setAutoscrolls(true);
setContentPane(scrollPane);

之后,我将名为 GateAND 的 Gate 子类添加到 GatesPanel:

gatesPanel.add(new GateAND());

那么,当用户在滚动 Pane 包围的面板内移动组件时,滚动条没有更新,我做错了什么?欢迎任何帮助。

格里兹,

最大pa1n87

最佳答案

To achive this I have written a custom LayoutManager which does not layout the JComponents in any way but the absolute position. But whenever I move a JComponent inside the JPanel and outside of the viewable area the scrollbars do not get updated.

也许您的布局管理器没有正确设置首选尺寸。当首选尺寸大于滚动 Pane 的尺寸时,滚动条将自动出现。

您还需要确保在完成拖动组件后在面板上调用 revalidate() 以便调用布局管理器。

查看 Drag Layout就是为此目的而设计的。

这是一个简单的例子:

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

public class DragLayoutTest
{
public static void main( String[] args )
{
DragListener drag = new DragListener();

DragLayout dl = new DragLayout();
dl.setUsePreferredSize(false);

JPanel panel = new JPanel( dl );
panel.setBorder( new MatteBorder(10, 10, 10, 10, Color.YELLOW) );

createLabel(drag, panel, "North", 150, 0);
createLabel(drag, panel, "West", 0, 100);
createLabel(drag, panel, "East", 300, 100);
createLabel(drag, panel, "South", 150, 200);
createLabel(drag, panel, "Center", 150, 100);

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame( "Drag Layout" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane(panel) );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}

public static void createLabel(MouseInputAdapter drag, JPanel panel, String text, int x, int y)
{
JLabel label = new JLabel( text );
label.setOpaque(true);
label.setBackground( Color.ORANGE );
label.setLocation(x, y);
panel.add( label );
label.addMouseListener( drag );
label.addMouseMotionListener( drag );
}

static class DragListener extends MouseInputAdapter
{
Point location;
MouseEvent pressed;

public void mousePressed(MouseEvent me)
{
pressed = me;
}

public void mouseDragged(MouseEvent me)
{
Component component = me.getComponent();
location = component.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
component.setLocation(x, y);
((JComonent)component.getParent()).revalidate();
}
}
}

关于java - JPanel 与 JScrollPane 内的 JComponent 和自定义 LayoutManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33445763/

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