当前位置: 技术问答>java相关
如何使JPanel先于其上的Component得到MouseEvent
来源: 互联网 发布时间:2015-06-19
本文导语: 我在JPanel中添加了若干Component,当点击这些Component时,我希望能直接触发JPanel的鼠标事件,而不是在Component的时间处理中再去向JPanel转发事件。 | javax.swing.SwingUtilities.convertMouseEvent(Component source...
我在JPanel中添加了若干Component,当点击这些Component时,我希望能直接触发JPanel的鼠标事件,而不是在Component的时间处理中再去向JPanel转发事件。
|
javax.swing.SwingUtilities.convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
|
不知如下代码对你有没有帮助:
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
public class JListDemo extends Frame {
Panel panelShow = new Panel();
JList dataList;
BorderLayout bordL = new BorderLayout();
JTextField textField;
public JListDemo() {
try {
textField=new JTextField("first");
String[] data = {"one", "two", "free", "four"};
JList dataList = new JList(data);
panelShow.setLayout(bordL);
dataList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
textField.setText("change");
}
});
dataList.setVisibleRowCount(12);
// Add list to a scrollpane
JScrollPane scrollPane = new JScrollPane(dataList);
panelShow.add(scrollPane);
panelShow.add(textField, BorderLayout.SOUTH);
this.add(panelShow, BorderLayout.CENTER);
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JListDemo JListDemo = new JListDemo();
JListDemo.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JListDemo.setBounds (120,120,300,300);
JListDemo.show ();
try
{
java.net.URL url1 = new java.net.URL("http://192.168.0.222/customs/index.html");
java.net.URLConnection con1 = url1.openConnection();
con1.setDoOutput(true);
con1.setUseCaches(false);
con1.setDoInput(true);
con1.setRequestProperty("Content-type", "application/octet-stream");
}
catch(Exception e)
{
e.printStackTrace();
}
JListDemo.show ();
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
public class JListDemo extends Frame {
Panel panelShow = new Panel();
JList dataList;
BorderLayout bordL = new BorderLayout();
JTextField textField;
public JListDemo() {
try {
textField=new JTextField("first");
String[] data = {"one", "two", "free", "four"};
JList dataList = new JList(data);
panelShow.setLayout(bordL);
dataList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
textField.setText("change");
}
});
dataList.setVisibleRowCount(12);
// Add list to a scrollpane
JScrollPane scrollPane = new JScrollPane(dataList);
panelShow.add(scrollPane);
panelShow.add(textField, BorderLayout.SOUTH);
this.add(panelShow, BorderLayout.CENTER);
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JListDemo JListDemo = new JListDemo();
JListDemo.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JListDemo.setBounds (120,120,300,300);
JListDemo.show ();
try
{
java.net.URL url1 = new java.net.URL("http://192.168.0.222/customs/index.html");
java.net.URLConnection con1 = url1.openConnection();
con1.setDoOutput(true);
con1.setUseCaches(false);
con1.setDoInput(true);
con1.setRequestProperty("Content-type", "application/octet-stream");
}
catch(Exception e)
{
e.printStackTrace();
}
JListDemo.show ();
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。