JAVA实验七 图形用户界面的设计与实现

一、实验目的
1.掌握图形界面设计。

2.掌握常用GUI控制组件及其事件处理。


二、实验内容

1.编程包含一个标签和一个按钮,单击按钮时,标签的内容在"你好"和"再见"之间切换。
2.设计一个窗口,窗口中包含有一个文本框,一个标签,两个按钮,当按下“文本一”按纽时,使得“Hello java!”显示在文本框中,按下“文本二”按纽时,使得“你好 Java!” 显示在文本框中(要求两个按纽之间使用网格布局,与其他组件共使用Frame默认布局管理)。

3. 试设计一个窗口,内含一个按钮。开始运行时,按钮显示“Click Me”字样,当按钮按下时,按钮显示为“Click Me Again”字样,再按一次,则按钮显示“Click Me”字样,依此循环。(1,2,3可以选作其一)

4.编程包含一个文本框和一个文本区域,在文本框中按回车键时,把文本框的内容写入文本区域。

5.试设计一个窗口,窗口界面如下图。包含Style菜单、Color菜单和Exit菜单,Style菜单设计字体的样式(包括Plane、Bold、Italic),Color(红、绿、蓝、自定义)菜单设计字体的颜色、Exit菜单(CloseWindow)退出系统。



代码实现:

1.

