我一直试图在ActionListener中从上面的类中访问一些变量,但是我一直都在失败:(我做错了什么?我希望你能帮助我的人。
public class FileFrameBetterStructured extends JFrame {
protected FileModel fileModel = new FileModel();
{
// Set Preferences
setSize(500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create table
FileModel fileModel = new FileModel();
JTable FileTable = new JTable(fileModel);
TableRowSorter<TableModel> TableRowSorter = new TableRowSorter<TableModel>(fileModel);
FileTable.setRowSorter(TableRowSorter);
FileTable.setColumnSelectionAllowed(true);
FileTable.setDefaultRenderer(Number.class, new BigRenderer(1000));
JScrollPane JScrollPane = new JScrollPane(FileTable);
getContentPane().add(JScrollPane, BorderLayout.CENTER);
// Create textfilter
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Filter");
panel.add(label, BorderLayout.WEST);
final JTextField filterText = new JTextField("");
panel.add(filterText, BorderLayout.CENTER);
add(panel, BorderLayout.NORTH);
JButton button = new JButton("Filter");
add(button, BorderLayout.SOUTH);
setSize(300, 250);
setVisible(true);
}
public static void main(String args[]) {
final FileFrameBetterStructured FileFrame = new FileFrameBetterStructured();
// Integrate ActionListener for textfilter
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = filterText.getText();
if (text.length() == 0) {
TableRowSorter.setRowFilter(null);
} else {
TableRowSorter.setRowFilter(RowFilter.regexFilter(text));
}
}
});
}
}
在ActionListener中,我想访问变量:按钮, filterText 和 TableRowSorter 。谢谢!
答案 0 :(得分:0)
将其添加到班级的顶部:
protected static JButton button;
protected static JTextField filterText;
protected static TableRowSorter<TableModel> TableRowSorter;
答案 1 :(得分:-1)
按以下方式更改您的代码
public class FileFrameBetterStructured extends JFrame {
static JButton button;
static JTextField filterText;
staitc TableRowSorter<TableModel> tableRowSorter;
protected FileModel fileModel = new FileModel();
FileFrameBetterStructured()
{
// Set Preferences
setSize(500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create table
FileModel fileModel = new FileModel();
JTable FileTable = new JTable(fileModel);
tableRowSorter = new TableRowSorter<TableModel>(fileModel);
FileTable.setRowSorter(TableRowSorter);
FileTable.setColumnSelectionAllowed(true);
FileTable.setDefaultRenderer(Number.class, new BigRenderer(1000));
JScrollPane JScrollPane = new JScrollPane(FileTable);
getContentPane().add(JScrollPane, BorderLayout.CENTER);
// Create textfilter
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Filter");
panel.add(label, BorderLayout.WEST);
filterText = new JTextField("");
panel.add(filterText, BorderLayout.CENTER);
add(panel, BorderLayout.NORTH);
button = new JButton("Filter");
add(button, BorderLayout.SOUTH);
setSize(300, 250);
setVisible(true);
}
public static void main(String args[]) {
final FileFrameBetterStructured FileFrame = new FileFrameBetterStructured();
// Integrate ActionListener for textfilter
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = filterText.getText();
if (text.length() == 0) {
TableRowSorter.setRowFilter(null);
} else {
TableRowSorter.setRowFilter(RowFilter.regexFilter(text));
}
}
});
}
}
希望它有所帮助。