[英]How to check if mouse pressed and still pressed on a button in java swing app
I have array of buttons and what I want is that when I press button and drag to another button color the path that I travel. 我有一系列按钮,而我想要的是当我按下按钮并将其拖动到另一个按钮时,为我的行进路线着色。
When I press a button it will change it's color by mousePressed method, then I drag to next button while still pressing, and will change the color of the 2nd button too. 当我按下一个按钮时,它将通过mousePressed方法更改其颜色,然后在按住该按钮的同时拖动到下一个按钮,并且也会更改第二个按钮的颜色。 How can I do this?
我怎样才能做到这一点?
This is the main class 这是主班
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();
}
}
and there is posbutton class that implements mouselistener. 并且有posbutton类实现mouselistener。
You could set up a glass pane that detects the mouse events, but it might get tricky if you're interfering with the default behavior of the JButtons. 您可以设置一个玻璃面板来检测鼠标事件,但是如果您干扰了JButton的默认行为,它可能会变得棘手。 Worth checking out though: https://weblogs.java.net/blog/alexfromsun/archive/2006/09/a_wellbehaved_g.html
虽然值得一试: https : //weblogs.java.net/blog/alexfromsun/archive/2006/09/a_wellbehaved_g.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.