import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test01 extends JFrame {
private JTable table;
Object[][]data={{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE}
};
String [] headerName={"1","2","3","4","5","6"};
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public test01() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(
headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener{
boolean isPress=false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
Object target = e.getSource();
if((JTable)target!=table){
return;
}
int selectedCol;
int selectedRow;
selectedRow = table.getSelectedRow();
selectedCol = table.getSelectedColumn();
if (selectedRow < 0) {
return;
}
if (selectedCol < 0) {
return;
}
Object obj = table.getValueAt(selectedRow, selectedCol);
Boolean val = (Boolean) obj;
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString());
for(int i=0;i<table.getColumnCount();i++){
if(selectedCol==i){
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString()+" );
}
}
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false,selectedRow ,i );
}
}
table.updateUI();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(isPress){
doPro(e);
isPress=false;
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isPress=true;
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
只要在你的eclipse 上运行一下 就可知道有什么问题? 要实现的功能每行每列只能选择一个 哪位好心的大哥哥 能帮帮小妹?
68 个解决方案
#1
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString()+" );多了个"
+ "列 " +"值为:"+ val.toString()+" );多了个"
#2
if ((i != selectedRow) && (i != -1))为什么要加i != -1?i会等于-1吗?
#3
package com.zhh.test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class Test extends JFrame {
private JTable table;
Object[][] data = {
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE } };
String[] headerName = { "1", "2", "3", "4", "5", "6" };
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public Test() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener {
boolean isPress = false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
#4
类名小写太不应该了。。。
我午睡了。。有空再看
我午睡了。。有空再看
#5
小妹 就是初学者呀
#6
你好 问题不在这呀 在帮忙看看吧 谢谢啦
#7
知道问题应该不在那。只是对那段代码表示疑问
#8
package com.zy.test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test01 extends JFrame {
private JTable table;
Object[][] data = {
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE } };
String[] headerName = { "1", "2", "3", "4", "5", "6" };
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public test01() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener {
boolean isPress = false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
Object target = e.getSource();
if((JTable)target!=table){
return;
}
int selectedCol;
int selectedRow;
selectedRow = table.getSelectedRow();
selectedCol = table.getSelectedColumn();
if (selectedRow < 0) {
return;
}
if (selectedCol < 0) {
return;
}
Object obj = table.getValueAt(selectedRow, selectedCol);
Boolean val = (Boolean) obj;
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString());
for(int i=0;i<table.getColumnCount();i++){
if(selectedCol==i){
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
}
}
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false,selectedRow ,i );
}
}
table.updateUI();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (isPress) {
doPro(e);
isPress = false;
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isPress = true;
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
#9
呵呵 我承认不完美 好哥哥 你看看到底是那块的问题呀 谢谢啦
#10
把包去掉(删除package com.zy.test;)
#11
不会吧 你运行 试了?
#12
当然,运行成功~~
你选择的是:1行4列 值为:true
你选择的是:1行4列 值为:true
你选择的是:2行3列 值为:true
你选择的是:2行3列 值为:true
#13
把你贴上来的换成这个:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test01 extends JFrame {
private JTable table;
Object[][] data = {
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE } };
String[] headerName = { "1", "2", "3", "4", "5", "6" };
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public test01() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener {
boolean isPress = false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
Object target = e.getSource();
if((JTable)target!=table){
return;
}
int selectedCol;
int selectedRow;
selectedRow = table.getSelectedRow();
selectedCol = table.getSelectedColumn();
if (selectedRow < 0) {
return;
}
if (selectedCol < 0) {
return;
}
Object obj = table.getValueAt(selectedRow, selectedCol);
Boolean val = (Boolean) obj;
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString());
for(int i=0;i<table.getColumnCount();i++){
if(selectedCol==i){
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
}
}
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false,selectedRow ,i );
}
}
table.updateUI();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (isPress) {
doPro(e);
isPress = false;
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isPress = true;
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
#14
偶尔也会出现 选多个的情况啊
#15
根据打印信息,觉得好像鼠标监听有些问题
你选择的是:1行2列 值为:true
你选择的是:1行2列 值为:true
你选择的是:0行2列 值为:true
你选择的是:0行2列 值为:true
你选择的是:1行2列 值为:true
你选择的是:1行2列 值为:true
------------------------------此处其实点击0行2列,但是没有打印信息
你选择的是:1行2列 值为:false
你选择的是:1行2列 值为:false
#16
恩就是的 当没有打印信息时 会出现多选的情况
#17
小妹又来啦。

#18
又是你 给我想想办法吧 明天就要交呢
#19
不着急,慢慢等。真没人解决了我帮你解决。
#20
你又只说不做
#21
根据打印,出现多选时,只检测到了mousePressed和mouseReleased事件,没有检测到mouseClicked事件。解决问题可以从这角度出发
#22
这个问题困扰 我有些时间了 我真的没办法了 谢谢你了 帮帮我吧 找出原因
#23
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test01 extends JFrame {
private JTable table;
Object[][]data={{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE}
};
String [] headerName={"1","2","3","4","5","6"};
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public test01() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(
headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener{
boolean isPress=false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
Object target = e.getSource();
if((JTable)target!=table){
return;
}
int selectedCol;
int selectedRow;
selectedRow = table.getSelectedRow();
selectedCol = table.getSelectedColumn();
if (selectedRow < 0) {
return;
}
if (selectedCol < 0) {
return;
}
Object obj = table.getValueAt(selectedRow, selectedCol);
Boolean val = (Boolean) obj;
//System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
for(int i=0;i<table.getColumnCount();i++){
if(selectedCol==i){
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
}
}
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false,selectedRow ,i );
}
}
table.updateUI();
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(isPress){
doPro(e);
isPress=false;
}
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isPress=true;
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
不知道是这个意思吗
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test01 extends JFrame {
private JTable table;
Object[][]data={{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE}
};
String [] headerName={"1","2","3","4","5","6"};
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public test01() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(
headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener{
boolean isPress=false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
Object target = e.getSource();
if((JTable)target!=table){
return;
}
int selectedCol;
int selectedRow;
selectedRow = table.getSelectedRow();
selectedCol = table.getSelectedColumn();
if (selectedRow < 0) {
return;
}
if (selectedCol < 0) {
return;
}
Object obj = table.getValueAt(selectedRow, selectedCol);
Boolean val = (Boolean) obj;
//System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
for(int i=0;i<table.getColumnCount();i++){
if(selectedCol==i){
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
}
}
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false,selectedRow ,i );
}
}
table.updateUI();
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(isPress){
doPro(e);
isPress=false;
}
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isPress=true;
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
不知道是这个意思吗
#24
有个小建议....
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString());
可以给selectedRow和selectedCol都....行和列都从0开始,感觉不舒服。
其他的,完全不懂,我是菜鸟级新手,或者是新手级菜鸟。
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString());
可以给selectedRow和selectedCol都....行和列都从0开始,感觉不舒服。
其他的,完全不懂,我是菜鸟级新手,或者是新手级菜鸟。
#25
为什么写了selectedRow和selectedCol都+1里面的+1不见了?迷茫
#26
问题不在这呀
#27
每行每列至多选中一个:
在方法 mousePressed(MouseEvent e)添加
是要这种效果吗?
在方法 mousePressed(MouseEvent e)添加
JTable tempTable = ((JTable) e.getSource());
int i = tempTable.getSelectedRow();
int j = tempTable.getSelectedColumn();
int rowCount = tempTable.getRowCount();
int columnCount = tempTable.getColumnCount();
Boolean val = (Boolean) tempTable.getValueAt(i, j);
for (int row = 0; row < i; row++)
tempTable.setValueAt(false, row, j);
for (int row = i + 1; row < rowCount; row++)
tempTable.setValueAt(false, row, j);
for (int col = 0; col < j; col++)
tempTable.setValueAt(false, i, col);
for (int col = j + 1; col < columnCount; col++)
tempTable.setValueAt(false, i, col);
System.out.println("你选择的是:" + i + "行" + j + "列 " + "值为:"
+ val.toString());
是要这种效果吗?
import java.awt.*; //写程序不注释,看得脑袋疼
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class test01 extends JFrame {
private JTable table;
Boolean[][] data = new Boolean[6][6];
String[] headerName = { "1", "2", "3", "4", "5", "6" };
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {//匿名内部类
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public test01() {
super();
for(int i=0; i<data.length ; i++) {//对data中的数据初始化
for(int j=0; j<data[i].length ; j++) {
data[i][j] = false;
}
}
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {//表格每列的名字
table.getColumnModel().getColumn(i).setHeaderValue(headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
}
class MousePress implements MouseListener {
//这句有什么用 ? ? boolean isPress = false;
/*
public void doPro(MouseEvent e) {//这里的名字为什么这么取?Pro是Process还是?
JTable target = (JTable) e.getSource();
int selectedRow = table.getSelectedRow();
int selectedCol = table.getSelectedColumn();
Boolean val = (Boolean) table.getValueAt(selectedRow, selectedCol);
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " + "值为:" + val.toString());
for (int i = 0; i < table.getColumnCount(); i++) {//你不是取出行号和列号了,你循环遍历它,又要干嘛,
if (selectedCol == i) {
System.out
.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " + "值为:" + val.toString() + " ");// error 少了一个"
}
}
//下面写法功能是什么????????????????????????????????????
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false, selectedRow, i);
}
}
table.updateUI();
}
*/
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
JTable tempTable = ((JTable) e.getSource());
int i = tempTable.getSelectedRow();
int j = tempTable.getSelectedColumn();
int rowCount = tempTable.getRowCount();
int columnCount = tempTable.getColumnCount();
Boolean val = (Boolean) tempTable.getValueAt(i, j);
for (int row = 0; row < i; row++)
tempTable.setValueAt(false, row, j);
for (int row = i + 1; row < rowCount; row++)
tempTable.setValueAt(false, row, j);
for (int col = 0; col < j; col++)
tempTable.setValueAt(false, i, col);
for (int col = j + 1; col < columnCount; col++)
tempTable.setValueAt(false, i, col);
System.out.println("你选择的是:" + i + "行" + j + "列 " + "值为:"
+ val.toString());
//取消你的算法哦,doPro(e);//添加方法
}
public void mouseReleased(MouseEvent e) {
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = (Boolean)obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
#28
小妞 ,我点击了上百次了,还未出现同一行有多个或同一列有多个的情况
#29
一群大哥哥给一个小妹妹解决问题,我就不凑这个热闹了。哈哈
#30
这是可以的........不错..不错.......小伙子有前途...
#31
小妹 我帮你
#32
我是来围观人妖的
#33
谢谢你啦 我试试
#34
不要用嘴 用实际行动吧!
#35
呵呵 问题终于解决了 谢谢你啦 看来你才是Swing 这方面的高手呀 小妹真不知道 该怎么谢你啦
#36
好人太多,这小妹的魅力太大。。。。这么长的代码。。。。。
#37
我也相信 ****上 全是好心人 呵呵
#38
但是还有点问题 就是打印出来的值 当你选中的时候 打印出来的全是 false 这个问题你发现了没? 呵呵
#39
小妹 你就 走IT 这趟浑水了 回头是岸
#40
System.out.println("你选择的是:" + i + "行" + j + "列 " + "值为:"
+ "true");
这一行写死不就完了么..
+ "true");
这一行写死不就完了么..
#41
现在看到代码就头晕.....哎.....
#42
巾帼不让须眉,嘎嘎
#43
采取的原则是已被选中小格 对应的值是true,没被选中小格 对应的值是false
因为空白格对应值是false,所以显示false,
如果获得true 你就连续两次点击小格 就会显示true了
#44
不过你的确没说清楚功能,除了要保证每行每列只有唯一被选中的功能外,你还需要效果呢?
#45
那怎样直接就让他 显示为 true呢 那说明你取到的 val值就有问题呀
#46
那个对应值 肯定要正确呀 呵呵 估计这对你来说 很容易吧
#47
送佛 送到西吧 呵呵 谢谢你啦
#48
把
[
System.out.println("你选择的是:" + i + "行" + j + "列 " + "值为:"
+ val.toString());
/code]
改为
[code=Java]
System.out.println("你选择的是:" + i + "行" + j + "列 " + "值为:"
+ !val);
就会出现很多很多true了
#49
这个不解决 根本问题呀 呵呵 我刚才也写了个
if(val){
val=true;
}else{
val=false;
}
呵呵 你再看看吧 把你写的那 放在click里就按照选择的直接打印 true 但是会有重复的
#50
上面那个写反了 if(val){
val=false;
}else{
val=true;
}
val=false;
}else{
val=true;
}
#1
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString()+" );多了个"
+ "列 " +"值为:"+ val.toString()+" );多了个"
#2
if ((i != selectedRow) && (i != -1))为什么要加i != -1?i会等于-1吗?
#3
package com.zhh.test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class Test extends JFrame {
private JTable table;
Object[][] data = {
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE } };
String[] headerName = { "1", "2", "3", "4", "5", "6" };
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public Test() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener {
boolean isPress = false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
#4
类名小写太不应该了。。。
我午睡了。。有空再看
我午睡了。。有空再看
#5
小妹 就是初学者呀
#6
你好 问题不在这呀 在帮忙看看吧 谢谢啦
#7
知道问题应该不在那。只是对那段代码表示疑问
#8
package com.zy.test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test01 extends JFrame {
private JTable table;
Object[][] data = {
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE } };
String[] headerName = { "1", "2", "3", "4", "5", "6" };
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public test01() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener {
boolean isPress = false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
Object target = e.getSource();
if((JTable)target!=table){
return;
}
int selectedCol;
int selectedRow;
selectedRow = table.getSelectedRow();
selectedCol = table.getSelectedColumn();
if (selectedRow < 0) {
return;
}
if (selectedCol < 0) {
return;
}
Object obj = table.getValueAt(selectedRow, selectedCol);
Boolean val = (Boolean) obj;
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString());
for(int i=0;i<table.getColumnCount();i++){
if(selectedCol==i){
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
}
}
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false,selectedRow ,i );
}
}
table.updateUI();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (isPress) {
doPro(e);
isPress = false;
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isPress = true;
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
#9
呵呵 我承认不完美 好哥哥 你看看到底是那块的问题呀 谢谢啦
#10
把包去掉(删除package com.zy.test;)
#11
不会吧 你运行 试了?
#12
当然,运行成功~~
你选择的是:1行4列 值为:true
你选择的是:1行4列 值为:true
你选择的是:2行3列 值为:true
你选择的是:2行3列 值为:true
#13
把你贴上来的换成这个:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test01 extends JFrame {
private JTable table;
Object[][] data = {
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE },
{ Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
Boolean.FALSE, Boolean.FALSE } };
String[] headerName = { "1", "2", "3", "4", "5", "6" };
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public test01() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener {
boolean isPress = false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
Object target = e.getSource();
if((JTable)target!=table){
return;
}
int selectedCol;
int selectedRow;
selectedRow = table.getSelectedRow();
selectedCol = table.getSelectedColumn();
if (selectedRow < 0) {
return;
}
if (selectedCol < 0) {
return;
}
Object obj = table.getValueAt(selectedRow, selectedCol);
Boolean val = (Boolean) obj;
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString());
for(int i=0;i<table.getColumnCount();i++){
if(selectedCol==i){
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
}
}
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false,selectedRow ,i );
}
}
table.updateUI();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (isPress) {
doPro(e);
isPress = false;
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isPress = true;
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
#14
偶尔也会出现 选多个的情况啊
#15
根据打印信息,觉得好像鼠标监听有些问题
你选择的是:1行2列 值为:true
你选择的是:1行2列 值为:true
你选择的是:0行2列 值为:true
你选择的是:0行2列 值为:true
你选择的是:1行2列 值为:true
你选择的是:1行2列 值为:true
------------------------------此处其实点击0行2列,但是没有打印信息
你选择的是:1行2列 值为:false
你选择的是:1行2列 值为:false
#16
恩就是的 当没有打印信息时 会出现多选的情况
#17
小妹又来啦。

#18
又是你 给我想想办法吧 明天就要交呢
#19
不着急,慢慢等。真没人解决了我帮你解决。
#20
你又只说不做
#21
根据打印,出现多选时,只检测到了mousePressed和mouseReleased事件,没有检测到mouseClicked事件。解决问题可以从这角度出发
#22
这个问题困扰 我有些时间了 我真的没办法了 谢谢你了 帮帮我吧 找出原因
#23
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test01 extends JFrame {
private JTable table;
Object[][]data={{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE}
};
String [] headerName={"1","2","3","4","5","6"};
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public test01() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(
headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener{
boolean isPress=false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
Object target = e.getSource();
if((JTable)target!=table){
return;
}
int selectedCol;
int selectedRow;
selectedRow = table.getSelectedRow();
selectedCol = table.getSelectedColumn();
if (selectedRow < 0) {
return;
}
if (selectedCol < 0) {
return;
}
Object obj = table.getValueAt(selectedRow, selectedCol);
Boolean val = (Boolean) obj;
//System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
for(int i=0;i<table.getColumnCount();i++){
if(selectedCol==i){
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
}
}
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false,selectedRow ,i );
}
}
table.updateUI();
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(isPress){
doPro(e);
isPress=false;
}
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isPress=true;
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
不知道是这个意思吗
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test01 extends JFrame {
private JTable table;
Object[][]data={{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE},
{Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,Boolean.FALSE}
};
String [] headerName={"1","2","3","4","5","6"};
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public test01() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {
table.getColumnModel().getColumn(i).setHeaderValue(
headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
//
}
class MousePress implements MouseListener{
boolean isPress=false;
public void doPro(MouseEvent e) {
// TODO Auto-generated method stub
Object target = e.getSource();
if((JTable)target!=table){
return;
}
int selectedCol;
int selectedRow;
selectedRow = table.getSelectedRow();
selectedCol = table.getSelectedColumn();
if (selectedRow < 0) {
return;
}
if (selectedCol < 0) {
return;
}
Object obj = table.getValueAt(selectedRow, selectedCol);
Boolean val = (Boolean) obj;
//System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
for(int i=0;i<table.getColumnCount();i++){
if(selectedCol==i){
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol+ "列 " +"值为:"+ val.toString());
}
}
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false,selectedRow ,i );
}
}
table.updateUI();
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(isPress){
doPro(e);
isPress=false;
}
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isPress=true;
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
// TODO Auto-generated method stub
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
不知道是这个意思吗
#24
有个小建议....
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString());
可以给selectedRow和selectedCol都....行和列都从0开始,感觉不舒服。
其他的,完全不懂,我是菜鸟级新手,或者是新手级菜鸟。
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " +"值为:"+ val.toString());
可以给selectedRow和selectedCol都....行和列都从0开始,感觉不舒服。
其他的,完全不懂,我是菜鸟级新手,或者是新手级菜鸟。
#25
为什么写了selectedRow和selectedCol都+1里面的+1不见了?迷茫
#26
问题不在这呀
#27
每行每列至多选中一个:
在方法 mousePressed(MouseEvent e)添加
是要这种效果吗?
在方法 mousePressed(MouseEvent e)添加
JTable tempTable = ((JTable) e.getSource());
int i = tempTable.getSelectedRow();
int j = tempTable.getSelectedColumn();
int rowCount = tempTable.getRowCount();
int columnCount = tempTable.getColumnCount();
Boolean val = (Boolean) tempTable.getValueAt(i, j);
for (int row = 0; row < i; row++)
tempTable.setValueAt(false, row, j);
for (int row = i + 1; row < rowCount; row++)
tempTable.setValueAt(false, row, j);
for (int col = 0; col < j; col++)
tempTable.setValueAt(false, i, col);
for (int col = j + 1; col < columnCount; col++)
tempTable.setValueAt(false, i, col);
System.out.println("你选择的是:" + i + "行" + j + "列 " + "值为:"
+ val.toString());
是要这种效果吗?
import java.awt.*; //写程序不注释,看得脑袋疼
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class test01 extends JFrame {
private JTable table;
Boolean[][] data = new Boolean[6][6];
String[] headerName = { "1", "2", "3", "4", "5", "6" };
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {//匿名内部类
public void run() {
try {
test01 frame = new test01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public test01() {
super();
for(int i=0; i<data.length ; i++) {//对data中的数据初始化
for(int j=0; j<data[i].length ; j++) {
data[i][j] = false;
}
}
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(new myTableModel());
for (int i = 0; i < 6; i++) {//表格每列的名字
table.getColumnModel().getColumn(i).setHeaderValue(headerName[i]);
}
scrollPane.setViewportView(table);
table.addMouseListener(new MousePress());
}
class MousePress implements MouseListener {
//这句有什么用 ? ? boolean isPress = false;
/*
public void doPro(MouseEvent e) {//这里的名字为什么这么取?Pro是Process还是?
JTable target = (JTable) e.getSource();
int selectedRow = table.getSelectedRow();
int selectedCol = table.getSelectedColumn();
Boolean val = (Boolean) table.getValueAt(selectedRow, selectedCol);
System.out.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " + "值为:" + val.toString());
for (int i = 0; i < table.getColumnCount(); i++) {//你不是取出行号和列号了,你循环遍历它,又要干嘛,
if (selectedCol == i) {
System.out
.println("你选择的是:" + selectedRow + "行" + selectedCol
+ "列 " + "值为:" + val.toString() + " ");// error 少了一个"
}
}
//下面写法功能是什么????????????????????????????????????
if (val.booleanValue()) {
for (int i = 0; i < table.getModel().getRowCount(); i++) {
if ((i != selectedRow) && (i != -1))
table.setValueAt(false, i, selectedCol);
}
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
if ((i != selectedCol) && (i != -1))
table.setValueAt(false, selectedRow, i);
}
}
table.updateUI();
}
*/
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
JTable tempTable = ((JTable) e.getSource());
int i = tempTable.getSelectedRow();
int j = tempTable.getSelectedColumn();
int rowCount = tempTable.getRowCount();
int columnCount = tempTable.getColumnCount();
Boolean val = (Boolean) tempTable.getValueAt(i, j);
for (int row = 0; row < i; row++)
tempTable.setValueAt(false, row, j);
for (int row = i + 1; row < rowCount; row++)
tempTable.setValueAt(false, row, j);
for (int col = 0; col < j; col++)
tempTable.setValueAt(false, i, col);
for (int col = j + 1; col < columnCount; col++)
tempTable.setValueAt(false, i, col);
System.out.println("你选择的是:" + i + "行" + j + "列 " + "值为:"
+ val.toString());
//取消你的算法哦,doPro(e);//添加方法
}
public void mouseReleased(MouseEvent e) {
}
}
class myTableModel extends AbstractTableModel {
public int getColumnCount() {
return 6;
}
public Class getColumnClass(int c) {
return data[0][c].getClass();
}
public int getRowCount() {
return data.length;
}
public void setValueAt(Object obj, int r, int c) {
data[r][c] = (Boolean)obj;
this.fireTableCellUpdated(r, c);
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int r, int c) {
return true;
}
}
}
#28
小妞 ,我点击了上百次了,还未出现同一行有多个或同一列有多个的情况
#29
一群大哥哥给一个小妹妹解决问题,我就不凑这个热闹了。哈哈
#30
这是可以的........不错..不错.......小伙子有前途...
#31
小妹 我帮你
#32
我是来围观人妖的
#33
谢谢你啦 我试试
#34
不要用嘴 用实际行动吧!
#35
呵呵 问题终于解决了 谢谢你啦 看来你才是Swing 这方面的高手呀 小妹真不知道 该怎么谢你啦
#36
好人太多,这小妹的魅力太大。。。。这么长的代码。。。。。
#37
我也相信 ****上 全是好心人 呵呵
#38
但是还有点问题 就是打印出来的值 当你选中的时候 打印出来的全是 false 这个问题你发现了没? 呵呵
#39
小妹 你就 走IT 这趟浑水了 回头是岸
#40
System.out.println("你选择的是:" + i + "行" + j + "列 " + "值为:"
+ "true");
这一行写死不就完了么..
+ "true");
这一行写死不就完了么..
#41
现在看到代码就头晕.....哎.....
#42
巾帼不让须眉,嘎嘎
#43
采取的原则是已被选中小格 对应的值是true,没被选中小格 对应的值是false
因为空白格对应值是false,所以显示false,
如果获得true 你就连续两次点击小格 就会显示true了
#44
不过你的确没说清楚功能,除了要保证每行每列只有唯一被选中的功能外,你还需要效果呢?
#45
那怎样直接就让他 显示为 true呢 那说明你取到的 val值就有问题呀
#46
那个对应值 肯定要正确呀 呵呵 估计这对你来说 很容易吧
#47
送佛 送到西吧 呵呵 谢谢你啦
#48
把
[
System.out.println("你选择的是:" + i + "行" + j + "列 " + "值为:"
+ val.toString());
/code]
改为
[code=Java]
System.out.println("你选择的是:" + i + "行" + j + "列 " + "值为:"
+ !val);
就会出现很多很多true了
#49
这个不解决 根本问题呀 呵呵 我刚才也写了个
if(val){
val=true;
}else{
val=false;
}
呵呵 你再看看吧 把你写的那 放在click里就按照选择的直接打印 true 但是会有重复的
#50
上面那个写反了 if(val){
val=false;
}else{
val=true;
}
val=false;
}else{
val=true;
}