|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
[Java] KeyListener...
...ho un JFrame a cui aggiungo e rimuovo dei JPanel di piccole porzioni adibite a diversi utilizzi...ora vorrei che ogni porzione notificasse eventuali movimenti del mouse nel caso in cui ci sia collisione con uno dei loro elementi ma nel momento in cui abilito un pannello il precedente perde il keylistener...come risolvere?...
|
|
|
|
|
|
#2 |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
...devo solo capire se è sbagliato usare tanti keylistener quanti sono i pannelli usati...se è cioè consigliato adottare un singolo keylistener nel main frame che periodicamente interroga i singoli pannelli attivi su collisioni degli elementi...
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Un javax.swing.JComponent (o java.awt.Componet) può richiedere gentilmente al window manager del sistema ospitante il keyboard focus tramite il metodo 'requestFocusInWindow'
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 01-12-2010 alle 17:16. |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
ally puoi cercare di essere più preciso nello spiegare il tuo problema? Prima parli di movimenti del mouse e poi parli di key listener... così non si capisce...
Devi ascoltare/gestire eventi del mouse o della testiera? Gli eventi del mouse sono determinabili a prescindere dal keybord focus...
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 01-12-2010 alle 17:14. |
|
|
|
|
|
#5 | |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
Quote:
Codice:
private class CursorChanger extends MouseMotionAdapter {
@Override
public void mouseMoved(MouseEvent e) {
systemLog("mouse header "+e.getX()+" "+e.getY());
if(getHeaderHit(e.getX(), e.getY())>0)
{
getParent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else {
getParent().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
}
|
|
|
|
|
|
|
#6 | |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
Quote:
|
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Ok, e questo è chiaro adesso.
Ho letto anche il tuo post precedente, ma non riesco a capire la situazione Comunque sapendo che: 1) un mouse listener fa quello che il suo codice dice solo per i componeneti presso cui è stato registrato; 2) un Cursor può essere settato solo relativamente a un certo Component, e perchè le modifiche al Cursor abbiano valore per il dato Component bisogna che prima il Component stesso sia: - displayable - visible - enabled altrimenti ciccia
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 01-12-2010 alle 17:42. |
|
|
|
|
|
#8 |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
...un esempio vale piu' di mille parole...
...il MainFrame... Codice:
public class MainFrame {
JFrame mainFrame;
JPanel1 jpanel1;
JPanel2 jpanel2;
public static void main(String args[]){
new MainFrame();
}
public MainFrame(){
//main class per la gestione del layout
//fondamentalmente un frame a cui vengono aggiunti i vari layer
initPlayerLayoutLayout();
}
public void initPlayerLayoutLayout(){
mainFrame = new JFrame();
mainFrame.setLayout(new StackLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(600,800);
mainFrame.setTitle("Java");
mainFrame.setUndecorated(false);
mainFrame.setVisible(true);
if(jpanel1==null)
this.jpanel1 = new JPanel1();
mainFrame.add(this.jpanel1,StackLayout.TOP);
jpanel1.setVisible(true);
mainFrame.setVisible(true);
if(jpanel2==null)
this.jpanel2 = new JPanel2();
mainFrame.add(this.jpanel2,StackLayout.TOP);
jpanel2.setVisible(true);
mainFrame.setVisible(true);
}
}
Codice:
public class JPanel1 extends JPanel{
private BufferedImage image;
private CursorChanger cursorChanger;
public JPanel1() {
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
loadHeaderImages();
this.initInputListeners();
this.addInputListeners();
}
private void loadHeaderImages(){
try{
image = ImageIO.read(new File("home.png"));
}catch (IOException e){
e.printStackTrace();
}
}
public Dimension getPreferredSize() {
return new Dimension(128 * 3, 128 * 2);
}
public Dimension getMaximumSize() {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
public boolean isOpaque() {
return false;
}
public boolean isFocusable() {
return true;
}
protected void paintChildren(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Composite oldComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
super.paintChildren(g);
g2.setComposite(oldComposite);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//Insets insets = getInsets();
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Composite oldComposite = g2.getComposite();
this.drawHeader(g2);
g2.setComposite(oldComposite);
}
private void drawHeader(Graphics2D g2){
g2.drawImage(image, 20, 50, null);
}
private int getHeaderHit(int x,int y){
int hit = 0;
try{
if(image.getRGB(x-20, y-50)<0)
hit = 1;
}catch(Exception e){
}
return hit;
}
private void addInputListeners() {
addMouseMotionListener(cursorChanger);
}
private void initInputListeners() {
cursorChanger = new CursorChanger();
}
private class CursorChanger extends MouseMotionAdapter {
public void mouseMoved(MouseEvent e) {
systemLog("mouse panel 1 "+e.getX()+" "+e.getY());
if(getHeaderHit(e.getX(), e.getY())>0)
{
getParent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else {
getParent().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
}
public void systemLog(String string){
System.out.println(string);
}
}
|
|
|
|
|
|
#9 |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
...per ora ho risolto agganciando il mouse listener nel main frame e richiamando i metodi di collisione dei pannelli visibili ad ogni movimento...non vorrei essermi perso qualcosa sui listener attivabili simultaneamente...
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 22:16.




















