我想将StateChanged方法中选定的选项卡索引值推送/调用到另一个类或私有方法,我该怎么做呢,
private class TabSelect implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JTabbedPane source = (JTabbedPane) e.getSource();
if (source.getSelectedComponent() != null) {
source.getSelectedIndex();
}
}
}
我想将此索引值推送到以下方法(或同一包中的另一个公共类)。怎么做?
private JPanel CreateSlice() {
JPanel Slice = new JPanel();
Slice.setPreferredSize(new Dimension(550, 600));
Slice.add(button);
return Slice;
}
这是CreateSlice的功能
private class TabPlus implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JPanel panel = CreateSlice();
String title = "Slice " + String.valueOf(pane.getTabCount());
pane.insertTab(title, null, panel, null, pane.getTabCount() - 1);
}
}
答案 0 :(得分:0)
@Override
public void stateChanged(ChangeEvent e) {
JTabbedPane source = (JTabbedPane) e.getSource();
if (source.getSelectedComponent() != null) {
int index = source.getSelectedIndex();
CreateSlice(index);
}
}
public JPanel CreateSlice(int index) {
//do whatever you want with index
JPanel Slice = new JPanel();
Slice.setPreferredSize(new Dimension(550, 600));
Slice.add(button);
return Slice;
}
请注意,您的CreateSlice方法应该是公开的
答案 1 :(得分:0)
int index = source.getSelectedIndex(); // Save it to variable
createSlice(index); // pass it into new method, follow camelCase for bestPractice
private JPanel createSlice(int index) {
//your implementation
}