Mouse Tracker em Java
Boa noite, estou criando um Mouse tracker para minha aplicação mas me deparei com o seguinte problema:
Tenho o código completo do mouse tracker e o mesmo funciona bem, entretanto não estou conseguindo coloca-lo no form principal da minha aplicação.
código do mouse tracker
código do Jframe principal da minha aplicação
Agradeço a ajuda desde já.
Não estou pedindo o código pronto, só uma explicação de como faria para colocar o texto que fala sobre a coordenada que o mouse se encontra na parte inferior da minha aplicação, dentro do JPanel Comandos.
Tenho o código completo do mouse tracker e o mesmo funciona bem, entretanto não estou conseguindo coloca-lo no form principal da minha aplicação.
código do mouse tracker
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Interface;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
/**
*
* @author LeonardoCezar
*/
public class MouseTracker extends JFrame
implements MouseListener, MouseMotionListener {
JLabel mousePosition;
public static void main(String args[])
{
new MouseTracker().start();
}
@Override
public void mouseClicked(MouseEvent e) {
mousePosition.setText("Mouse clicked at coordinate : ["+e.getX()+","+e.getY()+"]");
}
@Override
public void mouseEntered(MouseEvent e) {
mousePosition.setText("Current mouse Coordinates : ["+e.getX()+","+e.getY()+"]");
}
@Override
public void mouseExited(MouseEvent e) {
mousePosition.setText("Mouse outside access window");
}
@Override
public void mousePressed(MouseEvent e) {
mousePosition.setText("Mouse pressed at coordinates : ["+e.getX()+","+e.getY()+"]");
}
@Override
public void mouseReleased(MouseEvent e) {
mousePosition.setText("Current mouse coordinates : ["+e.getX()+","+e.getY()+"]");
}
@Override
public void mouseDragged(MouseEvent e) {
mousePosition.setText("Mouse dragged at coordinates : ["+e.getX()+","+e.getY()+"]");
}
@Override
public void mouseMoved(MouseEvent e) {
mousePosition.setText("Mouse moved to coordinates : ["+e.getX()+","+e.getY()+"]");
}
public void start()
{
mousePosition=new JLabel();
addMouseListener( this ); // listens for own mouse and
addMouseMotionListener( this ); // mouse-motion events
setLayout(new FlowLayout(1));
add(mousePosition);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
setSize(size );
setUndecorated(true);
setVisible( true );
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}código do Jframe principal da minha aplicação
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Interface;
/**
*
* @author LeonardoCezar
*/
public class Home extends javax.swing.JFrame{
/**
* Creates new form Home
*/
public Home() {
initComponents();
jButtonCriarPonto.setToolTipText("Criar Ponto");
jButtonCriarLinha.setToolTipText("Criar Linha");
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanelDesenho = new javax.swing.JPanel();
jToolBar1 = new javax.swing.JToolBar();
jSeparator1 = new javax.swing.JToolBar.Separator();
jButtonCriarPonto = new javax.swing.JButton();
jSeparator2 = new javax.swing.JToolBar.Separator();
jButtonCriarLinha = new javax.swing.JButton();
jSeparator3 = new javax.swing.JToolBar.Separator();
jPanelComandos = new javax.swing.JPanel();
jInternalFramePrincipal = new javax.swing.JInternalFrame();
mousePosition = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenuArquivo = new javax.swing.JMenu();
jMenuItemAbrir = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("MEFSolver");
setExtendedState(MAXIMIZED_BOTH);
setPreferredSize(new java.awt.Dimension(600, 700));
jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jToolBar1.setFloatable(false);
jToolBar1.setRollover(true);
jToolBar1.add(jSeparator1);
jButtonCriarPonto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/ponto.PNG"))); // NOI18N
jButtonCriarPonto.setToolTipText("");
jButtonCriarPonto.setFocusable(false);
jButtonCriarPonto.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButtonCriarPonto.setMaximumSize(new java.awt.Dimension(40, 40));
jButtonCriarPonto.setMinimumSize(new java.awt.Dimension(40, 40));
jButtonCriarPonto.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButtonCriarPonto.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButtonCriarPontoMouseClicked(evt);
}
});
jButtonCriarPonto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonCriarPontoActionPerformed(evt);
}
});
jToolBar1.add(jButtonCriarPonto);
jToolBar1.add(jSeparator2);
jButtonCriarLinha.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/linha.PNG"))); // NOI18N
jButtonCriarLinha.setFocusable(false);
jButtonCriarLinha.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButtonCriarLinha.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButtonCriarLinha.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButtonCriarLinhaMouseClicked(evt);
}
});
jToolBar1.add(jButtonCriarLinha);
jToolBar1.add(jSeparator3);
javax.swing.GroupLayout jPanelDesenhoLayout = new javax.swing.GroupLayout(jPanelDesenho);
jPanelDesenho.setLayout(jPanelDesenhoLayout);
jPanelDesenhoLayout.setHorizontalGroup(
jPanelDesenhoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPanelDesenhoLayout.setVerticalGroup(
jPanelDesenhoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
);
jPanelComandos.setBorder(javax.swing.BorderFactory.createEtchedBorder());
javax.swing.GroupLayout jPanelComandosLayout = new javax.swing.GroupLayout(jPanelComandos);
jPanelComandos.setLayout(jPanelComandosLayout);
jPanelComandosLayout.setHorizontalGroup(
jPanelComandosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 557, Short.MAX_VALUE)
);
jPanelComandosLayout.setVerticalGroup(
jPanelComandosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 42, Short.MAX_VALUE)
);
jInternalFramePrincipal.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jInternalFramePrincipal.setClosable(true);
jInternalFramePrincipal.setVisible(true);
javax.swing.GroupLayout jInternalFramePrincipalLayout = new javax.swing.GroupLayout(jInternalFramePrincipal.getContentPane());
jInternalFramePrincipal.getContentPane().setLayout(jInternalFramePrincipalLayout);
jInternalFramePrincipalLayout.setHorizontalGroup(
jInternalFramePrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 557, Short.MAX_VALUE)
);
jInternalFramePrincipalLayout.setVerticalGroup(
jInternalFramePrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 567, Short.MAX_VALUE)
);
jMenuBar1.setBackground(new java.awt.Color(204, 204, 204));
jMenuBar1.setBorder(null);
jMenuBar1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
jMenuArquivo.setText("Arquivo");
jMenuItemAbrir.setText("Abrir");
jMenuArquivo.add(jMenuItemAbrir);
jMenuBar1.add(jMenuArquivo);
jMenu2.setText("Sair");
jMenu2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenu2MouseClicked(evt);
}
});
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanelComandos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanelDesenho, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jInternalFramePrincipal)
.addContainerGap()))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(mousePosition, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(433, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanelDesenho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 588, Short.MAX_VALUE)
.addComponent(jPanelComandos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jInternalFramePrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(38, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(645, Short.MAX_VALUE)
.addComponent(mousePosition, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap()))
);
pack();
}// </editor-fold>
private void jMenu2MouseClicked(java.awt.event.MouseEvent evt) {
dispose();
}
private void jButtonCriarPontoMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void jButtonCriarPontoActionPerformed(java.awt.event.ActionEvent evt) {
CriarPonto frame=new CriarPonto();
frame.setVisible(true);
// TODO add your handling code here:
}
private void jButtonCriarLinhaMouseClicked(java.awt.event.MouseEvent evt) {
CriarLinha frame=new CriarLinha();
frame.setVisible(true);
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Home().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonCriarLinha;
private javax.swing.JButton jButtonCriarPonto;
private javax.swing.JInternalFrame jInternalFramePrincipal;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenuArquivo;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItemAbrir;
private javax.swing.JPanel jPanelComandos;
private javax.swing.JPanel jPanelDesenho;
private javax.swing.JToolBar.Separator jSeparator1;
private javax.swing.JToolBar.Separator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JLabel mousePosition;
// End of variables declaration
}
Agradeço a ajuda desde já.
Não estou pedindo o código pronto, só uma explicação de como faria para colocar o texto que fala sobre a coordenada que o mouse se encontra na parte inferior da minha aplicação, dentro do JPanel Comandos.
Leonardo Santos
Curtidas 0