0
這個例子的代碼是我的實際程序的一個短版:如何跟蹤我的內部框架?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
public class Example extends JFrame {
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Example frame = new Example();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Example() {
//what to do @ close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 792, 585);
//content pane
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//create desktop pane add it to content pane
final JDesktopPane desktopPane = new JDesktopPane();
contentPane.add(desktopPane, BorderLayout.CENTER);
//create Int Frame with table
JInternalFrame tableIntFrame = new JInternalFrame("TableIntFrame");
tableIntFrame.setBounds(31, 29, 300, 205);
desktopPane.add(tableIntFrame);
//create the table
table = new JTable();
table.setFillsViewportHeight(true);
table.setModel(new DefaultTableModel(
new Object[][] {
{"Row 0 (click for more info)"},
{"Row 1 (click for more info)"},
},
new String[] {
"Collumn 0"
}
));
//add the table to a ScrollPane
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(table);
//add Scrollpane to table
tableIntFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);
tableIntFrame.setVisible(true);
//Listen for events on selection
table.getSelectionModel().addListSelectionListener(new
ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// only fires ones
if (! e.getValueIsAdjusting())
{
//create info frame with title set to selectedrow index
createFrame(desktopPane, table.getSelectedRow()+"");
}
}
});
// Allow only one row to be selected.
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
//Creates an int frame with the title set to the row
private void createFrame(JDesktopPane desktopPane, String selectedRow){
JInternalFrame InfoIntFrame = new JInternalFrame("Info Row "+selectedRow);
InfoIntFrame.setBounds(425, 44, 183, 72);
//add to desktop
desktopPane.add(InfoIntFrame);
//set visible
InfoIntFrame.setVisible(true);
}
}
如何獲得它爲這項工作,當點擊它的相應infoIntFrame已經打開不會創建infoIntframe的另一個實例一排? (注:infoIntFrames必須在運行時創建)
因爲您檢查並確保它不爲空,所以您不會在第一個FrameListIntFrame之後創建一個新的FrameListIntFrame。看起來你只是進入if語句一次。 – Dodd10x 2012-02-09 22:22:15
它可能是很好的問題,請與http://sscce.org/ – mKorbel 2012-02-09 22:37:19
這【答案】(http://stackoverflow.com/a/2741726/230513)編輯您的帖子引用了使用'Action'一個實例跟蹤多個內部幀。 – trashgod 2012-02-09 23:01:39