为什么我的 gui 布局在上传照片时会发生变化?
Why does my gui layout change when uploading a photo?
我的程序用户界面目前使用网格包布局,我希望它是固定大小,但是当我将图片上传到标签时,整个界面的尺寸会发生变化。
下面是我的布局管理器的代码
public SearchService() throws Exception {
setSize(600, 600);
setResizable(false);
JPanel mainPanel = new JPanel();
JPanel templatePanel = new JPanel();
JPanel toolPanel = new JPanel();
JLabel picLabel = new JLabel();
JLabel tools = new JLabel("Tools");
JLabel templates = new JLabel("Templates");
JButton upload = new JButton("Upload");
JButton search = new JButton("Search");
JButton save = new JButton("Save");
//Main panel
GridBagLayout GBPanel = new GridBagLayout();
GridBagConstraints GBC = new GridBagConstraints();
mainPanel.setLayout( GBPanel );
//Template panel
GBC.gridx = 0;
GBC.gridy = 0;
GBC.gridwidth = 1;
GBC.gridheight = 3;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 1;
GBC.weighty = 0;
GBC.anchor = GridBagConstraints.WEST;
GBPanel.setConstraints( leftPanel, GBC );
leftPanel.add(templates);
mainPanel.add( leftPanel );
//Picture label
GBC.gridx = 1;
GBC.gridy = 0;
GBC.gridwidth = 2;
GBC.gridheight = 1;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 0;
GBC.weighty = 1;
GBC.anchor = GridBagConstraints.CENTER;
GBPanel.setConstraints( picLabel, GBC );
mainPanel.add( picLabel );
//Tool panel
GBC.gridx = 4;
GBC.gridy = 0;
GBC.gridwidth = 1;
GBC.gridheight = 3;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 1;
GBC.weighty = 0;
GBC.anchor = GridBagConstraints.EAST;
GBPanel.setConstraints( rightPanel, GBC );
rightPanel.add(tools);
mainPanel.add( rightPanel );
//Upload button
GBC.gridx = 1;
GBC.gridy = 1;
GBC.gridwidth = 1;
GBC.gridheight = 1;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 1;
GBC.weighty = 0;
GBC.anchor = GridBagConstraints.PAGE_START;
GBPanel.setConstraints( upload, GBC );
mainPanel.add( upload );
//Save button
GBC.gridx = 2;
GBC.gridy = 1;
GBC.gridwidth = 1;
GBC.gridheight = 1;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 1;
GBC.weighty = 0;
GBC.anchor = GridBagConstraints.PAGE_START;
GBPanel.setConstraints( save, GBC );
mainPanel.add( save );
//Search button
GBC.gridx = 1;
GBC.gridy = 2;
GBC.gridwidth = 2;
GBC.gridheight = 1;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 1;
GBC.weighty = 0;
GBC.anchor = GridBagConstraints.PAGE_START;
GBPanel.setConstraints( search, GBC );
mainPanel.add( search );
add(mainPanel);
下面是添加图片的代码
upload.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser("C:\Users);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Image", "jpg", "png", "bmp");
chooser.setFileFilter(filter);
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
BufferedImage bi;
userPhoto = chooser.getSelectedFile().getPath();
try {
bi = ImageIO.read(selectedFile);
Image dimg = bi.getScaledInstance(picLabel.getWidth(), picLabel.getHeight(), Image.SCALE_SMOOTH);
picLabel.setIcon(new ImageIcon(dimg));
}
catch(IOException IOe) {
IOe.printStackTrace();
}
System.out.println(userPhoto);
}
}
});
我添加了两张照片来显示我的程序的结果。这是我第一次 运行 时的样子,也是我希望布局保持不变的样子
这是上传图片后布局的样子
如您所见,左右面板得到 sh运行k,图片甚至没有占据整个图片标签。
我还添加了这一行 System.out.println(picLabel.getWidth());在动作侦听器中,看到当第一次按下按钮时,大小设置为 299,但如果我再次按下按钮,它会改变并且每次都会改变。我想知道是否可以让图像保持在299的宽度。
GBC.gridwidth = 5;
GBC.gridheight = 20
您不能随意将 gridwith/height 分配给组件。如果你想让组件跨越与其他组件相同的高度,你实际上需要 20 个其他组件。
as you can see the left and right panels get shrunk and the picture doesn't even take up the whole picture label.
如果您不想更改布局,请使用不同的布局管理器或具有不同布局管理器的嵌套面板。
例如,您从 BorderLayout
开始。
然后您可以将面板添加到 LINE_START
和 LINE_END
。
然后您需要 CENTER
中的另一个面板。同样,您可以使用 BorderLayout
。您将图片添加到 CENTER
,然后将另一个面板的按钮添加到 PAGE_END
。
现在除图像外的所有组件都是固定大小的。图像可用的 space 将根据框架的大小而有所不同。
所以基本代码是:
JPanel buttonPanel = new JPanel(...);
JLabel image = new JLabel(...);
JPanel center = new JPanel( new BorderLayout() );
center.add(image, BorderrLayout.CENTER);
center.add(buttonPanel, BorderLayout.PAGE_END);
JPanel leftPanel = new JPanel(...);
JPanel rightPanel = new JPanel(...);
frame.add(leftPanel, BorderLayout.LINE_START);
frame.add(center, BorderLayout.CENTER);
frame.add(rightPanel, BorderLayout.LINE_END);
比尝试使用 GridBagLayout 的所有约束要容易得多。
现在子面板(左、右、按钮)可以使用适当的布局管理器。
我的程序用户界面目前使用网格包布局,我希望它是固定大小,但是当我将图片上传到标签时,整个界面的尺寸会发生变化。
下面是我的布局管理器的代码
public SearchService() throws Exception {
setSize(600, 600);
setResizable(false);
JPanel mainPanel = new JPanel();
JPanel templatePanel = new JPanel();
JPanel toolPanel = new JPanel();
JLabel picLabel = new JLabel();
JLabel tools = new JLabel("Tools");
JLabel templates = new JLabel("Templates");
JButton upload = new JButton("Upload");
JButton search = new JButton("Search");
JButton save = new JButton("Save");
//Main panel
GridBagLayout GBPanel = new GridBagLayout();
GridBagConstraints GBC = new GridBagConstraints();
mainPanel.setLayout( GBPanel );
//Template panel
GBC.gridx = 0;
GBC.gridy = 0;
GBC.gridwidth = 1;
GBC.gridheight = 3;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 1;
GBC.weighty = 0;
GBC.anchor = GridBagConstraints.WEST;
GBPanel.setConstraints( leftPanel, GBC );
leftPanel.add(templates);
mainPanel.add( leftPanel );
//Picture label
GBC.gridx = 1;
GBC.gridy = 0;
GBC.gridwidth = 2;
GBC.gridheight = 1;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 0;
GBC.weighty = 1;
GBC.anchor = GridBagConstraints.CENTER;
GBPanel.setConstraints( picLabel, GBC );
mainPanel.add( picLabel );
//Tool panel
GBC.gridx = 4;
GBC.gridy = 0;
GBC.gridwidth = 1;
GBC.gridheight = 3;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 1;
GBC.weighty = 0;
GBC.anchor = GridBagConstraints.EAST;
GBPanel.setConstraints( rightPanel, GBC );
rightPanel.add(tools);
mainPanel.add( rightPanel );
//Upload button
GBC.gridx = 1;
GBC.gridy = 1;
GBC.gridwidth = 1;
GBC.gridheight = 1;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 1;
GBC.weighty = 0;
GBC.anchor = GridBagConstraints.PAGE_START;
GBPanel.setConstraints( upload, GBC );
mainPanel.add( upload );
//Save button
GBC.gridx = 2;
GBC.gridy = 1;
GBC.gridwidth = 1;
GBC.gridheight = 1;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 1;
GBC.weighty = 0;
GBC.anchor = GridBagConstraints.PAGE_START;
GBPanel.setConstraints( save, GBC );
mainPanel.add( save );
//Search button
GBC.gridx = 1;
GBC.gridy = 2;
GBC.gridwidth = 2;
GBC.gridheight = 1;
GBC.fill = GridBagConstraints.BOTH;
GBC.weightx = 1;
GBC.weighty = 0;
GBC.anchor = GridBagConstraints.PAGE_START;
GBPanel.setConstraints( search, GBC );
mainPanel.add( search );
add(mainPanel);
下面是添加图片的代码
upload.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser("C:\Users);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Image", "jpg", "png", "bmp");
chooser.setFileFilter(filter);
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
BufferedImage bi;
userPhoto = chooser.getSelectedFile().getPath();
try {
bi = ImageIO.read(selectedFile);
Image dimg = bi.getScaledInstance(picLabel.getWidth(), picLabel.getHeight(), Image.SCALE_SMOOTH);
picLabel.setIcon(new ImageIcon(dimg));
}
catch(IOException IOe) {
IOe.printStackTrace();
}
System.out.println(userPhoto);
}
}
});
我添加了两张照片来显示我的程序的结果。这是我第一次 运行 时的样子,也是我希望布局保持不变的样子
这是上传图片后布局的样子
如您所见,左右面板得到 sh运行k,图片甚至没有占据整个图片标签。
我还添加了这一行 System.out.println(picLabel.getWidth());在动作侦听器中,看到当第一次按下按钮时,大小设置为 299,但如果我再次按下按钮,它会改变并且每次都会改变。我想知道是否可以让图像保持在299的宽度。
GBC.gridwidth = 5;
GBC.gridheight = 20
您不能随意将 gridwith/height 分配给组件。如果你想让组件跨越与其他组件相同的高度,你实际上需要 20 个其他组件。
as you can see the left and right panels get shrunk and the picture doesn't even take up the whole picture label.
如果您不想更改布局,请使用不同的布局管理器或具有不同布局管理器的嵌套面板。
例如,您从 BorderLayout
开始。
然后您可以将面板添加到 LINE_START
和 LINE_END
。
然后您需要 CENTER
中的另一个面板。同样,您可以使用 BorderLayout
。您将图片添加到 CENTER
,然后将另一个面板的按钮添加到 PAGE_END
。
现在除图像外的所有组件都是固定大小的。图像可用的 space 将根据框架的大小而有所不同。
所以基本代码是:
JPanel buttonPanel = new JPanel(...);
JLabel image = new JLabel(...);
JPanel center = new JPanel( new BorderLayout() );
center.add(image, BorderrLayout.CENTER);
center.add(buttonPanel, BorderLayout.PAGE_END);
JPanel leftPanel = new JPanel(...);
JPanel rightPanel = new JPanel(...);
frame.add(leftPanel, BorderLayout.LINE_START);
frame.add(center, BorderLayout.CENTER);
frame.add(rightPanel, BorderLayout.LINE_END);
比尝试使用 GridBagLayout 的所有约束要容易得多。
现在子面板(左、右、按钮)可以使用适当的布局管理器。