java,gui,有两个菜单栏,分别是browse products和 check details。在browse products菜单下,有一个表格,可以实现筛选功能,单击表格中的某一行,显示更多细节,同时菜单栏变为check details。
时间: 2023-06-27 08:05:58 浏览: 93
好的,这个需求可以通过Java Swing实现。首先,我们需要创建一个包含两个菜单栏的主窗口。
```java
import javax.swing.*;
public class MainFrame extends JFrame {
private JMenuBar menuBar;
private JMenu browseMenu, checkMenu;
private JTable productsTable;
public MainFrame() {
setTitle("Product Management System");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建菜单栏
menuBar = new JMenuBar();
browseMenu = new JMenu("Browse Products");
checkMenu = new JMenu("Check Details");
menuBar.add(browseMenu);
menuBar.add(checkMenu);
setJMenuBar(menuBar);
// 创建表格
String[] columnNames = {"Product Name", "Price", "Stock"};
Object[][] data = {{"Product A", 10.0, 100}, {"Product B", 20.0, 200}, {"Product C", 30.0, 300}};
productsTable = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(productsTable);
add(scrollPane);
setVisible(true);
}
}
```
然后,我们需要实现在browseMenu下的表格中添加筛选功能和单击行显示更多细节的功能。我们可以使用TableRowSorter类来实现表格的筛选功能,使用ListSelectionListener监听表格的行选中事件。
```java
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
public class MainFrame extends JFrame {
private JMenuBar menuBar;
private JMenu browseMenu, checkMenu;
private JTable productsTable;
public MainFrame() {
setTitle("Product Management System");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建菜单栏
menuBar = new JMenuBar();
browseMenu = new JMenu("Browse Products");
browseMenu.addActionListener(e -> {
// 切换到Browse Products菜单时,显示表格并添加筛选功能
setContentPane(new JScrollPane(productsTable));
TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<>((DefaultTableModel) productsTable.getModel());
productsTable.setRowSorter(sorter);
JTextField filterField = new JTextField();
filterField.addActionListener(e1 -> {
String text = filterField.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter(text));
}
});
add(filterField, BorderLayout.NORTH);
revalidate();
});
checkMenu = new JMenu("Check Details");
menuBar.add(browseMenu);
menuBar.add(checkMenu);
setJMenuBar(menuBar);
// 创建表格
String[] columnNames = {"Product Name", "Price", "Stock"};
Object[][] data = {{"Product A", 10.0, 100}, {"Product B", 20.0, 200}, {"Product C", 30.0, 300}};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
productsTable = new JTable(model);
productsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
// 单击表格中的一行时,切换到Check Details菜单并显示该行的细节
int row = productsTable.getSelectedRow();
String productName = (String) productsTable.getValueAt(row, 0);
double price = (double) productsTable.getValueAt(row, 1);
int stock = (int) productsTable.getValueAt(row, 2);
setContentPane(new JLabel("Product Name: " + productName + " Price: " + price + " Stock: " + stock));
revalidate();
menuBar.remove(browseMenu);
menuBar.add(checkMenu);
}
}
});
JScrollPane scrollPane = new JScrollPane(productsTable);
add(scrollPane);
setVisible(true);
}
}
```
这样,我们就实现了一个简单的Java GUI程序,可以在两个菜单栏之间切换,并实现了表格的筛选和单击行显示更多细节的功能。
阅读全文