您好这是我在JTable
创建JFrame
的第一堂课。
public class code_selection extends JFrame implements MouseListener {
public JTable table;
private Color myback=new Color(0,85,170);
public int v,row,col;
public int h,str1;
public JScrollPane jsp;
public JPanel jp;
private int red=10;
private int green=125;
private int blue=10;
private Color mygreen=new Color(red,green,blue);
conn co=new conn();
public String str;
public String st2[]={"NAME","CODE"} ;
public String st3[][]={{"navneet","321"},
{"hehehe","123"},
{"hehehe","456"},
{"hehehe","789"}};
public code_selection()
{
setSize(620,400);
setTitle("Navneet Software Solutions");
Container contentPane=getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(mygreen);
setLocationRelativeTo(null);
setUndecorated(true);
table=new JTable(st3,st2);
table.setBackground(Color.WHITE);
table.setForeground(Color.BLACK);
table.setGridColor(Color.BLACK);
table.setBounds(0, 0, 400, 500);
table.addMouseListener(this);
v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
jsp=new JScrollPane(table,v,h);
jsp.setBounds(10,10,600,380);
add(jsp);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
try {
row=table.getSelectedRow();
col=0;
str=table.getModel().getValueAt(row, col).toString();
col=1;
str1=Integer.parseInt(table.getModel().getValueAt(row, col).toString());
dispose();
}
catch(Exception e)
{
System.out.println(e);
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
这是我的第二课,我用文本字段和按钮创建了框架。 当我点击按钮时,我的桌架打开了。
public class amt_deposit extends JPanel implements MouseListener {
private ImageIcon i1,i2,i3,i4;
private JLabel l1,l2,l3,l4,l5,l6;
public JTextField t2,t3;
private JButton b1,b2,b3;
private DatePicker datePicker=new DatePicker();
private Color myback=new Color(0,85,170);
public int st;
conn con1=new conn();
public amt_deposit() {
try {
setLayout(null);
setBackground(myback);
i1=new ImageIcon("images/deposit.png");
l1=new JLabel(i1);
l1.setBounds(10,10,70,85);
l2=new JLabel("Amount Deposit");
l2.setBounds(83, 50, 200, 50);
l2.setForeground(Color.WHITE);
l2.setFont(new Font("arial",Font.BOLD,21));
l2.setBackground(myback);
l2.setOpaque(true);
l5=new JLabel("Date :- ");
l5.setBounds(100,150,100,50);
l5.setForeground(Color.WHITE);
l5.setFont(new Font("arial",Font.BOLD,16));
l5.setBackground(myback);
l5.setOpaque(true);
datePicker.setBounds(230,163,165,23);
datePicker.setText("Select");
l3=new JLabel("Patient Code :- ");
l3.setBounds(100,200,130,50);
l3.setForeground(Color.WHITE);
l3.setFont(new Font("arial",Font.BOLD,16));
l3.setBackground(myback);
l3.setOpaque(true);
b3=new JButton("...");
b3.setBounds(370, 213, 30, 25);
//b3.addActionListener(this);
b3.addMouseListener(this);
//b3.setBackground(myback);
//b3.setOpaque(true);
t2=new JTextField();
t2.setBounds(230,213,140,25);
t2.addMouseListener(this);
l4=new JLabel("Amount :- ");
l4.setBounds(100,250,100,50);
l4.setForeground(Color.WHITE);
l4.setFont(new Font("arial",Font.BOLD,16));
l4.setBackground(myback);
l4.setOpaque(true);
t3=new JTextField();
t3.setBounds(230,263,140,25);
i2=new ImageIcon("images/ok2.png");
b1=new JButton(i2);
b1.setBounds(80,360,110,40);
b1.setBackground(myback);
b1.setOpaque(true);
i3=new ImageIcon("images/cancel2.png");
b2=new JButton(i3);
b2.setBounds(220,360,110,40);
b2.setBackground(myback);
b2.setOpaque(true);
i4=new ImageIcon("images/owl.png");
l6=new JLabel(i4);
l6.setBounds(800,30,200,120);
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(datePicker);
add(t2);
add(t3);
add(b1);
add(b2);
add(l6);
add(b3);
setVisible(true);
}
catch(Exception e)
{
System.out.println("Error in amt_deposit "+e);
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
try {
code_selection cs=new code_selection();
st=cs.table.getRowCount();
String str5=cs.table.getModel().getValueAt(st, 0).toString();
String str6=cs.table.getModel().getValueAt(st, 1).toString();
System.out.println(str5+str6);
t2.setText(str5);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
当我点击按钮时,我的框架打开,我在其中创建了一个表格。 我想从表中获取选定的值并设置在另一帧的文本字段中。
我正在使用正确的程序和正确的方法,但我仍然收到错误。
java.lang.ArrayIndexOutOfBoundsException:-1
你能帮帮我吗?你能告诉我我哪里错了吗?
为了您的信息,我能够从表格所在的同一帧上的表中获取值,但是我无法从表格到另一帧中选择值。