这些填空题,你会吗?来个学JAVA的 帮忙解答一下。我是学的c#

ay_changyafei 2011-10-18 05:44:26
3:
1.单击鼠标右键,实现弹出式多级菜单,通过“color”选择颜色;
2.在文本框中根据选择的颜色设置输出字符的颜色;
3.单击鼠标右键,选择“exit”可以退出程序。

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

//*********Found**********
public class Java_3 extends ______________________ implements ActionListener
{
//*********Found**********
private ____________________ pop;
private JMenu subPop;
private JMenuItem color;
private JMenuItem exit;
private JMenuItem red;
private JMenuItem blue;
private JTextArea textArea;
private JFrame frame;

public void initGUI()
{
pop=new JPopupMenu();

subPop=new JMenu("color");
//*********Found**********
red=new JMenuItem("______________________");
red.addActionListener(this);
blue=new JMenuItem("blue");
blue.addActionListener(this);
subPop.add(red);
subPop.add(blue);

exit=new JMenuItem("exit");
exit.addActionListener(this);

pop.add(subPop);
pop.add(exit);

frame=new JFrame("popup frame");
textArea=new JTextArea("",10,10);

textArea.addMouseListener(this);
//*********Found**********
frame.getContentPane().add(____________________);
frame.setSize(300,300);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==red)
{
//*********Found**********
textArea.setForeground(Color._____________________);
textArea.setText("red menu is selected");
}
else if(event.getSource()==blue)
{
textArea.setForeground(Color.blue);
textArea.setText("blue menu is selected");
}
else if(event.getSource()==exit)
{
frame.setVisible(false);
System.exit(0);
}
}

public void mousePressed(MouseEvent e)
{
if(e.getModifiers()==e.BUTTON3_MASK)
{
pop.show(e.getComponent(),e.getX(),e.getY());
}
}

public static void main(String args[])
{
Java_3 example=new Java_3();
example.initGUI();
}
}

6:在JFrame窗口中,显示出红色的2008北京奥运主题口号“同一个世界,同一个梦想”,同时在窗口的上方,有三个按钮可以改变窗口的背景色。该程序的运行结果如下:


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

