我有一系列按钮,我想要的是当我按下按钮并拖动到另一个按钮颜色时,我行进的路径。
当我按下一个按钮时,它会通过mousePressed方法改变它的颜色,然后我在按下时拖动到下一个按钮,并且也会改变第二个按钮的颜色。我怎么能这样做?
这是主要的课程
public class Flowcolor extends JFrame{
public Flowcolor(int rows,int cols,int hgap,int vgap){
setTitle("flow color");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
/**top panel in north of borderlayout
* have two label time , number of moves
* menue button
*/
JPanel toppanel = new JPanel();
add(toppanel,BorderLayout.NORTH);
toppanel.setLayout(new BoxLayout(toppanel,BoxLayout.X_AXIS));
JTextField t1=new JTextField("Time:");
t1.setEditable(false); // can't write in this textfeild
JTextField t2=new JTextField("movements number:");
t2.setEditable(false);
toppanel.add(t1);
toppanel.add(t2);
JPanel centerpanel = new JPanel();
add(centerpanel,BorderLayout.CENTER);
centerpanel.setBorder(BorderFactory.createLineBorder(Color.blue,3));
centerpanel.setLayout(new GridLayout(rows,cols));
PosButton z[][]=new PosButton[rows+1][cols+1];
for (int i=1;i<=rows;i++)
{
for (int j=1;j<=cols;j++)
{
PosButton btn= new PosButton(" ",Color.DARK_GRAY,Color.lightGray);
z[i][j]=btn;
System.out.println(i+" "+j);
centerpanel.add( z[i][j]);
}
}
setVisible(true);
}
public static void main(String args[])
{
Flowcolor z= new Flowcolor (3,3,0,0);
//startmenu s=new startmenu();
}
}
并且有posbutton类实现了mouselistener。
答案 0 :(得分:0)
您可以设置一个检测鼠标事件的玻璃窗格,但如果您干扰JButton的默认行为,则可能会变得棘手。值得一试:https://weblogs.java.net/blog/alexfromsun/archive/2006/09/a_wellbehaved_g.html