我有一个类,它读取 excel 文件并在 jpanel 内创建带有图形的框架。我通过 jmenuitem 中的 Action 监听器调用此类。然后我有另一个 jmenuitem 调用打开相同文件的同一个类,但读取不同的 Excel 工作表,并给出不同的图形(这是类中唯一更改的字符串)。具有这些 jmenitem 的 jmenubar 属于程序启动的 jframe。我想知道是否可以每次单击创建要添加到新 jframe 卡片布局中的图形的 jmenuitems 时,以便我可以在它们上滚动。提前致谢
这是我当前用来在单击 jmenuitem 时在 jframe 中打开图形的代码:
public class startup extends JFrame { // creates a jframe with some stuff and the jmenubar
public void menu() {
...
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event2) {
new Thread(new Runnable() {
@Override
public void run() {
new ReadExcel();
ReadExcel.excel(".xls", 0); // this jmenuitem invokes the class to read the excelfile sheet 0
graphgen.main(null);
}
}).start();
}
});
subsubmenu1.add(menuItem);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event2) {
new Thread(new Runnable() {
@Override
public void run() {
new ReadExcel();
ReadExcel.excel(".xls", 1); // this jmenuitem invokes the class to read the excelfile sheet 1
graphgen.main(null);
}
}).start();
}
});
subsubmenu1.add(menuItem);
....
}
public static void main(String[] args)
{
GUIquery frame = new GUIquery();
p.add(graphComponent, BorderLayout.CENTER);
frame.setLayout(new BorderLayout());
frame.add(p, BorderLayout.CENTER);
frame.setJMenuBar(GUIquery.createMenuBar());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setSize(1600, 1200);
frame.setVisible(true);
}
}
readexcel 类只是读取 excelfile 的 excelsheet 并返回一些在 graphgen 类中处理的数组列表。
public class graphgen extends JFrame {
public graphgen() {
super("Results");
gen();
}
public void gen(){
//creates the graphcomponent
getContentPane().add(graphComponent);
add(graphComponent);
}
public static void main(String[] args)
{
graphgen frame = new graphgen();
p2.add(graphComponent, BorderLayout.CENTER);
frame.add(p2, BorderLayout.CENTER);
frame.pack();
frame.setResizable(true);
frame.setSize(1600, 1200);
frame.setVisible(true);
}
最佳答案
使用Action
封装目标组件、文件和sheet,如图here和 here 。添加一个方法来根据所选工作表更新类的状态。在卡片之间导航的示例可见 here和 here 。另请参阅卡片布局操作,引用 here .
关于java - 将从同一类创建的 jpanels 添加到卡片布局框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19184670/