方法一:
package p;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class T {

	public static void main(String[] args) {
		
		JFrame f=new JFrame();
		FlowLayout flow =new FlowLayout();
		f.setLayout(flow);//设置布局
		
		JButton b=new JButton("exchange");
		final JLabel j=new JLabel("hello");
		b.addActionListener(new ActionListener(){//利用匿名类

			public void actionPerformed(ActionEvent e) {
				if(j.getText().equals("hello"))
					j.setText("bye");
				else j.setText("hello");
			}
			
		});
		
		f.add(b); //先添加哪个,哪个组件就会在前面
		f.add(j);
		
		f.setBounds(20,20,200,200);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

方法二:
package cn.nefu.edu.cn;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyWindow extends JFrame implements ActionListener{
	JButton bt;
	JLabel bq1;
	
	public MyWindow(){
		FlowLayout flow =new FlowLayout();
		this.setLayout(flow);
		bq1=new JLabel("bye");
		bt=new JButton("exchange");
		
		bt.addActionListener(this);//监听本身,自身实现了接口ActionListener
		
		this.add(bt);	
		this.add(bq1);
		validate();
		this.setBounds(20,20,200,200);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {//实现切换
		if(e.getSource()==bt){
			if(bq1.getText().equals("bye"))				
			       bq1.setText("hello");
			else bq1.setText("bye");
		}
	}
}


Test.java
package cn.nefu.edu.cn;

public class Test {

	public static void main(String[] args) {
		new MyWindow();
	}
}



2.

方法一:


package cn.edu.nefu.zhihu;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MyWindow2 extends JFrame implements ActionListener{
	JButton bt1;
	JButton bt2;
	JTextField wb;
	JLabel bq;
	
	public MyWindow2(){
		GridLayout grid=new GridLayout(2,2);
		this.setLayout(grid);
		
		wb=new JTextField();
		bq=new JLabel("name");
		bt1=new JButton("文本一");
		bt2=new JButton("文本二");
		
		this.add(bq);
		this.add(wb);
		this.add(bt1);
		this.add(bt2);
		
		bt1.addActionListener(this);
		bt2.addActionListener(this);
		
		this.setVisible(true);
		this.setBounds(30,30,300,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==bt1){
			wb.setText("Hello java!");
		}
		else wb.setText("你好 Java!");
	}
}

package cn.edu.nefu.zhihu;

public class Test2 {

	public static void main(String[] args) {
		new MyWindow2();
	}
}

方法二:

package cn.edu.nefu.zhihu;

import java.awt.BorderLayout;

public class MyWindow2 extends JFrame implements ActionListener{
	JButton bt1;
	JButton bt2;
	JTextField wb;
	JLabel bq;
	JPanel mb;
	
	public MyWindow2(){
		mb=new JPanel();
		GridLayout grid=new GridLayout(1,2);
		mb.setLayout(grid);
		
		wb=new JTextField();
		bq=new JLabel("name");
		bt1=new JButton("文本一");
		bt2=new JButton("文本二");
		
		this.add(bq,BorderLayout.WEST);
		this.add(wb,BorderLayout.CENTER);
		mb.add(bt1);
		mb.add(bt2);
		this.add(mb,BorderLayout.SOUTH);
		
		bt1.addActionListener(this);
		bt2.addActionListener(this);
		
		this.setVisible(true);
		this.setBounds(30,30,300,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==bt1){
			wb.setText("Hello java!");
		}
		else wb.setText("你好 Java!");
	}
}
package cn.edu.nefu.zhihu;

public class Test2 {

	public static void main(String[] args) {
		//new Jisuanqi("计算器");
		new MyWindow2();
	}
}


3.

package cn.edu.nefu.zhihu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MyWindow3 extends JFrame implements ActionListener{
	JButton an;
	MyWindow3(){
		an=new JButton("Click Me");
		this.add(an);
		an.addActionListener(this);
		
		this.setVisible(true);
		this.setBounds(50,50,200,150);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(an.getText().equals("Click Me")){
			an.setText("Click Me Again");
		}
		else an.setText("Click Me");
	}
}

package cn.edu.nefu.zhihu;

public class Test2 {

	public static void main(String[] args) {
		new MyWindow3();
	}
}


4.
package cn.edu.nefu.zhihu;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MyWindow4 extends JFrame implements ActionListener {
	JTextField t1;
	JTextArea t2;
	
	MyWindow4(){
		t1=new JTextField(20);
		t2=new JTextArea(20,10);
		this.add(t1,BorderLayout.WEST);
		this.add(t2,BorderLayout.CENTER);
		
		t1.addActionListener(this);
		
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(100,100,200,400);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==t1){
			t2.setText(t2.getText()+"\n"+t1.getText());
			t1.setText(" ");
		}
	}

}

package cn.edu.nefu.zhihu;

public class Test2 {

	public static void main(String[] args) {
		new MyWindow4();
	}
}


5.

package cn.edu.nefu.zhihu;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MyWindow5 extends JFrame {
	JMenuBar menubar;
	JMenu menu1,menu2,menu3;
	JMenuItem item1,item2,item3,item4,item5,item6,item7,item8;
	
	public MyWindow5(String ss){
		this.setTitle(ss);
		this.setSize(200,250);
		this.setVisible(true);
		
		menubar=new JMenuBar();
		this.setJMenuBar(menubar);
		
		menu1=new JMenu("Style");
		menu2=new JMenu("Color");
		menu3=new JMenu("Exit");
		menubar.add(menu1);
		menubar.add(menu2);
		menubar.add(menu3);
		
		item1=new JMenuItem("Plane");
		item2=new JMenuItem("Bold");
		item3=new JMenuItem("Italic");
		
		item4=new JMenuItem("Red");
		item5=new JMenuItem("Green");
		item6=new JMenuItem("Blue");
		item7=new JMenuItem("User-defined");
		
		item8=new JMenuItem("Close Window");
		
		menu1.add(item1);
		menu1.add(item2);
		menu1.add(item3);
		menu2.add(item4);
		menu2.add(item5);
		menu2.add(item6);
		menu2.add(item7);
		menu3.add(item8);
		
		validate();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

package cn.edu.nefu.zhihu;

public class Test2 {

	public static void main(String[] args) {
		new MyWindow5("a Window");
	}
}


6.

计算器的简易基本实现,只能实现整数(小数的也可以,但是界面不太友好)的加减乘除四则运算,且经过每一次计算之后都要进行清零,否则会计算有误。

代码实现如下:

package cn.edu.nefu.zhihu;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Jisuanqi extends JFrame{
	JTextField wbk;
	JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
	JButton b10,b11,b12,b13,b14;
	JButton b15,b16;
	JPanel mb;
	double acc=0;
	String yunSuanFu=null;
	
	public Jisuanqi(String ss){
		super(ss);
		wbk=new JTextField("0");
		b0=new JButton("0");
		b1=new JButton("1");
		b2=new JButton("2");
		b3=new JButton("3");
		b4=new JButton("4");
		b5=new JButton("5");
		b6=new JButton("6");
		b7=new JButton("7");
		b8=new JButton("8");
		b9=new JButton("9");
		b10=new JButton(".");
		
		b11=new JButton("+");
		b12=new JButton("-");
		b13=new JButton("*");
		b14=new JButton("/");
		
		b15=new JButton("=");
		b16=new JButton("C");
		
		mb=new JPanel();
		GridLayout grid=new GridLayout(4,4,4,12);
		mb.setLayout(grid);
		this.add(wbk,BorderLayout.NORTH);
		wbk.setHorizontalAlignment(JTextField.RIGHT);
		this.add(mb,BorderLayout.CENTER);
		mb.add(b1);
		mb.add(b2);
		mb.add(b3);
		mb.add(b11);
		
		mb.add(b4);
		mb.add(b5);
		mb.add(b6);
		mb.add(b12);
		
		mb.add(b7);
		mb.add(b8);
		mb.add(b9);
		mb.add(b13);
		
		mb.add(b0);
		mb.add(b10);
		mb.add(b15);
		mb.add(b14);
		
		this.add(b16,BorderLayout.SOUTH);
		
		A a=new A();
		b0.addActionListener(a);
		b1.addActionListener(a);
		b2.addActionListener(a);
		b3.addActionListener(a);
		b4.addActionListener(a);
		b5.addActionListener(a);
		b6.addActionListener(a);
		b7.addActionListener(a);
		b8.addActionListener(a);
		b9.addActionListener(a);
		b10.addActionListener(a);
		
		B b=new B();
		b11.addActionListener(b);
		b12.addActionListener(b);
		b13.addActionListener(b);
		b14.addActionListener(b);
		
		b15.addActionListener(new C());
		b16.addActionListener(new D());
		
		b16.setFont(new Font("宋体",Font.BOLD,28));
		
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(300,300,250,325);
	}
	class A implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String s=e.getActionCommand();//s为获取该事件的字符串
			String ss=wbk.getText();//ss为文本框中的内容
			if(ss.equals("0")||ss.equals("+")||ss.equals("-")||ss.equals("*")||ss.equals("/")){
				wbk.setText(s);
			}
			else{
				wbk.setText(ss+s);
			}
		}
		
	}
	
	class B implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			acc=Double.parseDouble(wbk.getText());
			yunSuanFu=e.getActionCommand();
			wbk.setText(e.getActionCommand());
		}
		
	}
	
	class C implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String ss=wbk.getText();
			double sum1=Double.parseDouble(ss);
			if(yunSuanFu.equals("+"))
				wbk.setText(Double.toString(acc+sum1));
			if(yunSuanFu.equals("-"))
				wbk.setText(Double.toString(acc-sum1));
			if(yunSuanFu.equals("*"))
				wbk.setText(Double.toString(acc*sum1));
			if(yunSuanFu.equals("/"))
				wbk.setText(Double.toString(acc/sum1));
		}
	}
	
	class D implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			wbk.setText("0");
			acc=0;
		}
		
	}
}



package cn.edu.nefu.zhihu;

public class Test2 {

	public static void main(String[] args) {
		new Jisuanqi("计算器");
	}
}


你可能感兴趣的:(java,图形用户界面设计)