微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决将面板中的项目与顶部摆动对齐

我正在尝试将一些项目动态添加到 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 );
    }
}

在此先感谢您的帮助

解决方法

我想将面板及其项目垂直对齐到顶部。

阅读 How to Use GridBagLayout 上的 Swing 教程。

您可以使用 anchor 对象的 GridBagConstraints 约束来控制组件在可用空间中的位置。

编辑:

如果有人知道解决此问题的更好方法,请发布答案!

我们无法给出“准确”的答案,因为布局管理总是处理框架调整大小。我们不知道随着框架尺寸的变化,哪些组件应该增长或缩小。

无论如何,解决方案总是相同的。使用不同的布局管理器嵌套面板以实现您想要的布局。

我意识到这个问题的发生是因为我的框架使用了flowlayout并且flowlayout中的2个组件的高度不同

所以我上面的回答仍然有效。您将框架布局更改为也使用 GridBagLayout(而不是 FlowLayout)。然后将两个子面板添加到框架中。您使用“锚定”约束将面板锚定到单元格的垂直顶部。

或者,如果您真的想使用 BoxLayout 而不是 FlowLayout,那么当您将面板添加到 BoxLayout 时,您需要使用:

panel.setAlignmentY(0.0f);

在每个面板上。这应该告诉每个面板与顶部对齐。不需要覆盖 getBaseLine() 方法。

,

通过我自己的实验,我意识到这个问题的发生是因为我的框架使用了流布局,并且流布局中的 2 个组件的高度不同。然后因为 flowlayouts 总是垂直居中,我的问题正在发生。我从这里用一些hacky代码解决了这个问题: https://stackoverflow.com/a/28878404/6716062 但是,如果有人知道解决此问题的更好方法,请发布答案! :)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。