可通过点击右键快捷菜单实现上移和下移 用java怎样实现
1个回答
2011-03-21
展开全部
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class Test {
public static void main(String[] args) throws Exception{
JPanel p=new JPanel(new FlowLayout(FlowLayout.CENTER,1,1));
JFrame f=new JFrame();
f.setSize(100,70);
JLabel l=new JLabel("1");
JLabel m=new JLabel("2");
f.setContentPane(p);
f.setDefaultCloseOperation(3);
l.setComponentPopupMenu(new P());
m.setComponentPopupMenu(new P());
p.add(l);
p.add(m);
l.setSize(100,20);
l.setPreferredSize(l.getSize());
l.setOpaque(true);
l.setBackground(Color.red);
m.setSize(l.getSize());
m.setPreferredSize(l.getSize());
m.setOpaque(true);
m.setBackground(Color.blue);
f.setVisible(true);
}
static class P extends JPopupMenu implements ActionListener{
P(){
add("UP").addActionListener(this);
add("DW").addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
boolean up=e.getActionCommand().equals("UP");
try{
JLabel l=(JLabel) getInvoker();
int zorder=l.getParent().getComponentZOrder(l);
zorder+=up?-1:1;
l.getParent().setComponentZOrder(l,zorder);
l.getParent().validate();
}catch(Exception ex){}
}
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class Test {
public static void main(String[] args) throws Exception{
JPanel p=new JPanel(new FlowLayout(FlowLayout.CENTER,1,1));
JFrame f=new JFrame();
f.setSize(100,70);
JLabel l=new JLabel("1");
JLabel m=new JLabel("2");
f.setContentPane(p);
f.setDefaultCloseOperation(3);
l.setComponentPopupMenu(new P());
m.setComponentPopupMenu(new P());
p.add(l);
p.add(m);
l.setSize(100,20);
l.setPreferredSize(l.getSize());
l.setOpaque(true);
l.setBackground(Color.red);
m.setSize(l.getSize());
m.setPreferredSize(l.getSize());
m.setOpaque(true);
m.setBackground(Color.blue);
f.setVisible(true);
}
static class P extends JPopupMenu implements ActionListener{
P(){
add("UP").addActionListener(this);
add("DW").addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
boolean up=e.getActionCommand().equals("UP");
try{
JLabel l=(JLabel) getInvoker();
int zorder=l.getParent().getComponentZOrder(l);
zorder+=up?-1:1;
l.getParent().setComponentZOrder(l,zorder);
l.getParent().validate();
}catch(Exception ex){}
}
}
}
追问
刚刚在Myeclipse上面运行了一下 没反应
追答
这搞不清楚了,我这边正常。JDK1.6_22
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询