将面板中的项目与顶部摆动对齐

问题描述

我正在尝试将一些项目动态添加到 jpanel。我已经使用了 gridbaglayout 作为我的这个 jpanel 的经理(下面是红色背景色),我想将面板及其项目垂直对齐到顶部。知道如何让这个面板进入面板顶部吗?

enter image description here

这是我的代码:

public class WebcamFrame extends JFrame implements Runnable {
    private Webcam webcam;
    private WebcamPanel webcamPanel;
    private JLabel labelText;
    private JPanel actionsPanel;
    private boolean running;
    private HashSet<String> cardsPlayed;
    private final JPanel actionsContainer;
    private final JPanel buttonsPanel;
    private final GridBagConstraints constraints;

    public WebcamFrame( Callback killWebcam,Callback handleSubmit ) {
        running = true;
        setLayout( new FlowLayout() );
        setTitle( "Scan a card" );
        setIconImage( new ImageIcon(getClass().getResource("/tslogo.png")).getImage() );
        setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
        addWindowListener( new WindowAdapter() {
            @Override
            public void windowClosing( WindowEvent e ) {
                super.windowClosing( e );
                killWebcam.call();
            }
        } );
        cardsPlayed = new HashSet<>();
        Dimension size = WebcamResolution.QVGA.getSize();
        webcam = Webcam.getDefault();
        webcam.setViewSize( size );
        webcamPanel = new WebcamPanel( webcam );
        webcamPanel.setPreferredSize( size );
        webcamPanel.setLayout( new BorderLayout() );
        labelText = new JLabel( "",SwingConstants.CENTER );
        if( Webcam.getDefault() == null ) labelText.setText( "No Webcam detected" );
        actionsContainer = new JPanel( new BorderLayout() );
        JButton confirmButton = new JButton( "Confirm Selection" );
        confirmButton.addActionListener( e -> {
            List<CardDataModel> cardsToSubmit = new ArrayList<>();
            cardsPlayed.forEach( cardName -> {
                CardDataModel card = new CardDataModel();
                card.cardName = cardName;
                cardsToSubmit.add( card );
            } );
            handleSubmit.call( cardsToSubmit );
        } );
        JButton clearButton = new JButton( "Clear Selection" );
        buttonsPanel = new JPanel();
        buttonsPanel.add( confirmButton );
        buttonsPanel.add( clearButton );
        buttonsPanel.setVisible( false );
        
        constraints = new GridBagConstraints();
        
        clearButton.addActionListener( e -> { 
            cardsPlayed.clear();
            actionsPanel.removeAll();
            constraints.gridy = 0;
            actionsPanel.add( new JLabel( "Played Cards"),constraints );
            actionsContainer.setVisible( false );
            buttonsPanel.setVisible( false );
            labelText.setVisible( false );
            constraints.gridy = 0;
            pack();
        } );
        
        JPanel subPanel = new JPanel();
        subPanel.setLayout( new BorderLayout() );
        subPanel.add( labelText,BorderLayout.NORTH );
        subPanel.add( buttonsPanel,BorderLayout.SOUTH );
        webcamPanel.add( subPanel,BorderLayout.SOUTH );
        
        actionsPanel = new JPanel( new GridBagLayout() ); 
        constraints.gridy = 0;
        constraints.insets = new Insets( 0,5,0 );
        actionsPanel.add( new JLabel( "Played Cards"),constraints );
        actionsPanel.setBackground(Color.red);
        JScrollPane scrollPane = new JScrollPane( actionsPanel );
        actionsContainer.add( scrollPane,BorderLayout.NORTH );
        actionsContainer.setVisible( false );
        add( webcamPanel );
        add( actionsContainer );
        
        pack();
        setLocationRelativeTo( null );
        setVisible( false );
        setResizable( false );
    }

    @Override
    public void run() {
        do {
            try {
                Thread.sleep( 100 );
            } catch( InterruptedException e ) {
                e.printStackTrace();
            }
            Result result = null;
            BufferedImage image;
            // Only try to read qr code if webcam is open and frame is webCamFrame is visible
            if( webcam.isOpen() && isVisible() ) {
                if( (image = webcam.getImage()) == null ) {
                    continue;
                }
                LuminanceSource source = new BufferedImageLuminanceSource( image );
                BinaryBitmap bitmap = new BinaryBitmap( new HybridBinarizer(source) );
                try {
                    result = new MultiFormatReader().decode( bitmap );
                } catch( NotFoundException e ) {
                    // fall through if there is no QR code in image
                }
            }
            if( result != null ) {
                String cardName = result.getText();
                if( !cardsPlayed.contains( cardName ) ) {
                    labelText.setText( "Play card" + (cardsPlayed.size() > 0 ? "s" : "") + "?" );
                    cardsPlayed.add( cardName ); 
                    JPanel cardPlayedPanel = new JPanel( new GridLayout( 0,1,5) );
                    cardPlayedPanel.setBorder( BorderFactory.createCompoundBorder(new LineBorder(Color.BLACK),new EmptyBorder(2,2,2)) );
                    cardPlayedPanel.setOpaque( true );
                    cardPlayedPanel.setBackground( Color.LIGHT_GRAY );
                    cardPlayedPanel.add( new JLabel(cardName) );
                    constraints.gridy++;
                    actionsPanel.add(cardPlayedPanel,constraints );
                    actionsContainer.setVisible( true );
                    buttonsPanel.setVisible( true );
                    pack();
                }
            }
        } while( running );
    }
}

在此先感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)