如何在此程序中进行异常处理? [英] How to put a exception handling in this program?

查看:84
本文介绍了如何在此程序中进行异常处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class CheckBoxDemonstration extends JFrame implements ItemListener{ //As you can see, we are now implementing another listener called ItemListener. This listener is the appropriate listener for check boxes. ActionListener is mostly for buttons. In Java, there are plenty of listeners that you can use.
	
	JLabel label = new JLabel ("What would you like to drink?");
	JLabel information = new JLabel ("");
	ButtonGroup bGroup = new ButtonGroup(); //This statement creates a ButtonGroup object called bGroup which will group our check boxes so users will not be allowed to select more than one item (check box).
	JCheckBox coffee = new JCheckBox("Coffee", false); //This is a check box called Coffee
	JCheckBox cola = new JCheckBox("Cola", false); //This is a check box called Cola
	JCheckBox milk = new JCheckBox("Milk", false); //This is a check box called Milk
	JCheckBox water = new JCheckBox("Water", false); //This is a check box called Water
	
	public CheckBoxDemonstration(){
		super("Checkbox Demonstration"); //This statement names the frame as Checkbox Demonstration as soon as CheckBoxDemonstration object is created in main method.
		setLayout(new FlowLayout());
		label.setFont(new Font("Arial", Font.ITALIC, 22));
		information.setFont(new Font("Arial", Font.ITALIC, 22));
		coffee.addItemListener(this);
		cola.addItemListener(this);
		milk.addItemListener(this);
		water.addItemListener(this);
		
		//The statements below adds coffe, cola, milk and water checkboxes as one by adding them to object bGroup which is a ButtonGroup object.
		bGroup.add(coffee);
		bGroup.add(cola);
		bGroup.add(milk);
		bGroup.add(water);
		
		//The statements below adds the components to our frame.
		add(label);
		add(coffee);
		add(cola);
		add(milk);
		add(water);
		add(information);
	}
	
	public void itemStateChanged(ItemEvent e){ //public void itemStateChanged() is a method from ItemListener interface that we will have to override. Remember that methods inside an interface MUST be overriden. The parameter ItemEvent e is necessary for this matter. e is just a variable name which means you can use any name other than e. Think of it this way: "e variable represents what has occured by the time ItemEvent happened.
		Object source = e.getItem(); //This statement will represent which of the check boxes made an event. Note that a check box is an object that is why we declared an object from Object class itself and we named it "object" which will hold information about what particular check box made an event.
		
		//The following if statements obviously perform identify jobs.
		//The first if statement will simply compare if the source (an object which holds information about a particular check box that made an event) is indeed equals to coffee check box.
		if (source == coffee){ 
			int select = e.getStateChange(); //This statement declares a variable select which holds an integer. e.getStateChange() simpy determines if a coffee check box is selected or deselectd. getStateChange() is a method that will help you determine the state of the check box if it is selected ot not. getStateChange() was called by object e which is what you have declared in ItemEvent e.
			
			if(select == ItemEvent.SELECTED){ //If the check box is indeed selected, information label will display the texts "Enjoy you coffee". Through the help of setText() method called information label itself, we are able to change the text of information label which is empty by default.
				information.setText("Enoy your coffee!"); //This is the actual statement that allows us to set a text for a label. Just use setText() method.
			}
			else{ //If the check box is not selected, we just set the text of information label to empty, none or null. 
				information.setText("");
			}
		}
		
		if (source == cola){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("Enoy your cola!");
			}
			else{
				information.setText("");
			}
		}
		
		if (source == milk){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("Enoy your milk!");
			}
			else{
				information.setText("");
			}
		}
		
		if (source == water){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("Enoy your water!");
			}
			else{
				information.setText("");
			}
		}
	}
	
	public static void main (String[] args) {
		final int FRAME_WIDTH = 350;
		final int FRAME_HEIGHT = 150;
		CheckBoxDemonstration frame = new CheckBoxDemonstration();
		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		frame.setVisible(true);
	}
	
}





我尝试过: < br $> b $ b



