java中鼠标监听器的使用
这个是我的代码想实现的功能是在窗口中显示当前时间并且增加几个按钮以改变时间字体的颜色请问如何修改调试了好久可能有很多无用代码新手请见谅感激不尽importjava.uti...
这个是我的代码
想实现的功能是在窗口中显示当前时间并且增加几个按钮以改变时间字体的颜色
请问如何修改 调试了好久
可能有很多无用代码 新手请见谅
感激不尽
import java.util.Date;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyTime extends JFrame implements ActionListener {
JFrame frame=new JFrame();
JLabel label=null;
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JButton btn1=new JButton(" 红 色 ");
JButton btn2=new JButton(" 绿 色 ");
JButton btn3=new JButton(" 蓝 色 ");
public MyTime(){
panel1.setBackground(Color.white);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
panel1.setBackground(Color.WHITE);
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
add(panel1,BorderLayout.CENTER);
add(panel2,BorderLayout.SOUTH);//创建一个监听器对象
btn1.addActionListener(this); //为按钮注册监听器
btn2.addActionListener(this);
btn3.addActionListener(this);
frame=new JFrame();
label=new JLabel();
label.setIcon(new ImageIcon("QQ拼音截图未命名.jpg"));
label.setFont(new Font("幼圆",Font.LAYOUT_NO_LIMIT_CONTEXT,76));
label.setForeground(Color.BLACK);
add(label);
setTitle("个性数字时钟");
setResizable(false);//锁定窗口大小
setSize(320,160);
setLocation(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
MyTime mt=new MyTime();
new TimeThread(mt).start();
}
}
class TimeThread extends Thread{
private MyTime mt;
public TimeThread(MyTime mt){
this.mt=mt;
}
public void run(){
while(true){
String fullTime=new Date().toString();
String time=fullTime.substring(11, 19);
mt.label.setText(time);
mt.label.repaint();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class AL implements ActionListener{ // 内部类
public void actionPerformed(ActionEvent e){
if((JButton)e.getSource()==btn1)
mt.label.setForeground(Color.RED);
else if((JButton)e.getSource()==btn2)
mt.label.setForeground(Color.GREEN);
else if((JButton)e.getSource()==btn3)
mt.label.setForeground(Color.BLUE);
}
}
} 展开
想实现的功能是在窗口中显示当前时间并且增加几个按钮以改变时间字体的颜色
请问如何修改 调试了好久
可能有很多无用代码 新手请见谅
感激不尽
import java.util.Date;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyTime extends JFrame implements ActionListener {
JFrame frame=new JFrame();
JLabel label=null;
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JButton btn1=new JButton(" 红 色 ");
JButton btn2=new JButton(" 绿 色 ");
JButton btn3=new JButton(" 蓝 色 ");
public MyTime(){
panel1.setBackground(Color.white);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
panel1.setBackground(Color.WHITE);
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
add(panel1,BorderLayout.CENTER);
add(panel2,BorderLayout.SOUTH);//创建一个监听器对象
btn1.addActionListener(this); //为按钮注册监听器
btn2.addActionListener(this);
btn3.addActionListener(this);
frame=new JFrame();
label=new JLabel();
label.setIcon(new ImageIcon("QQ拼音截图未命名.jpg"));
label.setFont(new Font("幼圆",Font.LAYOUT_NO_LIMIT_CONTEXT,76));
label.setForeground(Color.BLACK);
add(label);
setTitle("个性数字时钟");
setResizable(false);//锁定窗口大小
setSize(320,160);
setLocation(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
MyTime mt=new MyTime();
new TimeThread(mt).start();
}
}
class TimeThread extends Thread{
private MyTime mt;
public TimeThread(MyTime mt){
this.mt=mt;
}
public void run(){
while(true){
String fullTime=new Date().toString();
String time=fullTime.substring(11, 19);
mt.label.setText(time);
mt.label.repaint();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class AL implements ActionListener{ // 内部类
public void actionPerformed(ActionEvent e){
if((JButton)e.getSource()==btn1)
mt.label.setForeground(Color.RED);
else if((JButton)e.getSource()==btn2)
mt.label.setForeground(Color.GREEN);
else if((JButton)e.getSource()==btn3)
mt.label.setForeground(Color.BLUE);
}
}
} 展开
展开全部
import java.util.Date;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyTime extends JFrame {//删除了集成的接口
JFrame frame = new JFrame();
JLabel label = null;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JButton btn1 = new JButton(" 红 色 ");
JButton btn2 = new JButton(" 绿 色 ");
JButton btn3 = new JButton(" 蓝 色 ");
public MyTime() {
panel1.setBackground(Color.white);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
panel1.setBackground(Color.WHITE);
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);// 创建一个监听器对象
btn1.addActionListener(new TimeThread(this).new AL()); // 为按钮注册监听器(使用了TimeThread类的AL内部类)
btn2.addActionListener(new TimeThread(this).new AL());
btn3.addActionListener(new TimeThread(this).new AL());
frame = new JFrame();
label = new JLabel();
label.setIcon(new ImageIcon("QQ拼音截图未命名.jpg"));
label.setFont(new Font("幼圆", Font.LAYOUT_NO_LIMIT_CONTEXT, 76));
label.setForeground(Color.BLACK);
add(label);
setTitle("个性数字时钟");
setResizable(false);// 锁定窗口大小
setSize(320, 160);
setLocation(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
MyTime mt = new MyTime();
new TimeThread(mt).start();
}
}
class TimeThread extends Thread {
private MyTime mt;
public TimeThread(MyTime mt) {
this.mt = mt;
}
public void run() {
while (true) {
String fullTime = new Date().toString();
String time = fullTime.substring(11, 19);
mt.label.setText(time);
mt.label.repaint();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class AL implements ActionListener { // 内部类
public void actionPerformed(ActionEvent e) {
if ((JButton) e.getSource() == mt.btn1)//btn1属于MyTime类
mt.label.setForeground(Color.RED);
else if ((JButton) e.getSource() == mt.btn2)
mt.label.setForeground(Color.GREEN);
else if ((JButton) e.getSource() == mt.btn3)
mt.label.setForeground(Color.BLUE);
}
}
}
//修改的地方已经注释
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyTime extends JFrame {//删除了集成的接口
JFrame frame = new JFrame();
JLabel label = null;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JButton btn1 = new JButton(" 红 色 ");
JButton btn2 = new JButton(" 绿 色 ");
JButton btn3 = new JButton(" 蓝 色 ");
public MyTime() {
panel1.setBackground(Color.white);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
panel1.setBackground(Color.WHITE);
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);// 创建一个监听器对象
btn1.addActionListener(new TimeThread(this).new AL()); // 为按钮注册监听器(使用了TimeThread类的AL内部类)
btn2.addActionListener(new TimeThread(this).new AL());
btn3.addActionListener(new TimeThread(this).new AL());
frame = new JFrame();
label = new JLabel();
label.setIcon(new ImageIcon("QQ拼音截图未命名.jpg"));
label.setFont(new Font("幼圆", Font.LAYOUT_NO_LIMIT_CONTEXT, 76));
label.setForeground(Color.BLACK);
add(label);
setTitle("个性数字时钟");
setResizable(false);// 锁定窗口大小
setSize(320, 160);
setLocation(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
MyTime mt = new MyTime();
new TimeThread(mt).start();
}
}
class TimeThread extends Thread {
private MyTime mt;
public TimeThread(MyTime mt) {
this.mt = mt;
}
public void run() {
while (true) {
String fullTime = new Date().toString();
String time = fullTime.substring(11, 19);
mt.label.setText(time);
mt.label.repaint();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class AL implements ActionListener { // 内部类
public void actionPerformed(ActionEvent e) {
if ((JButton) e.getSource() == mt.btn1)//btn1属于MyTime类
mt.label.setForeground(Color.RED);
else if ((JButton) e.getSource() == mt.btn2)
mt.label.setForeground(Color.GREEN);
else if ((JButton) e.getSource() == mt.btn3)
mt.label.setForeground(Color.BLUE);
}
}
}
//修改的地方已经注释
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询