我真的是服了,都要考试了,还在那布置不属于考试范围的作业。真的吐血了,又不是只复习这一门课,搞得好像就学Java了一样,真的是想死了
想不通
实验目的
掌握Java Swing组件的使用方法,理解委托事件处理模式,掌握多种布局
方式,掌握窗口菜单和快捷菜单设计方式,熟悉在组件上绘图的方法,设计出
具有图形用户界面的、能够响应事件的Java应用程序。
实验内容
(1) 设计一界面,可输入某一年份,按“判断”按钮,可判断该年份是否是闰年,
并在界面上输出判断结果。
public class ex_14_ { public static void main(String args[]) { WindowActionEvent win = new WindowActionEvent(); PoliceListen police = new PoliceListen(); win.setMyCommandListener(police); win.setTitle("判断是否为闰年"); win.setBounds(100,100,360,360); } }
import java.awt.event.*; import javax.swing.*; public interface MyCommandListener extends ActionListener{ public void setJTextField(JTextField text); public void setJTextArea(JTextArea area); }
import java.awt.event.*; import javax.swing.*; public class PoliceListen implements MyCommandListener { JTextField textInput; JTextArea textShow; public void setJTextField(JTextField text) { textInput = text; } public void setJTextArea(JTextArea area) { textShow = area; } public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); int t = Integer.parseInt(str); if(((t%4==0)&&(t%100!=0))||(t%400==0)) { textShow.append(str+":是闰年。"); }else { textShow.append(str+":不是闰年。"); } } }
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class WindowActionEvent extends JFrame{ JTextField InputText; JTextArea textShow; JButton button; MyCommandListener listener; public WindowActionEvent() { init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init() { setLayout(new FlowLayout()); InputText = new JTextField(10); button = new JButton("判断"); textShow = new JTextArea(9,30); add(InputText); add(button); add(new JScrollPane(textShow)); } void setMyCommandListener(MyCommandListener listener) { this.listener = listener; listener.setJTextField(InputText); listener.setJTextArea(textShow); InputText.addActionListener(listener); button.addActionListener(listener); } }
真的对于老师很服气