What I have tried:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Shiro_Anime extends JFrame implements ItemListener{ 

	JLabel label = new JLabel ("PICK THE ANIME YOU LIKE");
	JLabel information = new JLabel ("\n");
	ButtonGroup bGroup = new ButtonGroup();
	JCheckBox BlackBullet = new JCheckBox("Black Bullet", false); 
	JCheckBox HighSchoolDxD = new JCheckBox("High School DxD", false); 
	JCheckBox OREIMO = new JCheckBox("OREIMO", false); 
	JCheckBox GuiltyCrown = new JCheckBox("Guilty Crown", false);
	JCheckBox Sakurasou = new JCheckBox("Sakurasou", false);
	JCheckBox AngelBeats = new JCheckBox("Angel Beats", false);
	JCheckBox AccelWorld = new JCheckBox("Accel World", false);
	JCheckBox Nisekoi = new JCheckBox("Nisekoi", false);
	JCheckBox NoGameNoLife = new JCheckBox("No Game No Life", false);

	
	public Shiro_Anime(){
		super("MY FAVORITE ANIME");
		setLayout(new FlowLayout());
		label.setFont(new Font("Arial", Font.ITALIC, 22));
		information.setFont(new Font("Arial", Font.ITALIC, 16));
		BlackBullet.addItemListener(this);
		HighSchoolDxD.addItemListener(this);
		OREIMO.addItemListener(this);
		GuiltyCrown.addItemListener(this);
		Sakurasou.addItemListener(this);
		AngelBeats.addItemListener(this);
		AccelWorld.addItemListener(this);
		Nisekoi.addItemListener(this);
		NoGameNoLife.addItemListener(this);
		
		bGroup.add(BlackBullet);
		bGroup.add(HighSchoolDxD);
		bGroup.add(OREIMO);
		bGroup.add(GuiltyCrown);
		bGroup.add(Sakurasou);
		bGroup.add(AngelBeats);
		bGroup.add(AccelWorld);
		bGroup.add(Nisekoi);
		bGroup.add(NoGameNoLife);
		
		add(label);
		add(BlackBullet);
		add(HighSchoolDxD);
		add(OREIMO);
		add(GuiltyCrown);
		add(Sakurasou);
		add(AngelBeats);
		add(AccelWorld);
		add(Nisekoi);
		add(NoGameNoLife);
		add(information);
	}
	
	public void itemStateChanged(ItemEvent e){ 
			Object source = e.getItem();
		
			if (source == BlackBullet){ 
				
			int select = e.getStateChange(); 
			
			if(select == ItemEvent.SELECTED){ 
				information.setText("You are now watching Black Bullet");
			}
			else{
				information.setText("");
			}
		}
		
		if (source == HighSchoolDxD){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching High School DxD");
			}
			else{
				information.setText("");
			}
		}
		
		if (source == OREIMO){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching OREIMO");
			}
			else{
				information.setText("");
			}
		}
		
		if (source == GuiltyCrown){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching Guilty Crown");
			}
			else{
				information.setText("");
			}
			
		}
		if (source == Sakurasou){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching Sakurasou");
			}
			else{
				information.setText("");
			}
			
		}
		if (source == AngelBeats){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching Angels Beats");
			}
			else{
				information.setText("");
			}
			
		}
		if (source ==AccelWorld){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching Accel World");
			}
			else{
				information.setText("");
			}
			
		}
			if (source ==Nisekoi){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching Nisekoi");
			}
			else{
				information.setText("");
			}
			
		}
		if (source ==NoGameNoLife){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching No Game No Life");
			}
			else{
				information.setText("");
			}
			
		}
	}
	
	public static void main (String[] args) {
		final int FRAME_WIDTH = 350;
		final int FRAME_HEIGHT = 200;
		Shiro_Anime frame = new Shiro_Anime();
		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		frame.setVisible(true);
	}
	
}

推荐答案

我还没有用你的代码来测试以下,但是......



您可以使用try / catch代码处理代码吗?

您可以选择如何在捕获区域中以任何方式处理异常。



此外,与您的查询无关,您是否可以使用switch / case而不是如果条件?这就是我通常做的事情。也许这不合适。
Hi, I haven't used your code to test the following, but...

Could you surround your code with a "try/catch" for the exception handling?
You can choose how to handle the exception any way you want in the catch area.

Also, not related to your query, could you use "switch/case" instead of the "if" conditions? this is what I usually do. Maybe it's not appropriate here.


这篇关于如何在此程序中进行异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