con.setLayout(new GridLayout(3,2));
this.setLocation(100, 100); this.setSize(400, 400); panel 1 = new Panel l ();
label l = new JLabel(\改变前景颜色\ con.add(label 1 ); con. ;//add(panel1) panel 1 .box 1 .addltemListener(this); panel 1 .box 2.addItemListener(this); panel 1 .box 3.addItemListener(this); this. setVisible (true); this.pack(); } ? }
6.2 列表和组合框
简单应用:声明和创建列表、组合框
1210-16. 列表在界面中表现为列表框,是___JList___类或它的子类的对象。 1010-16.组合框(JComboBox)是______的组合。文本框和列表
1101-6.鼠标单击列表中某个选项事件的相关接口是( ) A.ListActionListener B.ListSelectionListener C.ActionListener D.SelectionListener 1110-24.请写出JComboBox对象上可能发生的两种事件类型的名字。
6.3 菜单
综合应用:设置菜单和菜单项、处理菜单和菜单项事件 6.以下关于菜单的叙述中,正确的是( )
A.菜单分三级定义,最高一级的是菜单条,菜单条中放菜单,菜单中放菜单项 B.菜单分三级定义,最高一级的是菜单,菜单中放菜单条,菜单条中放菜单项 C.菜单分两级定义,最高一级的是菜单,菜单中放菜单项
D.菜单分两级定义,最高一级的是菜单条,菜单条中放菜单项
29.以下是生成菜单项对象,以及菜单项选择事件处理方法的部分代码。要求当菜单项singingItem被选中时,在文本框text中输出“唱歌菜单项被选中!”的字样。 ??
singingItem=new________("唱歌");//生成菜单项对象 ??
public void actionPerformed(ActionEvent e){ if(e.getSource()==singingItem)
text.setText(e.________()+ "菜单项被选中!"); } 生成菜单条是用类JMenuBar的构造方法JMenuBar(),把菜单条放入JFrame窗口中用setJMenuBar()方法;生成菜单用类JMenu的构造方法
JMenu()或JMenu(String s);生成菜单项用类JMenuItem的构造方法
JMenuItem()或JMenuItem(String s)。在菜单事件中获得事件源的方法是getSource(),获得事件源文字信息的方法是getActionCommand()。
1001-29.以下是子窗口中设置一个菜单条类的定义。类的构造方法根据指定的窗口名称和菜单表设置菜单条,菜单和菜单项,当选中某个菜单项时,在文本框中显示相应菜单项被选中的信息。
class MenuWindow extends JFrame implements ActionListener { public static JTextField text;
public MenuWindow(String s,String menuList[][]) { setTitle(s);
Container con=this.getContentPane(); con.setLayout(new BorderLayout());
this.setLocation(100,100); this.setSize(300,100); JMenuBar menubar=new JMenuBar(); for(int i=0; i JMenu menu=new JMenu(menuList[i][0]); for(int j=1;j JMenultem anltem=new JMenultem(menuList[i][j]); anltem.setActionCommand(menuList[i][j]); anltem.___ ActionListener(this)_____;menu.add(anltem); } menubar.__ add(menu)________; } text=new JTextField();setJMenuBar(menubar); con.add(text,BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e){ text.setText(e.getActionCommand()+″菜单项被选中! ″); } } public class Test29 extends Applet { MenuWindow window; String menuList[][]={{″体育″,″跑步″,″打蓝球″,″打乒乓″}, {″娱乐″,″唱歌″,″跳舞″}}; public void init() { window=new MenuWindow(″体育娱乐之窗″,menuList); window.setVisible(true); } } 35.阅读下列程序,请回答以下问题: (1)该程序的菜单条中共有几个菜单项? (2)单击哪个菜单项,可以打开一个对话框? (3)该对话框是强制型还是非强制型? (4)该对话框中显示的是什么内容? Import javax.swing.*;import java.awt.*; import java.awt.event.*; import java.util.*; class MyDialog extends JDialog { MyDialog(JFrame F, String s) { super(F, s, false); Date date = new Date(); String t = date.toString(); JLabel text = new JLabel(t); add(text); setSize(200,100); setVisible(true); } } public class MenuDemo extends JFrame implements ActionListener { JMenuBar menubar; JMenu action; JMenuItem show, set, quit; public MenuDemo() { menubar = new JMenuBar(); setJMenuBar(menubar); action = new JMenu(''操作''); menubar.add(action); show= new JMenuItem(''显示''), action.add(show); set = new JMenuItem(\设置\); action.add(set); action.addSeparator(); quit = new JMenuItem(\退出\); action.add(quit); show.addActionListener(this); set.addActionListener(this); quit.addActionListener(this); setSize(300,300); setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource() == show) {MyDialog diag = new MyDialog(this, \信息\);} else if(e.getSource()== set) { this.setTitle(\菜单演示\);} else if(e.getSource() == quit){ System.exit(0); } } public static void main(String[] args) {new MenuDemo();} } (1)3个菜单项(1分) (2)“显示”菜单项(1分) (3)非强制型对话框(1分) (4)显示当前事件(1分) 6.4 对话框 简单应用:声明和创建对话框 1010-24.在Swing中,对话框有几种?并请写出它们的主要区别。P121 1101-16.对话框的默认布局是____BorderLayOut_____。 1110-16.强制型对话框强制对话过程 ____不能中断___ ,直至对话过程结束,才让程序响应对话框以外的事件。 1210-6. 以下不是..JDialog类构造方法的是 A. JDialog( ) C. JDialog(JFrame f, String s) B. JDialog(boolean b) D. JDialog(JFrame f, String s, boolean b) 16.在构造非强制对话框时,所使用的构造方法是JDialog(f,s, ___false__),其中f为对话框所依赖的窗口,s为对话框的标题。 1001-38.请设计实现如下用于输入学号和姓名的对话框界面,其中空白格是文本框,用于输入相应的内容。 这里给出的是程序的一部分,你要编写的是类InputNoNameDialog的构造方法InputNoNameDialog(JFrame f,String s,JTextField t)。其中参数f是对话框的依赖窗口,s是对话框标题,t是依赖窗口中显示对话框输入内容的文本框。 以下是类InputNoNameDialog的程序框架。 class InputNoNameDialog extends JDialog implements ActionListener{ JLabel title;JTextField textl,text2,mainText;JButton done; InputNoNameDialog(JFrame f ,String s,JTextField t) { super(f,s,true); mainText = t;Container con = getContentPane(); title=new JLabel(s); textl=new JTextField(10); text2=new JTextField(10); con.setLayout(new GridLayout(3,2)); con.setSize(200,100); setModal(false); //请在以下位置续写其余代码 答: done=new JButton(\确定\ done.addActionListener(this); con.add(title);con.add(done); con.add(new JLabel(\学号\ con.add(new JLabel(\姓名\ setVisible(true); } public void actionPerformed(ActionEvent e) { //输入结束按确定按钮后,将对话框中输入的学号和姓名在它依赖窗口的文本框中显示。 mainText.setText(″学号:″+textl.getText()+″ 姓名:″+text2.getText()); setVisible(false);dispose(); } } 6.5 滚动条 简单应用:声明和创建滚动条 1404-1 6.一个水平滚动条对象的初始值是1 50,滑块的宽是8个像素,表示的范围是[0,300]。 创建这样的滚动条对象时,提供的最后2个参数依次是______。0,300 35.阅读以下程序,请写出该程序的功能。 import javax.swing.*; import java.awt.*;import java.awt.event.*; class MyScrollBar extends JscrollBar{ public MyScrollBar(int init, int len, int low, int high){ super(JScrollBar. HORIZONTAL, init,len,low,high); } } class MyWindow extends JFrame implements AdjustmentListener{ private JTextField text; MyWindow(String s){ 滑动滚动条的滑块,super(s); 在文本框显示块位MyScrollBar myBar=new MyScrollBar(10,10,0,255); Container con=this. getContentPane(); 置对应的数值 con. setLayout(new GridLayout(2,1)); this. setSize(200,100); this. setLocation(100,100); myBar.addAdjustmentListener(this); text=new JTextField("",20); con. add(text);con.add(myBar); this.setVisible(true); } public void adjustmentValueChanged(AdjustmentEvent e){ MyScrollBar myBar=(MyScrollBar)e.getAdjustable(); text.setText(""+myBar.getValue()); } } public class Class1{ public static void main(String[]args){ MyWindow myWindow=new MyWindow("text5_4"); } } 1010-35.阅读下列程序,请写出该程序的功能。 import javax.swing.*; import java.awt.*; import java.awt.event.*;