1

我想创建一个 jcombobox,在按下选项卡时将插入符号位置设置为 0 位置。即,当您的面板中有不同的组件并且当您按 Tab 时,它会选择 jcombobox,并且由于它是可编辑的 jcombobox,如果我在任何插入符号位置单击 jcombobox 的文本字段,当我按下标签时,它将停留在该插入符号位置并切换到其他组件,最后到达组合框,插入符号的位置保持不变。当我使用选项卡按钮时,我想将组合框文本字段中插入符号的位置设置为 0 位置。

4

1 回答 1

1

您可以将插入符号设置为0获得焦点的位置。但这也会在鼠标单击时设置插入符号。如果没问题?看:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.text.JTextComponent;

public class BorderPanel extends JFrame{

    public BorderPanel() {

        JButton btn0 = new JButton( "Hilfe" );
        JButton btn1 = new JButton( "Apply" );
        JButton btn2 = new JButton( "Cancel" );
        JButton btn3 = new JButton( "OK" );

        JPanel sub1 = new JPanel(new FlowLayout( FlowLayout.TRAILING, 5, 5 ));
        JPanel sub2 = new JPanel(new FlowLayout( FlowLayout.TRAILING, 5, 5 ));

        JMenuBar mnu = new JMenuBar();
        mnu.add( new JMenu( "File" ) );
        mnu.add( new JMenu( "Insert" ) );
        mnu.add( new JMenu( "Edit" ) );
        mnu.add( new JMenu( "Help" ) );

        JComboBox<String> cmb = new JComboBox<String>();
        cmb.getEditor().getEditorComponent().addFocusListener( new FocusAdapter() {
            @Override
            public void focusGained( FocusEvent e ) {
                if(e.getSource() instanceof JTextComponent) {
                    ((JTextComponent)e.getSource()).setCaretPosition( 0 );
                }
            }
        } );

        cmb.setEditable( true );
        cmb.addItem( "Lorem" );
        cmb.addItem( "Ipsum" );
        cmb.addItem( "Dolor" );
        cmb.addItem( "Sit Amet" );

        JPanel pnl = new JPanel();
        pnl.add(cmb);

        sub1.add( btn0 );
        sub2.add( btn1 );
        sub2.add( btn2 );
        sub2.add( btn3 );

        JPanel bar = new JPanel();
        bar.setLayout( new BorderLayout( 20, 20 ) );
        bar.add( sub1, BorderLayout.WEST );
        bar.add( Box.createHorizontalGlue(), BorderLayout.CENTER );
        bar.add( sub2, BorderLayout.EAST );

        setDefaultCloseOperation( EXIT_ON_CLOSE );
        getContentPane().setLayout( new BorderLayout() );
        getContentPane().add( mnu, BorderLayout.NORTH );
        getContentPane().add( pnl, BorderLayout.CENTER );
        getContentPane().add( bar, BorderLayout.SOUTH );
        setSize( new Dimension( 600, 400 ) );
        setLocationRelativeTo( null );
    }

    public static void main( String[] args ) {
        EventQueue.invokeLater( new Runnable() {
            @Override
            public void run() {
                new BorderPanel().setVisible( true );
            }
        } );
    }
}
于 2013-01-18T15:33:03.307 回答