Java基础编程练习题(50题及答案)2017 下载本文

内容发布更新时间 : 2024/5/4 18:42:31星期一 下面是文章的全部内容请认真阅读。

}

public MyException(String msg) { super(msg); }

public MyException(String msg, int x) { super(msg); i = x; }

public int val() { return i; }

private int i; }

public class DemoException { /**

*方法说明:使用MyException类中默认的构造器 */

public static void a() throws MyException {

System.out.println(\ throw new MyException(); }

/**

*方法说明:使用MyException类中带信息的构造器 */

public static void b() throws MyException {

System.out.println(\ throw new MyException(\ }

/**

*方法说明:使用了MyException中有编码的构造器 */

public static void c() throws MyException {

System.out.println(\ throw new MyException(\ }

public static void main(String[] args) {

精选

try { a();

} catch (MyException e) { e.getMessage(); } try { b();

} catch (MyException e) { e.toString(); } try { c();

} catch (MyException e) { e.printStackTrace();

System.out.println(\ } }

} // end :)

package test10;

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

* Title: 创建自己的窗体 * Description:

* Filename:mainFrame.java */

public class mainFrame extends JFrame {

private static final long serialVersionUID = 1L;

/**

*方法说明:构造器,通过传递参数来完成窗体的绘制。 *输入参数:String sTitle 窗体标题 *输入参数:int iWidth 窗体的宽度

*输入参数:int iHeight 窗体的高度 返回类型: */

public mainFrame(String sTitle, int iWidth, int iHeight) {

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();// 获取屏幕尺寸 ImageIcon ii = new ImageIcon(\ setTitle(sTitle);// 设置窗体标题

setIconImage(ii.getImage());// 设置窗体的图标

精选

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);// 设置但关闭窗体时退出程序

setSize(iWidth, iHeight);// 设置窗体大小 int w = getSize().width;// 获取窗体宽度 int h = getSize().height;// 获取窗体高度

System.out.println(\窗体宽:\窗体高:\ int x = (dim.width - w) / 2; int y = (dim.height - h) / 2;

setLocation(x, y);// 将窗体移到屏幕中间 setVisible(true);// 显示窗体 }

public static void main(String[] args) {

JFrame.setDefaultLookAndFeelDecorated(true);// 使用最新的SWING外观 new mainFrame(\ } }

精选