public class Java_3
{
public static void main(String[] args)
{
FontFrame frame = new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

//**********Found**********
class FontFrame extends ________________
{
public FontFrame()
{
setTitle("北京 2008");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
FontPanel panel = new FontPanel();
Container contentPane = getContentPane();
//**********Found**********
contentPane.add(_____________________);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 250;
}

class FontPanel extends JPanel
{
public FontPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Green");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction greenAction = new ColorAction(Color.GREEN);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(greenAction);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
String message = "同一个世界,同一个梦想!";
Font f = new Font("隶书", Font.BOLD, 27);
g2.setFont(f);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
double x = (getWidth() - bounds.getWidth()) / 2;
double y = (getHeight() - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.setPaint(Color.RED);
g2.drawString (message, (int)x, (int)(baseY));
}
//**********Found**********
private class ColorAction ____________________ ActionListener
{
public ColorAction(Color c)
{
BackgroundColor = c;
}
//**********Found**********
public void _____________________ (ActionEvent event)
{
setBackground(BackgroundColor);
}
private Color BackgroundColor;
}
}

9:在图形用户界面中,在JFrame窗口的左上方,有一个“红军不怕远征难”的字符串,用户可以通过窗口中的File菜单下的Open子菜单来选择014.JPG图片或306.JPG图片并显示。在窗口下方有一个选择框可对“红军不怕远征难”的字符串进行字体选择。



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

public class Java_3
{
public static void main(String[] args)
{
JFrame frame = new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class ImageViewerFrame extends JFrame
{
public ImageViewerFrame()
{
setTitle("ImageViewer");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
label = new JLabel();
Container contentPane = getContentPane();
contentPane.add(label,BorderLayout.CENTER);
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem openItem = new JMenuItem("Open");
//*********Found**********
_______________.add(openItem);
//*********Found**********
openItem.___________________________(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
int r = chooser.showOpenDialog(null);
if(r == JFileChooser.APPROVE_OPTION)
{
//*********Found**********
String name = __________________.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
});
labelT = new JLabel("红军不怕远征难");
labelT.setFont(new Font("隶书", Font.PLAIN, DEFAULT_SIZE));
contentPane.add(labelT, BorderLayout.NORTH );
faceCombo = new JComboBox();
faceCombo.setEditable(true);
faceCombo.addItem("隶书");
faceCombo.addItem("华文新魏");
faceCombo.addItem("华文行楷");
faceCombo.addItem("华文隶书");
//*********Found**********
faceCombo.________________________(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//*********Found**********
labelT.setFont(new Font((String)____________________.getSelectedItem(),
Font.PLAIN, DEFAULT_SIZE));
}
});
JPanel comboPanel = new JPanel();
comboPanel.add(faceCombo);
contentPane.add(comboPanel, BorderLayout.SOUTH);
}
private JLabel label;
private JLabel labelT;
private JFileChooser chooser;
private JComboBox faceCombo;
private static final int DEFAULT_SIZE = 24;
public static final int DEFAULT_WIDTH = 570;
public static final int DEFAULT_HEIGHT = 400;
}

10:计算并打印1,3,5,7的阶乘以及这些阶乘的和。完成程序,使程序输出结果如下:
1!= 1
3!= 6
5!= 120
7!= 5040
Sum=5167


public class Java_1
{

public static void main(String[] args)
{
long sum;
//*********Found**********
___________________;
for(int i=1;i<8;i+=2){
long b=1;
//*********Found**********
for(int j=1; j<=___________; j++)
//*********Found**********
__________________;
System.out.println( i + "!= " + b);
sum+=b;
}
System.out.println("sum=" + sum);

}
}

...全文
208 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
smilebombht 2011-10-24
  • 打赏
  • 举报
回复
图相当于挂了
Strive20101101 2011-10-24
  • 打赏
  • 举报
回复
我并不认为有多少人愿意花时间来读你的代码。
哀木落 2011-10-19
  • 打赏
  • 举报
回复
哇~~此图片来自qq空间
ay_changyafei 2011-10-18
  • 打赏
  • 举报
回复
高手们 把空给填完了 能告诉我是为什么吗?

谢谢了。 给点解析。。。 - -。 thanks。
格式如下:

试题解析:
【第一空】
参考答案:throws
解析:若是该方法中有异常的话,会直接抛出异常信息。

【第二空】
参考答案:readLine()
解析:逐行读取用户输入的信息。

【第三空】
参考答案:parseInt
解析:类型转换。将String类型转换为Integer。


ay_changyafei 2011-10-18
  • 打赏
  • 举报
回复

11:完成程序,使程序输出结果如下图所示。点击open file按钮,打开文件对话框,选择文件,则文本框中输出所选文件的路径;点击save file按钮,打开文件对话框,在对话框中输入文件名,则文本框中输出保存文件的路径。

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

public class Java_2 implements ActionListener {
private JFrame frame;
private JButton button,saveButton;
private JTextArea textArea;
private JFileChooser dia;
private JPanel buttonPanel;

public void init() {
frame=new JFrame("file chooser");
dia=new JFileChooser();
button=new JButton("open file");
button.setActionCommand("open");
//*********Found**********
_______________________________(this);

saveButton=new JButton("save file");
//*********Found**********
_______________________________(this);

buttonPanel=new JPanel();
buttonPanel.add(button);
buttonPanel.add(saveButton);

textArea=new JTextArea("",10,10);
//*********Found**********
frame.getContentPane().add(__________________,BorderLayout.NORTH);
frame.getContentPane().add(textArea,BorderLayout.CENTER);

frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
//*********Found**********
public void ________________________(ActionEvent event) {
//*********Found**********
if(event.getActionCommand().equals("_______________"))
dia.showOpenDialog(frame);
else
dia.showSaveDialog( frame );

dia.setVisible(true);
File file=dia.getSelectedFile();
if (file!=null){
String fileName=file.getAbsolutePath();
textArea.append("path of selected file: "+fileName+"\r\n");
}
}
public static void main(String args[]){
Java_2 example=new Java_2();
example.init();
}
}
14:程序列出当前目录下所有文件和文件夹的名称。如果当前目录含有子文件夹,则也递归地列出子文件夹的内容,用缩进地方式反映层次关系,文件夹的名称用尖括号括起来。某次运行结果如下:
当前的工作目录是:Z:\test
Java_2.class
<source>
<Java1>
Java_1.java
<Java2>
Java_2.java
<Java3>
Java_3.java
import java.io.File;

public class Java_2
{
public static void main(String s[])
{
//Getting the Current Working Directory
String curDir = System.getProperty("user.dir");
System.out.println("当前的工作目录是:"+curDir);

//*********Found**********
File ff=new File(____________________);
String[] files=ff.list();
for(int i=0; i<files.length; i++)
{
String ss=curDir+"\\"+files[i];
traverse(0,ss);
}
}

/**
* 递归地遍历目录树
* @param level 目录的层次
* @param s 当前目录路径名
*/
public static void traverse(int level,String s)
{
File f=new File(s);
for(int i=0; i<level; i++) System.out.print(" ");
if(f.isFile())
{
System.out.println(f.getName());
}
else if(f.isDirectory())
{
//*********Found**********
System.out.println("<"+f._____________________+">");
String[] files=f.list();
level++;
//*********Found**********
for(int i=0; i<files.length;________________________)
{
String ss=s+"\\"+files[i];
//*********Found**********
traverse(_____________________,ss);
}
}
else
{
System.out.println("ERROR!");
}
}
}

15:1.点击上图中的“JDialog”按钮,弹出下图所示的对话框;
2.输入姓名、口令后,在上图文本框中显示下图所输入的数据。

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

//*********Found**********
public class Java_3 __________________ WindowAdapter implements ActionListener
{
private JFrame frame;
private JTextField name;
private JPasswordField pass;
private JLabel nameLabel;
private JLabel passLabel;
private JPanel textPanel;
private JPanel labelPanel;
private JButton button;
private JTextArea textArea;

public void initGUI()
{
frame=new JFrame("Frame with Dialog");
frame.addWindowListener(this);
button=new JButton("JDialog");
//*********Found**********
button._______________(this);
textArea=new JTextArea("",3,10);

frame.getContentPane().add(textArea,BorderLayout.CENTER);
frame.getContentPane().add(button,BorderLayout.NORTH);

frame.setSize(400,300);
frame.setVisible(true);

}

public void actionPerformed(ActionEvent e)
{
final JDialog dia=new JDialog(frame,"login information");
JButton ok=new JButton("ok");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textArea.setText("");
textArea.append("name:"+name.getText()+"\r\n");
textArea.append("passWord:"+new String(pass.getPassword())+"\r\n");
//*********Found**********
dia.setVisible(________________); //隐藏对话框
}
});

name=new JTextField("",10);
pass=new JPasswordField("",10);
pass.setEchoChar('*');
textPanel=new JPanel();
textPanel.setLayout(new GridLayout(2,1,10,10));
textPanel.add(name);
textPanel.add(pass);

nameLabel=new JLabel("name");
passLabel=new JLabel("passWord");
labelPanel=new JPanel();
labelPanel.setLayout(new GridLayout(2,1,20,20));
labelPanel.add(nameLabel);
labelPanel.add(passLabel);

dia.getContentPane().add(labelPanel,BorderLayout.WEST);
dia.getContentPane().add(textPanel,BorderLayout.CENTER);
dia.getContentPane().add(ok,BorderLayout.SOUTH);
dia.setSize(200,130);
dia.setVisible(true);
}

public void windowClosing(WindowEvent event)
{
frame.setVisible(false);
System.exit(0);
}

public static void main(String args[])
{
Java_3 example=new Java_3();
//*********Found**********
example.____________;
}
}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