gpt4 book ai didi

java - 比较二维数组与 JTextField

转载 作者:行者123 更新时间:2023-12-01 21:58:47 25 4
gpt4 key购买 nike

我尝试了 3 天来解决这个问题,这很困难,我找不到解决方法。我尝试将另一个类的二维数组与 ActionListener 类与用户在搜索框中输入的 JtextField 进行比较,然后在 JTable 中查看结果。任何人都可以提供帮助,我对此表示感谢。

    the class how contain the array 

public class SerachClassic extends JFrame{

String[]columnNames2 = { "Car Name", "Color", "Miles", "Year Model"," Prise"};

String [][]info2 = {{"Ford","Black","1000","1923","34000$"},
{"Ford Mustang","red","1300","1969","58500$"},
{"Dodge","Sliver","6000","1972","38000$"},
{"Chevrolet Corvette","red & Whiat","500","1960","89000$"},
{"Buick Electra","blu","2000","1969","24000$"},
{"Pontiac Bonneville","blu","700","1963","26900$"},
{"Lincoln Continental","Black","1500","1964","79000$"},
{"Cadillac Fleetwood","Black","1200","1959","90000$"},
{"Ford Thunderbird","Sliver","500","1957","75000$"}};




public String[] getColumnNames2() {
return columnNames2;
}

public String[][] getInfo2() {
return info2;
}

}

这是创建搜索框的类

static class Action1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
SwingUtilities.invokeLater(new Runnable() { //using this method to update UI

@Override
public void run() {
TextFile textFile = new TextFile();// create a new object to write somthing
}
});
}
}

public static class TextFile{

JFrame ftext = new JFrame();
JPanel panel = new JPanel();
JTextField text = new JTextField("",30);
JButton button = new JButton("Serach");


public TextFile(){


text.addFocusListener(new FocusListener(){
@Override


public void focusGained(FocusEvent arg0){
text.setText("");
}
@Override

public void focusLost(FocusEvent arg0){
text.setText("Please enter something");

}

});

panel.add(text);
panel.add(button);

ftext.add(panel);
ftext.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
ftext.pack();
ftext.setVisible(true);

}

}

我在这里尝试通过将搜索框中的 JTextfile 与字符串进行比较来执行某些操作,但它不起作用

static class Action8 implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
SerachClassic findt = new SerachClassic();
TextFile find1 = new TextFile();

String fromUser = find1.text.getText();

if(Arrays.asList(findt.info2).contains(fromUser)){

JFrame searchType = new JFrame("Car Type");
JPanel panel = new JPanel();
searchType.setVisible(true);
searchType.setSize(400, 200);
JTable table = new JTable();

panel.add(table);
searchType.add(panel);

最佳答案

由于我不完全明白你到底想要实现什么,我只能猜测。我现在将列出我在代码中更改的重要内容:

  1. 修复了 focusGained() & focusedLost()方法。例如。如果文本字段内没有文本,它只会将其更改为“请输入内容”。
  2. Arrays.asList()在您的情况下仍会返回“二维数组”(例如 List<List<String>> )。因此,我创建了一个自己的方法,将 2D 数组转换为 1D 列表(称为 to1DList() )
  3. 避免 the use of mutliple JFrame s ,使用 JDialog 改为 s。
  4. 我添加了 ActionListenerbutton带有文字“搜索”
  5. 一个new JTable()不会神奇地显示您的 info2 。您必须使用此构造函数:JTable(Object[][] rowData, Object[] columnNames) .
  6. 我认为您希望突出显示搜索项所在的行,因此我实现了方法 findRow() 。那么你就可以做table.setRowSelectionInterval(findRow(), findRow()); .
  7. 我修正了一些拼写错误,并使变量名称更加清晰。

输出:

enter image description here

代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SearchClassic {

private String[] columnNames2 = { "Car Name", "Color", "Miles", "Year Model", "Price" };

private String[][] info2 = { { "Ford", "Black", "1000", "1923", "34000$" },
{ "Ford Mustang", "Red", "1300", "1969", "58500$" }, { "Dodge", "Silver", "6000", "1972", "38000$" },
{ "Chevrolet Corvette", "Red & White", "500", "1960", "89000$" },
{ "Buick Electra", "Blue", "2000", "1969", "24000$" },
{ "Pontiac Bonneville", "Blue", "700", "1963", "26900$" },
{ "Lincoln Continental", "Black", "1500", "1964", "79000$" },
{ "Cadillac Fleetwood", "Black", "1200", "1959", "90000$" },
{ "Ford Thunderbird", "Silver", "500", "1957", "75000$" } };

public String[] getColumnNames2() {
return columnNames2;
}

public String[][] getInfo2() {
return info2;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TextFile textFile = new TextFile();
}
});
}
}

class TextFile {

JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextField textField = new JTextField("", 30);
JButton button = new JButton("Search");

public TextFile() {

textField.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent arg0) {
if (textField.getText().equals("Please enter something")) {
textField.setText("");
}
}

@Override
public void focusLost(FocusEvent arg0) {
if (textField.getText().equals("")) {
textField.setText("Please enter something");
}
}

});

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SearchClassic searchClass = new SearchClassic();
String input = textField.getText();
if (to1DList(searchClass.getInfo2()).contains(input)) {
JTable table = new JTable(searchClass.getInfo2(), searchClass.getColumnNames2());
table.setRowSelectionInterval(findRow(searchClass.getInfo2(), input),
findRow(searchClass.getInfo2(), input));
JPanel tablePanel = new JPanel(new BorderLayout());
tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
tablePanel.add(table);
JOptionPane.showMessageDialog(frame, tablePanel);
}
}
});

panel.add(textField);
panel.add(button);

frame.add(panel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}

private ArrayList<Object> to1DList(Object[][] array) {
ArrayList<Object> newArray = new ArrayList<Object>();
for (int row = 0; row < array.length; row++) {
for (int column = 0; column < array[row].length; column++) {
newArray.add(array[row][column]);
}
}
return newArray;
}

private Integer findRow(Object[][] array, Object target) {
for (int row = 0; row < array.length; row++) {
for (int column = 0; column < array[row].length; column++) {
if (array[row][column].equals(target)) {
return row;
}
}
}
return null;
}

}

关于java - 比较二维数组与 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33970794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com