欢迎来到CloudService文涵的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

Java作业三:GUI设计及事件处理

1. (其它)编写java应用程序,实现如下效果功能。

image

import javax.swing.*;
 
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class computers implements ActionListener {
static   JButton clear_Button = new JButton("清空");
static  String s1;
static  String s2;
static  String ss="";
static  int con;
static  int num=0;
	public static void main(String[] args) {
        computers computers=new computers();
		// 创建一个 JFrame 对象并初始化
		JFrame frame = new JFrame("整数加法器");
		    // 清除按钮
		/* 数字键0到9 */
		clear_Button.setSize(frame.getWidth(),frame.getHeight());
		JButton button0 = new JButton("0");
		JButton button1 = new JButton("1");
		JButton button2 = new JButton("2");
		JButton button3 = new JButton("3");
		JButton button4 = new JButton("4");
		JButton button5 = new JButton("5");
		JButton button6 = new JButton("6");
		JButton button7 = new JButton("7");
		JButton button8 = new JButton("8");
		JButton button9 = new JButton("9");
		// 计算命令按钮,加减乘除以及小数点等
		JButton button_dy = new JButton("等于");
		JButton button_jia = new JButton("加");
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(4, 3, 0, 0));
        panel.setSize(500, 800);
        panel.setBackground(Color.gray);    // 设置背景颜色为灰色
	/* 将用于计算的按钮添加到容器内 */
        panel.add(button7);
        panel.add(button8);
        panel.add(button9);
        panel.add(button4);
        panel.add(button5);
        panel.add(button6);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button0);
        panel.add(button_jia);
        panel.add(button_dy);
		panel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
		JPanel panel2 = new JPanel();    // 按照同样的方式设置第二个JPanel
		panel2.add(clear_Button, BorderLayout.CENTER);
		panel2.setLayout(new GridLayout(1, 1, 0, 0));
		frame.setLocation(400, 200);        // 设置主窗口出现在屏幕上的位置
		frame.setResizable(true);        // 设置窗体可以调大小
		frame.getContentPane().setLayout(new BorderLayout());
		// 窗体中可以放置 JPanel,这里我们将面板panel和面板panel2让如窗体
		frame.getContentPane().add(panel2, BorderLayout.NORTH);
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
		//添加事件监听
		button0.addActionListener(computers);
		button1.addActionListener(computers);
		button2.addActionListener(computers);
		button3.addActionListener(computers);
		button4.addActionListener(computers);
		button5.addActionListener(computers);
		button6.addActionListener(computers);
		button7.addActionListener(computers);
		button8.addActionListener(computers);
		button9.addActionListener(computers);
		button_dy.addActionListener(computers);
		button_jia.addActionListener(computers);
		clear_Button.addActionListener(computers);
 
 
	}
 
	@Override
	public void actionPerformed(ActionEvent e) {
		JButton jt=new JButton();
		System.out.println(e.getActionCommand());
		System.out.println(num);
		if (e.getSource().equals(clear_Button)){
		clear_Button.setText("清除");
		ss="";
		s1="";
		s2="";
		num=0;
		}else {
			if (!e.getActionCommand().equals("加")&&!e.getActionCommand().equals("等于")){
 
            if (num==0||num==1){
				s1=e.getActionCommand();
				ss=ss+s1;
				s1=ss;
				clear_Button.setText(ss);
				num=1;
			}else if (num==2||num==3){
				s2=e.getActionCommand();
 
				ss=ss+s2;
				System.out.println(ss);
				s2=ss.substring(ss.indexOf("+")+1);
				clear_Button.setText(ss);
				System.out.println(s1+"   "+s2);
				con= Integer.parseInt(s1)+Integer.parseInt(s2);
				num=3;
			}
			}
 
			else if (e.getActionCommand().equals("等于")&&num==3){
             ss=ss+"="+con;
			 clear_Button.setText(ss);
			 num=0;
			 s1="";
			 s2="";
			 ss="";
			}else  if (e.getActionCommand().equals("加")&&num==1){
				ss=ss+"+";
				clear_Button.setText(ss);
				num=2;
			}
 
		}
 
	}
}

2. (其它)编写java应用程序,实现如下效果功能。

