问答题

本程序的功能是将图像“exam_36.gif”显示在窗口的正中间,并且设置窗口的颜色为白色,如图所示。


import java.awt.*;
import javax.swing.*;
public class exam_36
public static void main(String[] args)
{
ImageFrame frame=new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ImageFrame extends JFrame
{
public ImageFrame()
{
setTitle("exam_36");
setSize(WIDTH, HEIGHT);
ImagePanel panel=new ImagePanel();
Container
contentPane=getContentPane();
contentPane.add(panel);
}
public static final int WIDTH=300;
public static final int HEIGHT=200;
}
class ImagePanel extends JPanel
{
public ImagePanel()
{
setBackground(Color.white);
image=Toolkit.getDefaultToolkit().
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image, i);
try
{
tracker.waitForID(1);
}
catch (InterruptedException exception)
{}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int imageWidth=image.getWidth(this);
int imageHeight=image.getHeight(this);
int FrameWidth=getWidth();
int FrameHeight=getHeight();
int x=(FrameWidth - imageWidth ) / 2;
int y=(FrameHeight - imageHeight ) / 2;
______(image, x, y, null);
}
private Image image;
}

【参考答案】

第1处:getImage("exam_36.gif")
第2处:g.drawImage
热门 试题

问答题
本程序的功能是监听列表的选择,并将选中的内容显示在文本域中。窗口中有一个文本域和一个支持多项选择的列表,当选中列表中的一项或多项时,文本域中就会分行显示所选项的索引值和值,如图所示。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class exam_37{public static void main (String[] args){ListFrame frame=new ListFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();}}class ListFrame extends JFrame{public ListFrame(){setTitle( exam_37 );setSize(WIDTH, HEIGHT);Container contentPane=getContentPane();JPanel textPanel=new JPanel();myTextArea=new JTextArea(checkedLabel, 5, 20);JScrollPane textScrollPane=new JScrollPane(myTextArea);textPanel.add(textScroilPane);contentPane.add(textPanel);listPanel=new JPanel();String[] courses={ 数学 , 英语 , 物理 , 化学 , 生物 , 地理 };courseList=new JList(courses);courseList.setVisibleRowCount(4);courseList.addListSelectionListener(new courseListener());JScrollPane listScrollPane=new JScrollPane(courseList);listPanel.add(listScrollPane);contentPane.add(listPanel, BorderLayout.SOUTH);private class courseListener implements ListSelectionListenerpublic void valueChanged(ListSelectionEvent event){Object[] selectedCourses=courseList.getSelectedValues();int[] selectedIndexCourses=courseList.getSelectedIndices();StringBuffer tempSeletedText=new StringBuffer(checkedLabel);for (int i=0; ______; i++){String strl=new String(selectedIndexCourses[i] + , );String str2=(String) selectedCourses[i];tempSeletedText.append(str1);tempSeletedText.append(str2);tempSeletedText.append( n );}myTextArea.setText(______);}}public static final int WIDTH=300;public static final int HEIGHT=250;public static final String checkedLabel= 请选择你喜欢的课程: n ;private JTextArea myTextArea;private JList courseList;private JPanel listPanel;}