Java程序设计机试题库 联系客服

发布时间 : 星期六 文章Java程序设计机试题库更新完毕开始阅读325bc970964bcf84b9d57b7b

import java.awt.*;

import java.awt.event.*; import javax.swing.*; //步骤2:声明类 public class Eg7_15{

public static void main(String args[]){ //步骤3:写主方法

JFrame f = new JFrame(\按钮实例\创建窗口框架实例f 。

Container contentPane = f.getContentPane(); //用f创建内容面板实例

contentPane.setLayout(new GridLayout(3,1)); //设置内容面板的布局管理器 //Icon icon1=new ImageIcon(\ Icon icon2=new ImageIcon(\ Icon icon3=new ImageIcon(\

JToggleButton b1 = new JToggleButton(\我是按钮1\ //创建文字按钮 JToggleButton b2 = new JToggleButton(icon2); //创建图像按钮

JToggleButton b3 = new JToggleButton(\我是按钮3\创建文字和图像按钮

contentPane.add(b1); //将文字按钮放到框架的内容面板上 contentPane.add(b2); //将图像按钮放到框架的内容面板上 contentPane.add(b3); //将文字图像按钮放到框架的内容面板上 f.pack(); f.show();

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) { System.exit(0); } }); } }

16. 设计一GUI程序,在窗口中制作一个按钮,当将鼠标的光标移到按钮上时显示图一,按下按钮时显示图二,离开按钮时显示图三。

import java.awt.*;

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

13

public class Eg7_16{

public static void main(String args[]){

JFrame f = new JFrame(\ Container contentPane = f.getContentPane();

Icon rollover = new ImageIcon(\创建icon对像实例rollover Icon general = new ImageIcon(\创建icon对像实例 general Icon press = new ImageIcon(\创建icon对像实例 press Icon disable = new ImageIcon(\创建icon对像实例 disable

JButton b = new JButton(); //创建按钮对像实例 b b.setRolloverEnabled(true); //设按钮b的标示具有翻滚的功能 b.setIcon(general); //将icon实例general加载到按钮上,按钮上就显示该图形

b.setRolloverIcon(rollover); //按钮翻滚后显示的图形是rollover b.setPressedIcon(press); //鼠标按下时显示press图形 contentPane.add(b); //将按钮加到内容面板上 f.pack(); //显示窗口

f.show(); //显示窗口中的组件

f.addWindowListener(new WindowAdapter() {//处理窗口关闭 public void windowClosing(WindowEvent e) { System.exit(0); } }); } } 17. 设计一程序,要求在图形用户界面中输入产品的说明书,输入内容有产品名称、产品规格、产品编号、产品说明。

import java.awt.*;

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

14

public class Eg7_17{

public static void main(String args[]){

JFrame f = new JFrame(\例题\ //创建JFrame窗口容器类对象f。 Container contentPane = f.getContentPane();//创建内容面板对象实例

contentPane.setLayout(new BorderLayout()); //设置内容面板采用边界布局管理器

JPanel p1 = new JPanel(); //创建面板容器实例。

p1.setLayout(new GridBagLayout()); //设置面板采用网袋布局管理器 GridBagConstraints gbc = new GridBagConstraints();//创建网袋格布局对象gbc。 gbc.anchor = GridBagConstraints.WEST; //确定网袋格布局管理器对象gbc的位置

gbc.insets = new Insets(2,2,2,2); //gbc与边界的距离(上,左,下,右) p1.setBorder(BorderFactory.createTitledBorder(\建构一般的JTextArea\

//设置边界框和说明文字 JLabel l1 = new JLabel(\产品名称:\ //创建4个标签实例 JLabel l2 = new JLabel(\产号编号:\

JLabel l3 = new JLabel(\产品规格:\ JLabel l4 = new JLabel(\产品说明:\

JTextArea t1 = new JTextArea(2,8); //创建4个文本域实例

JTextArea t2 = new JTextArea(10,2);//书有错,请改正 JTextArea t3 = new JTextArea(\系列\

JTextArea t4 = new JTextArea(\采用因特尔迅驰移动技术,因特尔@奔腾M处理器1.5GHz\ //以最合适的长度存放字符串

t1.setText(\宏基笔记本电脑\ //向文本域t1写入文字数据,清除以前的内容

t2.append(\在文本域t2中已有文字后写入该字符串,

t4.setLineWrap(true); /*激活自动换行功能,当文字输入长度大于文本域提供的宽

度时,会自动换行。*/

gbc.gridy=1;gbc.gridx=0;p1.add(l1,gbc);/*29-36行将4个标签和4个文本域按指定

的位置加到面板容器中。*/ gbc.gridx=1;p1.add(t1,gbc);

gbc.gridy=2;gbc.gridx=0;p1.add(l2,gbc); gbc.gridx=1;p1.add(t2,gbc);

gbc.gridy=3;gbc.gridx=0;p1.add(l3,gbc); gbc.gridx=1;p1.add(t3,gbc);

gbc.gridy=4;gbc.gridx=0;p1.add(l4,gbc); gbc.gridx=1;p1.add(t4,gbc);

contentPane.add(p1); //把面板容器加到内容面板里。 f.pack(); //使窗口可见

f.show(); //使窗口中的组件可见

15

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) { System.exit(0); } }); } }

18. 设计一程序,在窗口中显示一个名为JEditorPane.html文件.

import java.awt.*;

import java.awt.event.*; import javax.swing.*; import java.io.*;//书错

//开始编写文本显示类,给类起一个好名字,别忘了在类名前加上public class public class Eg7_18 {

public static void main(String args[]) { //在类体中写主方法main()

JEditorPane editPane = null; //定义一个JEditorPane 的对象editPane,并初始化为null。

//

try{

File file = new File (\先创建一文件类的实例。 String str = file.getAbsolutePath();/*通过文件类对象调用方法getAbsolutePath()获的

文件所在的路径,并将该路径支付给字符串变量str。*/ str = \给一字符串变量赋值。----书错

editPane = new JEditorPane();//创建JEditorPane类的实例。 editPane.setPage(str);

} //由该实例对象负责将字符串显示在窗口面板上。

catch(IOException ioe) { /*捕捉文件打不开或不存在等异常事件,然后对异

常事件进行处理,关闭窗口。*/

ioe.printStackTrace(System.err);//打印发生异常的信息 System.exit(0);

16