简答题 Java
题号 #2246019

本题是一个表格式的成绩单,其中包括“姓名”、“英语成绩”、“数学成绩”和“总成绩”,姓名和成绩都可以进行修改,单击按钮“计算每人总成绩”,则可以统计出每个人的总成绩并显示在总成绩栏中。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class java2 extends JFrame implements ActionL-
istener
{JTable table;Object a[][];
Object name[]={"姓名","英语成绩","数学成 绩","总成绩"};
JButton button;
java2()
{setTitle("java2");
a=new Object[8][4]:
for(int i=0;i<8;i++) 
{for(int j=0;j<4;j++)
{if(j!=0)
a[i][j]="";
else
a[i][j]="';
}
}
button=new JButton("计算每人总成绩");
table=       
button.addActionListener(this);
getContentPane().add(new JScrollPane(table),
BorderLayout.CENTER);
getContentPane().add(button,BorderLayout.
SOUTH):
setSize(400,200);
setVisible(true):
validate();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(O);
}
});
}
public void actionPerformed(ActionEvent e)
{for(int i=0;i<8;i++)
{double sum=0;
boolean boo=true;
for(int J=1;j<=2;j++)
{try{
sum=sum+Double.parseDouble(       );
}
catch(Exception ee)
{
boo=false;
table.repaint();
}
if(boo= =true)
{
a[i][3]=""+sum:
table.repaint();
}
}
}
}
public static void main(String args[-])
{java2 Win=new java2();
}
}

进入在线模考

参考答案

暂无

答案解析

第1处:new.JTable(a,name)
第2处:a[i][J].toString()
【解析】第1处初始化表格变量;第2处取得表格中单元格内容并转换成Double型计算出总成绩。

你可能感兴趣的试题

1 本题中,通过菜单“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;
}
3 本题中定义了一个树型的通信录,窗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();}
}
找试题 · 找答案 · 找资料 在线模考