Tôi hiện đang phát triển một chương trình trong Java mà một sự kiện nhất định phải được kích hoạt chỉ khi người dùng nhấp chuột với cả hai bên trái và nhấp chuột phải vào một nút.Nút chuột nào là nút giữa?
Vì đó là một chút độc đáo, tôi quyết định đầu tiên kiểm tra điều này. Dưới đây là:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class GUI
{
private JFrame mainframe;
private JButton thebutton;
private boolean left_is_pressed;
private boolean right_is_pressed;
private JLabel notifier;
public GUI()
{
thebutton = new JButton ("Double Press Me");
addListen();
thebutton.setBounds (20, 20, 150, 40);
notifier = new JLabel (" ");
notifier.setBounds (20, 100, 170, 20);
mainframe = new JFrame ("Double Mouse Tester");
mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
mainframe.setResizable (false);
mainframe.setSize (400, 250);
mainframe.setLayout (null);
mainframe.add (thebutton);
mainframe.add (notifier);
mainframe.setVisible (true);
left_is_pressed = right_is_pressed = false;
}
private void addListen()
{
thebutton.addMouseListener (new MouseListener()
{
@Override public void mouseClicked (MouseEvent e) { }
@Override public void mouseEntered (MouseEvent e) { }
@Override public void mouseExited (MouseEvent e) { }
@Override public void mousePressed (MouseEvent e)
{
//If left button pressed
if (e.getButton() == MouseEvent.BUTTON1)
{
//Set that it is pressed
left_is_pressed = true;
if (right_is_pressed)
{
//Write that both are pressed
notifier.setText ("Both pressed");
}
}
//If right button pressed
else if (e.getButton() == MouseEvent.BUTTON3)
{
//Set that it is pressed
right_is_pressed = true;
if (left_is_pressed)
{
//Write that both are pressed
notifier.setText ("Both pressed");
}
}
}
@Override public void mouseReleased (MouseEvent e)
{
//If left button is released
if (e.getButton() == MouseEvent.BUTTON1)
{
//Set that it is not pressed
left_is_pressed = false;
//Remove notification
notifier.setText (" ");
}
//If right button is released
else if (e.getButton() == MouseEvent.BUTTON3)
{
//Set that it is not pressed
right_is_pressed = false;
//Remove notification
notifier.setText (" ");
}
}
});
}
}
Tôi đã thử nghiệm và hoạt động, nhưng có sự cố.
Như bạn có thể thấy, nút chuột trái được thể hiện bằng MouseEvent.BUTTON1
và nút chuột phải bằng MouseEvent.BUTTON3
.
Nếu người dùng có con chuột không có bánh xe cuộn (dường như những con chuột này vẫn tồn tại), thì chỉ có hai nút được đặt trong MouseEvent. Điều đó có nghĩa là nút bên phải sẽ được đại diện bởi MouseEvent.BUTTON2
thay vì MouseEvent.BUTTON3
? Nếu có, làm cách nào tôi có thể thay đổi mã của mình để chứa mã này? Có cách nào tôi có thể phát hiện một cái gì đó như thế này?
Tôi đọc bất cứ điều gì tôi có thể tìm thấy trên giao diện MouseListener và trên MouseEvent, nhưng tôi không thể tìm thấy điều gì đó về điều này.
@PetarMinchev điều này sẽ không là vấn đề nếu tôi là người dùng duy nhất ... nhưng tôi sẽ xuất bản chương trình của mình trực tuyến để nhiều người có thể sử dụng nó (hoặc ít nhất là thử). –
Đúng, tôi chỉ đùa thôi :) –
Có 3 nút chuột không có bánh xe cuộn. – Ingo