image

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class font implements ActionListener, ChangeListener, ItemListener {
         String fonts="宋体";
		 int fonttype=Font.BOLD;
		 int fontSize=10;
 
    static JTextArea textArea=	new JTextArea("我喜欢C++,\n但更喜欢Java");
	public static void main(String[] args) {
		//实例font对象
        font font =new font();
		Font fnA = new Font(font.fonts, font.fonttype, font.fontSize);
		textArea.setFont(fnA);
		// 创建一个 JFrame 对象并初始化
		JFrame frame = new JFrame("字体");
 
		//  创建面板,这个类似于 HTML 的 div 标签
		frame.setBounds(500,500,400,300);
		JPanel panel = new JPanel();
		JPanel panel1=new JPanel();
 
		panel.setLayout(new GridLayout(5,1));
		panel1.setLayout(new GridLayout(1,1));
        ButtonGroup group=new ButtonGroup();
 
		JRadioButton jRadioButton=new JRadioButton("大字号");
		JRadioButton jRadioButton1=new JRadioButton("中字号");
		JRadioButton jRadioButton2=new JRadioButton("小字号");
		JCheckBox checkbox= new JCheckBox("斜体");
		JCheckBox checkbox1=new JCheckBox("粗体");
		checkbox.setBackground(Color.gray);
		checkbox1.setBackground(Color.gray);
		group.add(jRadioButton);
		group.add(jRadioButton1);
		group.add(jRadioButton2);
		panel.setBackground(Color.gray);
        panel.add(checkbox);
		panel.add(checkbox1);
		panel.add(jRadioButton);
		panel.add(jRadioButton1);
		panel.add(jRadioButton2);
		jRadioButton.setBackground(Color.gray);
		jRadioButton1.setBackground(Color.gray);
		jRadioButton2.setBackground(Color.gray);
 
	    textArea.setBackground(Color.WHITE);
		textArea.setAlignmentX(0);
		textArea.setAlignmentY(0);
 
 
        panel1.add(textArea);
		panel1.setBackground(Color.WHITE);
		frame.setLayout(new GridLayout(1,2));
		frame.add(panel);
		frame.add(panel1);
		//响应式宽高
//		frame.pack();
		frame.setVisible(true);
		//添加事件监听
		jRadioButton.addActionListener(font);
		jRadioButton1.addActionListener(font);
		jRadioButton2.addActionListener(font);
		//
        checkbox.addItemListener(font);
		checkbox1.addItemListener(font);
		checkbox1.setSelected(true);//默认选中
	}
 
	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println(e.getActionCommand());
 
		if (e.getActionCommand().equals("大字号")){
           fontSize=20;
			Font fnA = new Font(fonts,fonttype,fontSize);
			textArea.setFont(fnA);
		}else if (e.getActionCommand().equals("中字号")){
			fontSize=15;
			Font fnA = new Font(fonts,fonttype,fontSize);
			textArea.setFont(fnA);
		}
		else if (e.getActionCommand().equals("小字号")){
			fontSize=10;
			Font fnA = new Font(fonts,fonttype,fontSize);
			textArea.setFont(fnA);
		}
 
	}
 
	@Override
	public void stateChanged(ChangeEvent e) {
 
 
 
 
 
	}
 
	@Override
	public void itemStateChanged(ItemEvent e) {
		JCheckBox checkbox= (JCheckBox) e.getSource();
 
		if (checkbox.getText().equals("粗体")){
			System.out.println(checkbox.getText());
			if (checkbox.isSelected()){
				fonttype=Font.BOLD;
				Font fnA = new Font(fonts,fonttype,fontSize);
				textArea.setFont(fnA);
			}else {
				fonttype=Font.ITALIC;
				Font fnA = new Font(fonts,fonttype,fontSize);
				textArea.setFont(fnA);
			}
 
		}else{
			System.out.println("斜体");
			if (checkbox.isSelected()){
				fonttype=Font.ITALIC;
				Font fnA = new Font(fonts,fonttype,fontSize);
				textArea.setFont(fnA);
			}else {
				fonttype=Font.BOLD;
				Font fnA = new Font(fonts,fonttype,fontSize);
				textArea.setFont(fnA);
			}
		}
	}
}

posted on 2024-04-25 14:20  Cloudservice  阅读(301)  评论(0编辑  收藏  举报