java问题请教,下面的一个程序,为什么我加了一个线程之后能显示时间了,但是点击菜单却没反应了
importjava.awt.*;importjava.awt.event.MouseEvent;importjava.awt.event.MouseAdapter;im...
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.util.*;
import java.text.SimpleDateFormat;
public class PopupTest implements Runnable{
JPanel timepanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel timelabel=new JLabel();
JFrame f=new JFrame("弹出菜单测试");
public PopupTest(){
f.setSize(300,400);
f.setLocation(200,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
f.add(timepanel);
timepanel();
final JPopupMenu popupmenu1=new JPopupMenu();
final JMenu bgmenu=new JMenu("背景");
final JMenuItem redcolorItem=new JMenuItem("红色");
final JMenuItem greencolorItem=new JMenuItem("绿色");
bgmenu.add(redcolorItem);
bgmenu.add(greencolorItem);
popupmenu1.add(bgmenu);
popupmenu1.setVisible(false);
redcolorItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.getContentPane().setBackground(Color.red);
}
});
greencolorItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.getContentPane().setBackground(Color.green);
}
});
f.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
popupmenu1.show(f, e.getX(), e.getY());
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
popupmenu1.show(f, e.getX(), e.getY());
}
}
});
}
public void timepanel(){
timepanel.add(timelabel);
}
public void run(){
while(true){
Date nowDate=new Date();
SimpleDateFormat newSDF=new SimpleDateFormat("hh:mm:ss");
timelabel.setText(newSDF.format(nowDate));
timelabel.setForeground(Color.red);
try {
Thread.sleep(1000);
}
catch(Exception e){
timelabel.setText("出错了!!!");
}
}
}
public static void main(String[] args){
PopupTest p=new PopupTest();
Thread thread1=new Thread(p);
thread1.start();
}
} 展开
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.util.*;
import java.text.SimpleDateFormat;
public class PopupTest implements Runnable{
JPanel timepanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel timelabel=new JLabel();
JFrame f=new JFrame("弹出菜单测试");
public PopupTest(){
f.setSize(300,400);
f.setLocation(200,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
f.add(timepanel);
timepanel();
final JPopupMenu popupmenu1=new JPopupMenu();
final JMenu bgmenu=new JMenu("背景");
final JMenuItem redcolorItem=new JMenuItem("红色");
final JMenuItem greencolorItem=new JMenuItem("绿色");
bgmenu.add(redcolorItem);
bgmenu.add(greencolorItem);
popupmenu1.add(bgmenu);
popupmenu1.setVisible(false);
redcolorItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.getContentPane().setBackground(Color.red);
}
});
greencolorItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.getContentPane().setBackground(Color.green);
}
});
f.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
popupmenu1.show(f, e.getX(), e.getY());
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
popupmenu1.show(f, e.getX(), e.getY());
}
}
});
}
public void timepanel(){
timepanel.add(timelabel);
}
public void run(){
while(true){
Date nowDate=new Date();
SimpleDateFormat newSDF=new SimpleDateFormat("hh:mm:ss");
timelabel.setText(newSDF.format(nowDate));
timelabel.setForeground(Color.red);
try {
Thread.sleep(1000);
}
catch(Exception e){
timelabel.setText("出错了!!!");
}
}
}
public static void main(String[] args){
PopupTest p=new PopupTest();
Thread thread1=new Thread(p);
thread1.start();
}
} 展开
展开全部
在f.add(timepanel);后面加上一句timepanel.setOpaque(false);就行了。
因为timepanel在最上层覆盖了f.getContentPane()的Panel.
完整的程序如下:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class PopupTest implements Runnable{
JPanel timepanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel timelabel=new JLabel();
JFrame f=new JFrame("弹出菜单测试");
public PopupTest(){
f.setSize(300,400);
f.setLocation(200,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
f.add(timepanel);
timepanel.setOpaque(false); //这里加了一句。
timepanel();
final JPopupMenu popupmenu1=new JPopupMenu();
final JMenu bgmenu=new JMenu("背景");
final JMenuItem redcolorItem=new JMenuItem("红色");
final JMenuItem greencolorItem=new JMenuItem("绿色");
bgmenu.add(redcolorItem);
bgmenu.add(greencolorItem);
popupmenu1.add(bgmenu);
popupmenu1.setVisible(false);
redcolorItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.getContentPane().setBackground(Color.red);
}
});
greencolorItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.getContentPane().setBackground(Color.green);
}
});
f.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
popupmenu1.show(f, e.getX(), e.getY());
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
popupmenu1.show(f, e.getX(), e.getY());
}
}
});
}
public void timepanel(){
timepanel.add(timelabel);
}
public void run(){
while(true){
Date nowDate=new Date();
SimpleDateFormat newSDF=new SimpleDateFormat("hh:mm:ss");
timelabel.setText(newSDF.format(nowDate));
timelabel.setForeground(Color.red);
try {
Thread.sleep(1000);
}
catch(Exception e){
timelabel.setText("出错了!!!");
}
}
}
public static void main(String[] args){
PopupTest p=new PopupTest();
Thread thread1=new Thread(p);
thread1.start();
}
}
因为timepanel在最上层覆盖了f.getContentPane()的Panel.
完整的程序如下:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class PopupTest implements Runnable{
JPanel timepanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel timelabel=new JLabel();
JFrame f=new JFrame("弹出菜单测试");
public PopupTest(){
f.setSize(300,400);
f.setLocation(200,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
f.add(timepanel);
timepanel.setOpaque(false); //这里加了一句。
timepanel();
final JPopupMenu popupmenu1=new JPopupMenu();
final JMenu bgmenu=new JMenu("背景");
final JMenuItem redcolorItem=new JMenuItem("红色");
final JMenuItem greencolorItem=new JMenuItem("绿色");
bgmenu.add(redcolorItem);
bgmenu.add(greencolorItem);
popupmenu1.add(bgmenu);
popupmenu1.setVisible(false);
redcolorItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.getContentPane().setBackground(Color.red);
}
});
greencolorItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.getContentPane().setBackground(Color.green);
}
});
f.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
popupmenu1.show(f, e.getX(), e.getY());
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
popupmenu1.show(f, e.getX(), e.getY());
}
}
});
}
public void timepanel(){
timepanel.add(timelabel);
}
public void run(){
while(true){
Date nowDate=new Date();
SimpleDateFormat newSDF=new SimpleDateFormat("hh:mm:ss");
timelabel.setText(newSDF.format(nowDate));
timelabel.setForeground(Color.red);
try {
Thread.sleep(1000);
}
catch(Exception e){
timelabel.setText("出错了!!!");
}
}
}
public static void main(String[] args){
PopupTest p=new PopupTest();
Thread thread1=new Thread(p);
thread1.start();
}
}
本回答被提问者采纳
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询