📄 listselectiondialog.java
字号:
// Set icons setIconImages( owner.getIconImages( ) ); //---- List panel GridBagLayout gridBag = new GridBagLayout( ); GridBagConstraints gbc = new GridBagConstraints( ); JPanel listPanel = new JPanel( gridBag ); GuiUtilities.setPaddedLineBorder( listPanel ); int gridY = 0; // Label: list JLabel listLabel = new FLabel( listLabelStr ); gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.FIRST_LINE_START; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 0, 0, 0, 0 ); gridBag.setConstraints( listLabel, gbc ); listPanel.add( listLabel ); // List list = new JList( listStrs ); AppFont.MAIN.setFont( list ); int cellHeight = getFontMetrics( list.getFont( ) ).getHeight( ) + 2; list.setFixedCellHeight( cellHeight ); list.setVisibleRowCount( listStrs.length ); list.setCellRenderer( new SelectionListCellRenderer( ) ); list.addListSelectionListener( this ); // Scroll pane: list JScrollPane listScrollPane = new JScrollPane( list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); int width = LIST_NUM_COLUMNS * GuiUtilities.getCharWidth( '0', getFontMetrics( list.getFont( ) ) ); int height = listStrs.length * cellHeight; listScrollPane.getViewport( ).setPreferredSize( new Dimension( width, height ) ); gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.FIRST_LINE_START; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 4, 0, 0, 0 ); gridBag.setConstraints( listScrollPane, gbc ); listPanel.add( listScrollPane ); //---- Button panel: select all, deselect all JPanel selectionButtonPanel = new JPanel( new GridLayout( 1, 0, 8, 0 ) ); // Button: select all selectAllButton = new FButton( SELECT_ALL_STR ); selectAllButton.setToolTipText( SELECT_ALL_TOOL_TIP_STR ); selectAllButton.setActionCommand( Command.SELECT_ALL ); selectAllButton.addActionListener( this ); selectionButtonPanel.add( selectAllButton ); // Button: deselect all deselectAllButton = new FButton( DESELECT_ALL_STR ); deselectAllButton.setToolTipText( DESELECT_ALL_TOOL_TIP_STR ); deselectAllButton.setActionCommand( Command.DESELECT_ALL ); deselectAllButton.addActionListener( this ); selectionButtonPanel.add( deselectAllButton ); //---- Button panel: OK, cancel JPanel okCancelButtonPanel = new JPanel( new GridLayout( 1, 0, 8, 0 ) ); // Button: OK okButton = new FButton( AppConstants.OK_STR ); okButton.setActionCommand( Command.ACCEPT ); okButton.addActionListener( this ); okCancelButtonPanel.add( okButton ); // Button: cancel JButton cancelButton = new FButton( AppConstants.CANCEL_STR ); cancelButton.setActionCommand( Command.CLOSE ); cancelButton.addActionListener( this ); okCancelButtonPanel.add( cancelButton ); //---- Button panel JPanel buttonPanel = new JPanel( gridBag ); buttonPanel.setBorder( BorderFactory.createEmptyBorder( 2, 24, 3, 24 ) ); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.5; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 0, 0, 0, 12 ); gridBag.setConstraints( selectionButtonPanel, gbc ); buttonPanel.add( selectionButtonPanel ); gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.5; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 0, 12, 0, 0 ); gridBag.setConstraints( okCancelButtonPanel, gbc ); buttonPanel.add( okCancelButtonPanel ); //---- Main panel JPanel mainPanel = new JPanel( gridBag ); mainPanel.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) ); gridY = 0; gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets( 0, 0, 0, 0 ); gridBag.setConstraints( listPanel, gbc ); mainPanel.add( listPanel ); gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets( 4, 0, 0, 0 ); gridBag.setConstraints( buttonPanel, gbc ); mainPanel.add( buttonPanel ); //---- Window // Set content pane setContentPane( mainPanel ); // Update components updateComponents( ); // Set orientation of components according to locale App.applyOrientationByLocale( this ); // Add key commands to action map KeyAction.create( mainPanel, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, KEY_COMMANDS, this ); // Dispose of window explicitly setDefaultCloseOperation( DO_NOTHING_ON_CLOSE ); // Handle window closing addWindowListener( new WindowEventHandler( ) ); // Prevent dialog from being resized setResizable( false ); // Resize dialog to its preferred size pack( ); // Set location of dialog box if ( location == null ) location = GuiUtilities.getComponentLocation( this, owner ); setLocation( GuiUtilities.getComponentLocation( this, location ) ); // Set default button getRootPane( ).setDefaultButton( okButton ); // Show dialog setVisible( true ); } //------------------------------------------------------------------ private void updateComponents( ) { boolean enabled = (list.getSelectedIndices( ).length < list.getModel( ).getSize( )); selectAllButton.setEnabled( enabled ); enabled = !list.isSelectionEmpty( ); deselectAllButton.setEnabled( enabled ); okButton.setEnabled( enabled ); Util.moveFocus( this ); } //------------------------------------------------------------------ private void doSelectAll( ) { list.setSelectionInterval( 0, list.getModel( ).getSize( ) - 1 ); } //------------------------------------------------------------------ private void doDeselectAll( ) { list.clearSelection( ); } //------------------------------------------------------------------ private void doAccept( ) { accepted = true; doClose( ); } //------------------------------------------------------------------ private void doClose( ) { location = getLocation( ); setVisible( false ); dispose( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class variables//////////////////////////////////////////////////////////////////////// private static Point location;////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private boolean accepted; private JList list; private JButton selectAllButton; private JButton deselectAllButton; private JButton okButton;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -