随机数序列查找重复值,已经可以实现数组找出重复值,各项功能都能实现,但是怎么标记为红色呐
package FileRandomJFrame;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.io.*;
public class FileRandomJFrame extends JFrame implements ActionListener
{
protected JPanel cmdpanel;
protected JTextField text_filename,text_count;
protected DefaultTableModel tablemodel;
int number[]=new int[1000];
int temp;
public FileRandomJFrame(String filename)
{
super("随机数序列");
this.setBounds(300,300,360,200);
this.setBackground(java.awt.Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.cmdpanel=new JPanel();
this.getContentPane().add(this.cmdpanel,"North");
this.cmdpanel.add(new JLabel("随机数个数"));
this.cmdpanel.add(this.text_count =new JTextField(10+"",5));
this.text_count.addActionListener(this);
String[] bstr = {"生成","查找重复值"};
for(int i=0; i < bstr.length;i++)
{
JButton button = new JButton(bstr[i]);
this.cmdpanel.add(button);
button.addActionListener(this);
}
// this.cmdpanel.add(new JLabel("文件名"),3);
String[] title = {"1","2","3","4","5","6","7","8","9","10"};
this.cmdpanel.add(this.text_filename =new JTextField(filename,10) ,4);
this.tablemodel =new DefaultTableModel(title,0);//表格模型
JTable jtable =new JTable(this.tablemodel);
this.getContentPane().add(new JScrollPane(jtable));
int number[]=new int[Integer.parseInt(text_count.getText())];
this.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand().equals("生成") || event.getSource() == this.text_count)
random(this.tablemodel,this.text_count);
else
{
switch(event.getActionCommand())
{
// case"打开":readFrom(this.text_filename.getText(),this.tablemodel);break;
//case"保存":writeTo(this.text_filename.getText(),this.tablemodel);
// case "查找重复值":
}
}
}
protected void random(DefaultTableModel tablemodel,JTextField text)
{
int a=0;
try {
int n =Integer.parseInt(text.getText()),i = 0;
int columns =tablemodel.getColumnCount();
int rows =(n%columns == 0) ? n/columns :n/columns+1;
tablemodel.setRowCount(rows);
for(i=0;i< n;i++)
{
number[a]=(int) (Math.random() * 100);
tablemodel.setValueAt( number[a], i / columns, i % columns);
a++;
}
for(i=n;i/columns<rows && i%columns<columns;i++)
tablemodel.setValueAt(null,i/columns,i%columns );
}
catch(NumberFormatException ex)
{
JOptionPane.showMessageDialog(this, "\""+text.getText()+ "\"不能转化为整数");
}
}
public void search()
{
int n =Integer.parseInt(text_count.getText());
int columns =tablemodel.getColumnCount();
int rows =(n%columns == 0) ? n/columns :n/columns+1;
for (int i = 0; i < Integer.parseInt(text_count.getText()); i++)
for (int j = 0; j < (Integer.parseInt(this.text_count.getText() )- 1); j++)
if ( number[j] > number[j + 1])
{
temp = number[j];
number[j] = number[j + 1];
number[j + 1] = temp;
}
for(int i = 0; i < Integer.parseInt(text_count.getText()); i++)
if(number[i+1]==number[i])
{
// tablemodel.setValueAt((int) (number[(i / columns)*(i % columns)]), i / columns, i % columns);
}
}
protected void writeTo(String filename,DefaultTableModel tablemodel)
{
try
{
OutputStream out = new FileOutputStream(filename);
DataOutputStream dataout = new DataOutputStream(out);
int n = 0;
for(int i=0; i< tablemodel.getRowCount();i++)
{
for(int j=0;j < tablemodel.getColumnCount();j++)
{
Object obj =tablemodel.getValueAt(i,j);
if(obj !=null && obj instanceof Integer)
{
dataout.writeInt(((Integer)obj).intValue());
n++;
}
else if(obj != null && obj instanceof String && !obj.equals(""))
{
try {
dataout.writeInt(Integer.parseInt((String)obj));
n++;
}
catch(NumberFormatException ex) {}
}
}
dataout.close();
out.close();
this.text_count.setText(n+"");
}
}
catch(FileNotFoundException ex)
{
JOptionPane.showMessageDialog(this, "\""+filename+"\"文件不存在。");
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(this, filename+"写入文件时错误");
}
}
public static void main (String[] arg)
{
new FileRandomJFrame("random.int");
}
}
你这是 表格吧? 是要更改 指定 单元格文字颜色?
找了个这种效果的,改的是单元格颜色,你自己再研究研究
关键代码:
public void search() {
int n = Integer.parseInt(this.text_count.getText());
int row = n / 10 + 1;
int arr[][] = new int[row][10]; // 定义一个二维数组,数组里面标1的就是要标红的
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j && this.number[i] == this.number[j]) {
int rowI = i / 10; // i 所在的行
int columnI = i % 10; // i 所在的列
int rowJ = j / 10; // i 所在的列
int columnJ = j % 10; // j 所在的列
arr[rowI][columnI] = 1; // 标记1,要标红
arr[rowJ][columnJ] = 1; // 标记1,要标红
}
}
}
//新建列表现器------------------------//
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
if (arr[row][column] == 1 && cell.isBackgroundSet())//设置变色的单元格
cell.setBackground(Color.RED);
else
cell.setBackground(Color.WHITE);
return cell;
}
};
int columns = tablemodel.getColumnCount();
for (int m = 0; m < columns; m++) {
jtable.getColumn(title[m]).setCellRenderer(tcr);
}
}
不明白你具体的意思,试试单独用一个队列存放这些重复值呢
标记为红色是啥意思,可以说的详细些