Java 将面板中的项目与顶部摆动对齐
我正在尝试将一些项目动态添加到jpanel。我使用了gridbaglayout作为这个jpanel的管理器(下面是红色背景色),我想将面板及其项目垂直对齐到顶部。你知道我怎样才能把这个面板放到面板的顶部吗 这是我的密码:Java 将面板中的项目与顶部摆动对齐,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我正在尝试将一些项目动态添加到jpanel。我使用了gridbaglayout作为这个jpanel的管理器(下面是红色背景色),我想将面板及其项目垂直对齐到顶部。你知道我怎样才能把这个面板放到面板的顶部吗 这是我的密码: public class WebcamFrame extends JFrame implements Runnable { private Webcam webcam; private WebcamPanel webcamPanel; private
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, 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, 5) );
cardPlayedPanel.setBorder( BorderFactory.createCompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(2, 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 );
}
}
公共类WebcamFrame扩展JFrame实现可运行{
私人网络摄像机;
私人网络摄像机网络摄像机;
私人JLabel标签文本;
私人JPanel actionsPanel;
私有布尔运行;
私有哈希集卡片显示;
私人最终JPanel行动承包商;
专用最终JPanel按钮面板;
私有最终网格约束;
公共网络摄像头框架(回调网络摄像头、回调手柄提交){
运行=真;
setLayout(新的FlowLayout());
setTitle(“扫描卡片”);
setIconImage(新的图像图标(getClass().getResource(“/tslogo.png”)).getImage();
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(新的WindowAdapter(){
@凌驾
公共无效窗口关闭(WindowEvent e){
超级窗口关闭(e);
killWebcam.call();
}
} );
cardsPlayed=newhashset();
维度大小=WebcamResolution.QVGA.getSize();
webcam=webcam.getDefault();
网络摄像头。setViewSize(大小);
网络摄像机=新的网络摄像机(网络摄像机);
webcamPanel.setPreferredSize(大小);
setLayout(新的BorderLayout());
labelText=新的JLabel(“,SwingConstants.CENTER);
if(Webcam.getDefault()==null)labelText.setText(“未检测到网络摄像头”);
actionsContainer=newjpanel(newborderlayout());
JButton confirButton=新JButton(“确认选择”);
confirButton.addActionListener(e->{
List cardsToSubmit=new ArrayList();
cardsPlayed.forEach(cardName->{
CardDataModel卡=新的CardDataModel();
card.cardName=cardName;
卡片提交。添加(卡片);
} );
handleSubmit.call(cardsToSubmit);
} );
JButton clearButton=新JButton(“清除选择”);
buttonsPanel=新的JPanel();
按钮面板添加(确认按钮);
按钮面板添加(clearButton);
按钮面板设置可见(假);
约束=新的GridBagConstraints();
clearButton.addActionListener(e->{
cardsPlayed.clear();
actionsPanel.removeAll();
constraints.gridy=0;
actionsPanel.add(新的JLabel(“扑克牌”),约束条件);
actionsContainer.setVisible(false);
按钮面板设置可见(假);
labelText.setVisible(假);
constraints.gridy=0;
包装();
} );
JPanel subPanel=新的JPanel();
子面板setLayout(新的BorderLayout());
子面板添加(labelText,BorderLayout.NORTH);
子面板添加(按钮面板,BorderLayout.SOUTH);
webcamPanel.add(BorderLayout.SOUTH子面板);
actionsPanel=newJPanel(newGridBagLayout());
constraints.gridy=0;
constraints.insets=新的insets(0,0,5,0);
actionsPanel.add(新的JLabel(“扑克牌”),约束条件);
行动计划。挫折背景(颜色。红色);
JScrollPane scrollPane=新的JScrollPane(actionsPanel);
actionsContainer.add(滚动窗格,BorderLayout.NORTH);
actionsContainer.setVisible(false);
添加(网络摄像机);
添加(actionsContainer);
包装();
setLocationRelativeTo(空);
setVisible(假);
可设置大小(假);
}
@凌驾
公开募捐{
做{
试一试{
睡眠(100);
}捕捉(中断异常e){
e、 printStackTrace();
}
结果=空;
缓冲图像;
//只有当网络摄像头打开且摄像头框架可见时,才尝试读取二维码
if(webcam.isOpen()&&isVisible()){
如果((image=webcam.getImage())==null){
继续;
}
亮度源=新的缓冲区图像亮度源(图像);
BinaryBitmap位图=新的BinaryBitmap(新的混合二进制程序(源));
试一试{
结果=新的MultiFormatReader()。解码(位图);
}捕获(未发现异常){
//如果图像中没有二维码,则失败
}
}
如果(结果!=null){
String cardName=result.getText();
如果(!cardsPlayed.contains(cardName)){
labelText.setText(“扑克牌”+(cardsPlayed.size()>0?):“)+”?”;
卡片显示。添加(卡片名称);
JPanel cardPlayedPanel=新的JPanel(新的网格布局(0,1,5,5));
cardPlayedPanel.setboorder(BorderFactory.createCompoundBorder(新的LineBorder(Color.BLACK),新的EmptyBorder(2,2,2,2));
cardPlayedPanel.setOpaque(true);
插卡式面板。立根地面(颜色:浅灰色);
添加(新JLabel(cardName));
约束条件;
actionsPanel.add(cardPlayedPanel,约束);
actionsContainer.setVisible(true);
按钮面板设置可见(真);
包装();
}
}
}跑步时;
}
}
提前谢谢你的帮助
我想将面板及其项目垂直对齐到顶部
panel.setAlignmentY(0.0f);