200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > java上机实验学习笔记————实验七 图形用户界面设计

java上机实验学习笔记————实验七 图形用户界面设计

时间:2022-04-04 03:04:30

相关推荐

java上机实验学习笔记————实验七 图形用户界面设计

java上机实验学习笔记————实验七 图形用户界面设计

题一:计算器程序雏形

该窗口模拟Windows的计算器功能,添加一个文本行和4个按钮,单击【1】、【2】、【+】按钮时,将按钮的标签添加到文本行中;单击【C】按钮时,清空文本行中的内容;单击窗口的关闭按钮,将关闭该窗口。

MyCalculator.java:

import javax.swing.JFrame;import javax.swing.JButton;import javax.swing.JTextField;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class MyCalculator extends JFrame {private JTextField xianshi;private JButton button_1,button_2,button_add,button_c;public MyCalculator() {this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setTitle("Calculator");getContentPane().setLayout(null);//设置文本框this.xianshi = new JTextField();this.xianshi.setBounds(10, 10, 416, 30);this.add(xianshi);//设置按钮“1”this.button_1 = new JButton("1");this.button_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {xianshi.setText(xianshi.getText()+"1");}});//匿名内部类可以帮助提高编程速度this.button_1.setBounds(10, 50, 45, 45);this.add(button_1);//设置按钮“2”this.button_2 = new JButton("2");this.button_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {xianshi.setText(xianshi.getText()+"2");}});this.button_2.setBounds(65, 50, 45, 45);this.add(button_2);//设置按钮“+”this.button_add = new JButton("+");this.button_add.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {xianshi.setText(xianshi.getText()+"+");}});this.button_add.setBounds(120, 50, 45, 45);this.add(button_add);//设置按钮“清空”this.button_c = new JButton("c");this.button_c.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {xianshi.setText("");}});this.button_c.setBounds(175, 50, 45, 45);this.add(button_c);}}

Test.java:

import java.awt.Dimension;public class Test {public static void main(String[] args) {MyCalculator onee= new MyCalculator();onee.setSize(new Dimension(500,350));onee.setVisible(true);}}

题二:

通过继承JFrame来设计窗口,如下图所示,要求:窗体名称为“MyFrame”,并且有2个按钮,一个显示“show”,另一个显示“close”。当用户点击“show”按钮时,弹出右边对话框,当用户点击“close”按钮时,窗体关闭,系统退出。

MyFrame.java:

import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.FlowLayout;public class MyFrame extends JFrame {private JButton button_show,button_close;public MyFrame() {this.setTitle("MyFrame");this.setLayout(new FlowLayout());this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.button_show = new JButton("show");this.add(button_show);this.button_show.addActionListener(new MyActionListener());this.button_close = new JButton("close");this.add(button_close);this.button_close.addActionListener(new MyActionListener());}class MyActionListener implements ActionListener{public void actionPerformed(ActionEvent event) {if(event.getSource()==button_show)JOptionPane.showMessageDialog(null, "这是一个例子!");else if(event.getSource()==button_close)System.exit(EXIT_ON_CLOSE);}}public static void main(String[] args) {MyFrame twee=new MyFrame();twee.setSize(new Dimension(300,150));twee.setVisible(true);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。