1、先修改Reminder类
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
import javax.swing.*;
public class Reminder{
Timer timer;
JLabel jl;
public Reminder(open op) { ////////////////////将open对象由此传入
jl=op.aLabel; ////////////////////将aLabel赋值给jl
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), new Date(),1000);
}
class RemindTask extends TimerTask {
public void run() {
jl.setText((new Date()).toLocaleString());
}
}
}
将open对象传入该类,也可以只传open.aLabel2、
在open中加入一句:
aLabel=new JLabel();
new Reminder(this); //////由于open有诸多错误,大概改了一下,能运行了import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.util.Date;
import java.io.*;
import javax.swing.text.*;
import javax.swing.event.*;import java.io.FileWriter;
public class open extends JFrame implements ActionListener
{
public JFrame aFrame;
public JLabel aLabel,bLabel;
public JPanel aPane1, aPane2;
public JButton open,save,exit,color,help;
public JTextArea aTextArea;
public JScrollPane aScrollPane;
public JPopupMenu filePopup;
public JMenuItem newMenuItem,openMenuItem,saveMenuItem,exitMenuItem,copyMenuItem,pasteMenuItem,cutMenuItem;
public JComboBox aComboBox;
public Clipboard aClipboard; public open()
{
aFrame =new JFrame("open");
aPane1=new JPanel(); aTextArea=new JTextArea(); aLabel=new JLabel();
new Reminder(this);
bLabel=new JLabel("font");
Date now=new Date();
aLabel.setText(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());
open=new JButton("open");
save=new JButton("save");
exit=new JButton("exit");
color=new JButton("color");
help=new JButton("help");
String[] choices={"1","2"};
aComboBox=new JComboBox(choices);
filePopup= new JPopupMenu();
newMenuItem=new JMenuItem("new");
openMenuItem= new JMenuItem("open");
saveMenuItem= new JMenuItem("save",'s');
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK,false));
copyMenuItem= new JMenuItem("copy",'c');
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK,false));
pasteMenuItem= new JMenuItem("paste",'p');
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK,false));
cutMenuItem= new JMenuItem("cut",'t');
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_MASK,false));
exitMenuItem= new JMenuItem("exit"); aClipboard = getToolkit().getSystemClipboard();
open.addActionListener(this);
save.addActionListener(this);
exit.addActionListener(this);
color.addActionListener(this);
help.addActionListener(this);
aComboBox.addItemListener( new ChoiceListener()); newMenuItem.addActionListener(this);
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
copyMenuItem.addActionListener(this);
pasteMenuItem.addActionListener(this);
cutMenuItem.addActionListener(this); aScrollPane=new JScrollPane(aTextArea);
aFrame.setSize(400,400);
aFrame.getContentPane().add(aPane1,BorderLayout.NORTH);
aFrame.getContentPane().add(aScrollPane);
aTextArea.addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger())
{
filePopup.show(e.getComponent(),e.getX(),e.getY());
}
}
});aPane1.add(aLabel);
aPane1.add(open);
aPane1.add(save);
aPane1.add(color);
aPane1.add(exit);
aPane1.add(help);
aPane1.add(bLabel);
aPane1.add(aComboBox);filePopup.add(newMenuItem);
filePopup.add(openMenuItem);
filePopup.add(saveMenuItem);
filePopup.addSeparator();
filePopup.add(copyMenuItem);
filePopup.add(pasteMenuItem);
filePopup.add(cutMenuItem);
filePopup.addSeparator();
filePopup.add(exitMenuItem); aFrame.setVisible(true);
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aTextArea.requestFocus();
}
public void actionPerformed(ActionEvent e){
}
class ChoiceListener implements ItemListener
{ public void itemStateChanged( ItemEvent e)
{
if(aComboBox.getSelectedItem()=="1")
{
aTextArea.setFont(new Font("ËÎÌå",Font.PLAIN,16));
}
}
} public static void main (String args[]) {
new open();
// System.out.println("Task scheduled.");
}
}
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
import javax.swing.*;
public class Reminder{
Timer timer;
JLabel jl;
public Reminder(open op) { ////////////////////将open对象由此传入
jl=op.aLabel; ////////////////////将aLabel赋值给jl
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), new Date(),1000);
}
class RemindTask extends TimerTask {
public void run() {
jl.setText((new Date()).toLocaleString());
}
}
}
将open对象传入该类,也可以只传open.aLabel2、
在open中加入一句:
aLabel=new JLabel();
new Reminder(this); //////由于open有诸多错误,大概改了一下,能运行了import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.util.Date;
import java.io.*;
import javax.swing.text.*;
import javax.swing.event.*;import java.io.FileWriter;
public class open extends JFrame implements ActionListener
{
public JFrame aFrame;
public JLabel aLabel,bLabel;
public JPanel aPane1, aPane2;
public JButton open,save,exit,color,help;
public JTextArea aTextArea;
public JScrollPane aScrollPane;
public JPopupMenu filePopup;
public JMenuItem newMenuItem,openMenuItem,saveMenuItem,exitMenuItem,copyMenuItem,pasteMenuItem,cutMenuItem;
public JComboBox aComboBox;
public Clipboard aClipboard; public open()
{
aFrame =new JFrame("open");
aPane1=new JPanel(); aTextArea=new JTextArea(); aLabel=new JLabel();
new Reminder(this);
bLabel=new JLabel("font");
Date now=new Date();
aLabel.setText(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());
open=new JButton("open");
save=new JButton("save");
exit=new JButton("exit");
color=new JButton("color");
help=new JButton("help");
String[] choices={"1","2"};
aComboBox=new JComboBox(choices);
filePopup= new JPopupMenu();
newMenuItem=new JMenuItem("new");
openMenuItem= new JMenuItem("open");
saveMenuItem= new JMenuItem("save",'s');
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK,false));
copyMenuItem= new JMenuItem("copy",'c');
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK,false));
pasteMenuItem= new JMenuItem("paste",'p');
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK,false));
cutMenuItem= new JMenuItem("cut",'t');
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_MASK,false));
exitMenuItem= new JMenuItem("exit"); aClipboard = getToolkit().getSystemClipboard();
open.addActionListener(this);
save.addActionListener(this);
exit.addActionListener(this);
color.addActionListener(this);
help.addActionListener(this);
aComboBox.addItemListener( new ChoiceListener()); newMenuItem.addActionListener(this);
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
copyMenuItem.addActionListener(this);
pasteMenuItem.addActionListener(this);
cutMenuItem.addActionListener(this); aScrollPane=new JScrollPane(aTextArea);
aFrame.setSize(400,400);
aFrame.getContentPane().add(aPane1,BorderLayout.NORTH);
aFrame.getContentPane().add(aScrollPane);
aTextArea.addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger())
{
filePopup.show(e.getComponent(),e.getX(),e.getY());
}
}
});aPane1.add(aLabel);
aPane1.add(open);
aPane1.add(save);
aPane1.add(color);
aPane1.add(exit);
aPane1.add(help);
aPane1.add(bLabel);
aPane1.add(aComboBox);filePopup.add(newMenuItem);
filePopup.add(openMenuItem);
filePopup.add(saveMenuItem);
filePopup.addSeparator();
filePopup.add(copyMenuItem);
filePopup.add(pasteMenuItem);
filePopup.add(cutMenuItem);
filePopup.addSeparator();
filePopup.add(exitMenuItem); aFrame.setVisible(true);
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aTextArea.requestFocus();
}
public void actionPerformed(ActionEvent e){
}
class ChoiceListener implements ItemListener
{ public void itemStateChanged( ItemEvent e)
{
if(aComboBox.getSelectedItem()=="1")
{
aTextArea.setFont(new Font("ËÎÌå",Font.PLAIN,16));
}
}
} public static void main (String args[]) {
new open();
// System.out.println("Task scheduled.");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货