gpt4 book ai didi

java - gridbag约束不起作用

转载 作者:行者123 更新时间:2023-12-01 14:40:51 25 4
gpt4 key购买 nike

我使用 gridbaglayout 来显示 JLabel (缩略图数量)和一些缩略图,每当我使用 gridbaglayout 将缩略图添加到 Jpanel 时,它都会显示在中心,忽略网格值。 gridx 值工作正常,但 gridy 值完全被忽略......

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;


public class grid {

private JFrame frame = new JFrame();

private JLabel a = new JLabel("Welcome to PhotoAlbum55");

/**
* Create the frame.
*/
public grid() {

createAndShowGUI();

}

private void addComponentsToPane(Container pane) {

SpringLayout layout = new SpringLayout();
pane.setLayout(layout);

layout.putConstraint(SpringLayout.WEST, a, 60, SpringLayout.WEST, pane);
layout.putConstraint(SpringLayout.NORTH, a, 0, SpringLayout.NORTH, pane);
a.setFont(new Font("Segoe UI", Font.BOLD, 20));
pane.add(a);

JPanel photoPanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
photoPanel.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();

JScrollPane photoScroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
photoScroll.setViewportView(photoPanel);
pane.add(photoScroll);

gbc.gridx = 0;
gbc.gridy = 0;
gbc.ipadx = 15;
gbc.fill = GridBagConstraints.HORIZONTAL;

JLabel photo = new JLabel();
Image img = new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg").getImage();
Image newimg = img.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(newimg);
photo.setIcon(newIcon);

gbl.setConstraints(photo, gbc);
photoPanel.add(photo);

JLabel photo1 = new JLabel();
Image img1 = new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg").getImage();
Image newimg1 = img1.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon1 = new ImageIcon(newimg1);
photo1.setIcon(newIcon1);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.ipadx = 15;
gbc.fill = GridBagConstraints.HORIZONTAL;


gbl.setConstraints(photo1, gbc);
photoPanel.add(photo1);







// photoPanel.setPreferredSize(new Dimension(900, 900));
photoScroll.setPreferredSize(new Dimension(500, 500));


layout.putConstraint(SpringLayout.WEST, photoScroll, 60, SpringLayout.WEST, a);
layout.putConstraint(SpringLayout.NORTH, photoScroll, 100, SpringLayout.NORTH, a);


}

private void createAndShowGUI() { // Creating the GUI...

frame.getContentPane().setBackground(Color.WHITE);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("PhotoAlbum55");

// Set up the content pane.
addComponentsToPane(frame.getContentPane());

frame.pack();
frame.setSize(690, 622);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);

}

/**
* Launch the application.
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

SwingUtilities.invokeLater(new Runnable() {

public void run() {

try {

new grid();
}

catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

最佳答案

两个图像都位于相同的 y 坐标上,因此应用程序的行为符合预期。另一方面,x 坐标不同,因此您需要设置

gbc.weightx = 1;

让他们被定位。如果您需要将图像添加到面板顶部,则需要将它们锚定在那里并设置沿 y 轴的权重:

gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;

关于java - gridbag约束不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15979818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com