我试图从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);
}
}
我试着通过将搜索框中的JText文件与字符串进行比较来尝试做某事,但是它不起作用
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);
答案 0 :(得分:3)
由于我不完全明白你想要达到的目标,我可以猜测。我现在将列出我在代码中改变的重要事项:
focusGained()
& focusedLost()
方法。例如。它只会改为"请输入一些内容"如果文本域内没有文本。Arrays.asList()
仍然会返回“2D数组”&#39;在你的情况下(例如List<List<String>>
)。所以我创建了一个自己的方法,将2D数组转换为1D列表(称为to1DList()
)JFrame
s,而是使用JDialog
。ActionListener
添加了button
文字&#34;搜索&#34; new JTable()
不会神奇地展示您的info2
。您必须使用此构造函数:JTable(Object[][] rowData, Object[] columnNames)
。findRow()
。然后你就可以table.setRowSelectionInterval(findRow(), findRow());
。<强>输出:强>
<强>代码:强>
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;
}
}