当前位置: 技术问答>java相关
能给个jtable的例子吗?
来源: 互联网 发布时间:2015-10-21
本文导语: 简单的能显示数据就行了,但要是applet,不能是连数据库,数组或Vector都行 | //刚做的作业题,正好对上 package p6_13; import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; impo...
简单的能显示数据就行了,但要是applet,不能是连数据库,数组或Vector都行
|
//刚做的作业题,正好对上
package p6_13;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.math.*;
import java.util.*;
import javax.swing.table.*;
class PhoneCard
{
//variables:--------------------------------
long cardNumber;
int passWord;
double balance;
boolean connected;
static String connectNumber;
static double additionalfee;
//constructor:------------------------------
PhoneCard(int pw,double b)
{
cardNumber=createcardnum();
passWord=pw;
if(b>0)
balance=b;
else
System.exit(1);
connectNumber="200";
additionalfee=0.1;
connected=false;
}
//methods:----------------------------------
long createcardnum()
{
double a=Math.random();
long r=(long)(a*10000000);
return r;
}
void setCardNumber(long cardNumber)
{
this.cardNumber=cardNumber;
}
void setPassWord(int passWord)
{
this.passWord=passWord;
}
void setBalence(double balance)
{
this.balance=balance;
}
void setconnected(boolean connected)
{
this.connected=connected;
}
public String toString()
{
String s=new String();
s+="电话卡接入号码: "+connectNumber+'n';
s+="电话卡卡号: "+cardNumber+'n';
s+="电话卡密码: "+passWord+'n';
s+="剩余金额: "+balance+'n';
s+="附加费用: "+additionalfee+'n';
if(connected)
s+="电话已接通"+'n';
else
s+="电话未接通"+'n';
return s;
}
}
public class p6_13 extends JApplet implements ActionListener
{
Vector cardlist;
PhoneCard nowcard;
JButton addcard;
JPasswordField psw;
JTextField money;
JTextArea info;
JLabel l1,l2,l3;
JTable table;
public void init()
{
//变量初始化:
cardlist=new Vector(100,50);
addcard=new JButton("添加");
l1=new JLabel("密码:",JLabel.CENTER);
l2=new JLabel("金额:",JLabel.CENTER);
l3=new JLabel("电话卡信息:",JLabel.CENTER);
psw=new JPasswordField(10);
money=new JTextField();
Vector names=new Vector();
names.add("卡号");
names.add("金额");
names.add("密码");
table=new JTable(new mytable(cardlist));
table.setPreferredScrollableViewportSize(new Dimension(550,30));
JScrollPane scrollPane=new JScrollPane(table);
info=new JTextArea(13,3);
//添加控件:
Container contentPane=this.getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel panelup=new JPanel(new GridLayout(1,10));
JPanel paneldown=new JPanel(new BorderLayout());
panelup.add(l1);
panelup.add(psw);
panelup.add(l2);
panelup.add(money);
panelup.add(addcard);
paneldown.add(l3,BorderLayout.NORTH);
paneldown.add(info,BorderLayout.SOUTH);
contentPane.add(panelup,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(paneldown,BorderLayout.SOUTH);
//设置监听:
addcard.addActionListener(this);
}
//插入向量
void insert()
{
boolean inserted=false;
int l=cardlist.toArray().length;
int i;
if(l==0)
{
inserted=true;
cardlist.insertElementAt(nowcard,0);
}
for(i=0;inowcard.balance&&!inserted)
{
cardlist.insertElementAt(nowcard,i);
inserted=true;
}
}
if(!inserted)
cardlist.insertElementAt(nowcard,i);
}
public void actionPerformed(ActionEvent e)
{
int password=Integer.parseInt(new String(psw.getPassword()));
double balance=Double.valueOf(money.getText()).doubleValue();
nowcard=new PhoneCard(password,balance);
insert();
table.setModel(new mytable(cardlist));
info.setText(nowcard.toString());
}
}
class mytable extends AbstractTableModel
{
Vector cardlist;
public mytable(Vector list)
{
cardlist=list;
}
public int getColumnCount()
{
return 3;
}
public int getRowCount()
{
return cardlist.toArray().length;
}
public Object getValueAt(int row,int col)
{
if(col==0)
return Long.toString(((PhoneCard)cardlist.elementAt(row)).cardNumber);
if(col==1)
return Double.toString(((PhoneCard)cardlist.elementAt(row)).balance);
if(col==2)
return Integer.toString(((PhoneCard)cardlist.elementAt(row)).passWord);
return null;
}
public String getColumnName(int col)
{
if(col==0)
return "卡号";
if(col==1)
return "金额";
if(col==2)
return "密码";
return "";
}
}
package p6_13;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.math.*;
import java.util.*;
import javax.swing.table.*;
class PhoneCard
{
//variables:--------------------------------
long cardNumber;
int passWord;
double balance;
boolean connected;
static String connectNumber;
static double additionalfee;
//constructor:------------------------------
PhoneCard(int pw,double b)
{
cardNumber=createcardnum();
passWord=pw;
if(b>0)
balance=b;
else
System.exit(1);
connectNumber="200";
additionalfee=0.1;
connected=false;
}
//methods:----------------------------------
long createcardnum()
{
double a=Math.random();
long r=(long)(a*10000000);
return r;
}
void setCardNumber(long cardNumber)
{
this.cardNumber=cardNumber;
}
void setPassWord(int passWord)
{
this.passWord=passWord;
}
void setBalence(double balance)
{
this.balance=balance;
}
void setconnected(boolean connected)
{
this.connected=connected;
}
public String toString()
{
String s=new String();
s+="电话卡接入号码: "+connectNumber+'n';
s+="电话卡卡号: "+cardNumber+'n';
s+="电话卡密码: "+passWord+'n';
s+="剩余金额: "+balance+'n';
s+="附加费用: "+additionalfee+'n';
if(connected)
s+="电话已接通"+'n';
else
s+="电话未接通"+'n';
return s;
}
}
public class p6_13 extends JApplet implements ActionListener
{
Vector cardlist;
PhoneCard nowcard;
JButton addcard;
JPasswordField psw;
JTextField money;
JTextArea info;
JLabel l1,l2,l3;
JTable table;
public void init()
{
//变量初始化:
cardlist=new Vector(100,50);
addcard=new JButton("添加");
l1=new JLabel("密码:",JLabel.CENTER);
l2=new JLabel("金额:",JLabel.CENTER);
l3=new JLabel("电话卡信息:",JLabel.CENTER);
psw=new JPasswordField(10);
money=new JTextField();
Vector names=new Vector();
names.add("卡号");
names.add("金额");
names.add("密码");
table=new JTable(new mytable(cardlist));
table.setPreferredScrollableViewportSize(new Dimension(550,30));
JScrollPane scrollPane=new JScrollPane(table);
info=new JTextArea(13,3);
//添加控件:
Container contentPane=this.getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel panelup=new JPanel(new GridLayout(1,10));
JPanel paneldown=new JPanel(new BorderLayout());
panelup.add(l1);
panelup.add(psw);
panelup.add(l2);
panelup.add(money);
panelup.add(addcard);
paneldown.add(l3,BorderLayout.NORTH);
paneldown.add(info,BorderLayout.SOUTH);
contentPane.add(panelup,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(paneldown,BorderLayout.SOUTH);
//设置监听:
addcard.addActionListener(this);
}
//插入向量
void insert()
{
boolean inserted=false;
int l=cardlist.toArray().length;
int i;
if(l==0)
{
inserted=true;
cardlist.insertElementAt(nowcard,0);
}
for(i=0;inowcard.balance&&!inserted)
{
cardlist.insertElementAt(nowcard,i);
inserted=true;
}
}
if(!inserted)
cardlist.insertElementAt(nowcard,i);
}
public void actionPerformed(ActionEvent e)
{
int password=Integer.parseInt(new String(psw.getPassword()));
double balance=Double.valueOf(money.getText()).doubleValue();
nowcard=new PhoneCard(password,balance);
insert();
table.setModel(new mytable(cardlist));
info.setText(nowcard.toString());
}
}
class mytable extends AbstractTableModel
{
Vector cardlist;
public mytable(Vector list)
{
cardlist=list;
}
public int getColumnCount()
{
return 3;
}
public int getRowCount()
{
return cardlist.toArray().length;
}
public Object getValueAt(int row,int col)
{
if(col==0)
return Long.toString(((PhoneCard)cardlist.elementAt(row)).cardNumber);
if(col==1)
return Double.toString(((PhoneCard)cardlist.elementAt(row)).balance);
if(col==2)
return Integer.toString(((PhoneCard)cardlist.elementAt(row)).passWord);
return null;
}
public String getColumnName(int col)
{
if(col==0)
return "卡号";
if(col==1)
return "金额";
if(col==2)
return "密码";
return "";
}
}