简答题 Java
题号 #2246024

本题中,通过菜单“Connect”显示一个对话框,单击“ok”按钮后,所填写的内容就会传回到主窗口并显示出来。
import java.awt.* ;
import java.awt.event.*;
import javax.swin9.*;
public class java3 extends JFrame implements ActionL-
istener{
public java3(){
setTitle("java3");
setSize(300,300);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
JMenuBar mbar = new JMenuBar();
setJMenuBar(bar);
JMenu fileMenu=new JMenu("File");
mbar.add(fileMenu);
connectltem=new JMenuhem("Connect");
connecthem.addActionListener(this);
fileMenu.add(connecthem);
exithem=new JMenuhem("Exit");
exithem.addActionListener(this);
fileMenu.add(exithem);
}
public void actionPerformed(ActionEvent evt){
Object source=evt.getSource();
if(source= =connecthem){
Connectlnfo transfer=new ConnectInfo ("your-
name","pw");
if(dialog= =null)
dialog=new ConnectDialog(this);
if(dialog.showDialog(transfer)){
String uname=transfer.username;
String pwd=transfer.password;
Container contentPane=getContentPane();
contentPane.add(new JLabel("username="+
uname+",password="+pwd),"South");
validate();
}
}
else if(source= =exitltem)
System.exit(0);
} 
public static void main(String[]args){
JFrame f=new java3();
f.show();
}
private ConnectDialog dialog=null;
private JMenuhem connecthem;
private JMenuhem exithem;
}
class Connectlnfo{
public String username;
public String password;
public Connectlnfo(String U,String p){
username=u;password=P;
}
}
class ConnectDialog extends JDialog implements Ac-
tionListener{
public ConnectDialog(){
super(parent,"Connect",true);
Container contentPane=getContentPane();
JPanel pl=new JPanel();
pl.setLayout(new GridLayout(2,2));
pl.add(newJLabel("User name:"));
pl.add(username=new JTextField(""));
pl.add(new JLabel("Password:"));
pl.add(password=new JPasswordField(""));
contentPane.add("Center",pl);
Panel p2=new Panel();
okButton=addButton(p2,"ok");
cancelButton=addButton(p2。"Cancel");
contentPane.add("South",p2);
setSize(240,120);
}
JButton addButton(Container C,String name){
JButton button=new JButton(name);
button.addActionListener(this);
C.add(button);
return button;
}
public void actionPerformed(ActionEvent evt){
object source=evt.getSource();
if(source= =okButton){
ok=true:
setVisible(false);
}
else if(source= =cancelButton)
setVisible(false);
}
public void showDialog(Connectlnfo transfer){
username.setText(transfer.username);
password.setText(transfer.password);
ok=false;
show();
if(ok){
transfer.username=username.getText();
transfer.password=new String(password.get-
Password());
}
return ok;
}
private JTextField username ;
private JPasswordField password;
private boolean ok;
private JButton okButton; 
private JButton cancelButton;
}

进入在线模考

参考答案

暂无

答案解析

第1处:setJMenuBar(mbar)
第2处:public ConnectDialog(JFrame parent)
第3处:public Boolean showDialog(Connectlnfotransfer)
【解析】第1处参数错误,bar未定义;第2处从下一行的super(parent,"Connect",true);可以看出,这里需要的参数为父窗体;第3处从下面的return ok;等可以看出,这是一个有Boolean型返回值的函数,故类型应为Boolean。

你可能感兴趣的试题

2 本题中定义了一个树型的通信录,窗El左侧是一个树,
右侧是一个文本域,单击树的结点,则在右侧文本域中显示
相关信息,如果单击的是树结点,则显示对应名字的电话
信息。
import javax.swing.*;
import javax.swing.tree.*; 
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
class Mytree2 extends JFrame     
{JTree tree=null;JTextArea text=new JTextArea
(20,20);
Mytree2()
{Container con=getContentPane();
DefauhMutableTreeNode root=new Default-
MutableTreeNode("同学通信录");
DefaultMutableTreeNode tl=new Default-
MutableTreeNode("大学同学");
DefaultMutableTreeNode t2=new Default-
MutableTreeNode("研究生同学");
DefaultMutableTreeNode tl l=new Default-
MutatleTreeNode("陈艳");
DefaultMutableTreeNode tl 2=new Default-
MutableTreeNode("李小永");
DefaultMutableTreeNode t2 1=new Defauh-
MutableTreeNode("王小小");
DefauhMutableTreeNode t2 2=new Defauh-
MutableTreeNode("董小");
setTitle("java2");
root.add(t1);root.add(t2);
tl.add(t1_1);tl.add(t1_2);t2.add(t2_1);t2.
add(t2_2);
tree=new JTree(root); 
JSerollPane scrollpane=new JScrollPane(text);
JSplitPane splitpane=new JSplitPane(JSplitPane.
HORIZONTAL SPLIT,
true,tree,scrollpane);
tree.addTreeSeleetionListener(this); 
con.add(splitpane);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{System.exit(0);}});
setVisible(true);setBounds(70,80,200,300);
}
public void valueChanged(TreeSelectionEvent e)
{if(e.getSouree()= =tree)
{DefauhMutableTreeNode node=
(DefaultMutableTreeNode)tree.getLastSelected-
PathComponent();
if(node.isLeaf())
(String str=      ;
if(str.equals("陈艳"))
(text.setText(str+":联系电话:0411-
4209876");}
else if(str.equals("李小永")) 
{text.setText(str+":联系电话:010-
62789876");} 
else if(str.equals("王小小"))
{text.setText(str+":联系电话:0430-
63596677");)
else if(str.equals("董小"))
{text.setText(str+":联系电话:020-
85192789");}
}
else
{text.setText(node.getUserObject().toString
());
}
}
}
} 
public class java2
{public static void main(String args[])
{Mytree2 win=new Mytree2();win.pack();}
}
3 本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。
import java.awt.*;
import java.awt.event.*;
import javax swing.*;
class MousePanel extends JPanel extends MouseMo-
tionListener
{public MousePanel()
{addMouseListener(new MouseAdapter()
{public void mousePressed(MouseEvent evt)
{int X=evt.getX();
int Y=evt.getY();
current=find(x,y);
if(current<0)
add(x,y);
}
public void mouseClicked(MouseEvent evt)
{int X=evt.getX();
int Y=evt.getY();
if(evt.getClickCount()>=2)
{remove(current);
}
} 
});
addMouseMotionListener(this);
}
public void paintComponent(Graphics g)
{super.paintComponent();
for(int i=0;i<nsquares;i++)
draw(g,i);
}
public int find(int X,int y)
(for(int i=0;i<nsquares;i++)
if(squares[i].x-SQUARELENGTH/2<=
x&&
X<=squares[i].x+SQuARELENGTH/2
&&squares[i].Y-SQUARELENGTH/2<
=Y
&&y<=squares[i].Y+SQUARELENGTH
/2)
return i ;
return-1 ;
}
public void draw(Graphics g,int i)
{g.drawRect(squares[i].X-SQUARE-
LENGTH/2。
squares[i].Y-SQUARELENGTH/2,
SQUARELENGTH,
SQUARELENGTH);
}
public void add(int X,int Y)
{if(nsquares<MAXNSQUARES)
{squares[nsquares]=new Point(x,y);
current=nsquares ;
nsquares++;
repaint();
}
}
public void remove(int n)
{if(n<0 ‖ n>=nsquares)return;
Nsquares- -;
squares[n]=squares[nsquares];
if(current= =n)current= -l;
repaint();
}
public void mouseMoved(MouseEvent evt)
{}
public void mouseDragged(MouseEvent evt)
{}
private static final int SQUARELENGTH=10:
private static final int MAXNSQUARES=100;
private Point[]squares=new Point[MAX-
NSQUARES];
private int nsquares=0;
private int current=-l;
}
class MouseFrame extends JFramc
{public MouseFrame()
{setTitle("java3");
setSize(300,200);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(MousePanel());
} 
}
public class java3
{public static void main(String[]args)
{JFrame frame=new MouseFrame();
frame.show();
}
}
找试题 · 找答案 · 找资料 在线模考