我已经在谷歌上搜索了几个小时,尝试了一百万种不同的细微变化,但都无济于事,所以我认为最好的选择是挑选比我更熟练的人的大脑。
我正在编写一个从数据库加载一堆按钮的类,目标是让用户能够根据自己的喜好排列按钮,但是,由于某种原因,我似乎无法让框架重新验证或重新粉刷。按钮会四处移动,但不会像我编码的那样重新排列。该组织似乎工作正常,即释放鼠标按钮时进行重新排列所涉及的代码,只是组件将仅停留在它们被拖放的位置,即使它们在各自的列表中重新排序。
代码很长,我不想发布整个课程,因为它可能会关闭一些人,但我不知道我在哪里犯了错误,所以我认为发布代码最符合我的利益整件事情。主要关注的领域是mouseReleased(MouseEvent e) {...}
/repaint()
方法refresh()
,但是,我可能在其他地方遗漏了一些东西。
tl; dr:我基本上只是setBounds()
在用户按照他们想要的顺序拖放按钮后尝试执行 a ,但按钮保持在它们被拖放的同一位置,并且不会revalidate()
or repaint()
。我什removeAll
至无法清除组件面板并重新加载。
提前谢谢你。这是我的代码:
public class AdminButtonEditor extends javax.swing.JFrame {
public AdminButtonEditor(OrderView parent) { ...
...
Component[] components = buttonsPanel.getComponents();
for (int i = 0; i < components.length; i++) {
Component c = components[i];
if (c instanceof JButton) {
JButton jb = (JButton) c;
jb.setFocusable(false);
buttons.add(new MyJButton(...));
}
}
for (int i = 0; i < buttons.size(); i++) {
buttons.get(i).addTo(editPanel);
buttons.get(i).orderIndex=modButtonList.get(i).menuModifier.getViewOrderValue();
buttons.get(i).idx=i;
}
EventHandler eh = new EventHandler();
addWindowListener(eh);
editPanel.addMouseMotionListener(eh);
editPanel.addMouseListener(eh);
contentPane.add(editPanel, BorderLayout.CENTER);
}
protected void refresh() {
if (!buttons.isEmpty() && buttons.get(0) != null) {
contentPane.remove(editPanel);
editPanel.removeAll();
for (int i = 0; i < buttons.size(); i++) {
MyJButton s = buttons.get(i);
s.addTo(editPanel);
}
contentPane.add(editPanel, BorderLayout.CENTER);
editPanel.repaint();
}
}
public void paint(Graphics g) {
refresh();
super.paint(g);
}
private int getSelectionIndex(int x, int y) {
int s=-1;
for (int i=buttons.size()-1; i>=0;i--){
if (buttons.get(i).contains(x, y)) {
s = i;
break;
}
}
return s;
}
private class EventHandler implements MouseInputListener,WindowListener, ActionListener {
private int selectionIndex, startX, startY, lastX, lastY;
private MyJButton selected;
private boolean moving=false;
....
....
public void mouseReleased(MouseEvent e) {
if (moving){
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
moving=false;
int dropX=e.getX();
int dropY=e.getY();
int row = dropY/selected.height;
int col = dropX/selected.width;
int idx=(row*3+col)-1;
int oldIdx=buttons.indexOf(selected);
insertIntoList(idx,oldIdx);
}
if (selected!=null){
selected.unHighLight();
selected=null;
}
Collections.sort(buttons);
for (int i=0;i<buttons.size();i++){
//modButtonList.get(buttons.get(i).idx).menuModifier.setViewOrderValue(buttons.get(i).orderIndex);
}
editPanel.validate();
repaint();
}
private void insertIntoList(int idx, int oldIdx) {
MyJButton temp = buttons.get(idx);
int tempid=buttons.get(idx).idx;
buttons.set(idx, new MyJButton(selected.text,selected.x,selected.y,selected.width,selected.height,selected.idx));
buttons.get(idx).orderIndex=temp.orderIndex;
if (idx<oldIdx){
int id;
for (int i=oldIdx;i>idx+1;i--){
id=buttons.get(i).orderIndex;
buttons.set(i, new MyJButton(buttons.get(i-1).text,buttons.get(i-1).x,buttons.get(i-1).y,buttons.get(i-1).width,buttons.get(i-1).height,buttons.get(i-1).idx));
buttons.get(i).orderIndex=id;
}
id = buttons.get(idx+1).orderIndex;
buttons.set(idx+1,new MyJButton(temp.text,temp.x,temp.y,temp.width,temp.height,temp.idx));
buttons.get(idx+1).orderIndex=id;
} else if (idx>oldIdx) {
int id;
for (int i=oldIdx;i<idx-1;i++){
id=buttons.get(i).orderIndex;
buttons.set(i, new MyJButton(buttons.get(i+1).text,buttons.get(i+1).x,buttons.get(i+1).y,buttons.get(i+1).width,buttons.get(i+1).height,buttons.get(i+1).idx));
buttons.get(i).orderIndex=id;
}
id = buttons.get(idx-1).orderIndex;
buttons.set(idx-1,new MyJButton(temp.text,temp.x,temp.y,temp.width,temp.height,temp.idx));
buttons.get(idx-1).orderIndex=id;;
} else {
buttons.get(idx).x=buttons.get(idx).originx;
buttons.get(idx).y=buttons.get(idx).originy;
}
repaint();
}
public void mouseDragged(MouseEvent e) {
if (moving) {
Graphics g = editPanel.getGraphics();
g.setColor(Color.black);
g.drawLine(selected.getXPos(), 0, selected.getXPos(),editPanel.getWidth());
g.drawLine(0, selected.getYPos(), editPanel.getHeight(), selected.getYPos());
selected.moveBy(e.getX()-lastX, e.getY()-lastY);
g.setXORMode(Color.black);
g.drawLine(selected.getXPos(), 0, selected.getXPos(), editPanel.getWidth());
g.drawLine(0, selected.getYPos(), editPanel.getHeight(), selected.getYPos());
lastX=e.getX();
lastY=e.getY();
repaint();
}
}
....
}
private class MyJButton extends JButton implements Comparable {
private int orderIndex,idx;
private int x, y, width, height,originx,originy;
private String text;
public Border DEFAULT_BORDER;// new SoftBevelBorder(BevelBorder.RAISED);
public Border SELECT_BORDER = BorderFactory.createLineBorder(Color.RED, 3, true);
public MyJButton(String text, int x, int y, int width, int height) {
....
setFocusable(false);
}
public MyJButton(String text, int x, int y, int width, int height,int idx) {....}
public void addTo(JPanel p) {
setBounds(x, y, width, height);
p.add(this);
}
@Override
public boolean contains(int x, int y) {
int x1 = x, y1 = y;
if (x1 >= this.x && y1 >= this.y && x1 <= this.x + width && y1 <= this.y + height) {
return true;
} else {
return false;
}
}
@Override
public void setSize(int w, int h) {
width = w;
height = h;
}
....
public void moveBy(int dx, int dy) {
x += dx;
y += dy;
}
@Override
public void resize(int newWidth, int newHeight) {
this.width = newWidth;
this.height = newHeight;
setBounds(x, y, width, height);
}
public int compareTo(Object o) {
MyJButton mjb = (MyJButton)o;
return this.idx-mjb.idx;
}
}
}