我目前正在尝试使用Luna Eclipse中的Window Builder创建一个Java swing程序,以显示学生的图像以确认身份(已更改,因为我不想惹麻烦)。如果图像与人物匹配,则观察者点击“确认投票”按钮。我还没有实现功能,我只是在视觉上工作。当运行下面的代码时,我希望图像随窗口大小缩放。
通过研究,我发现了如何显示图像,现在需要将其缩放到窗格的大小。如果没有解决方案,我可以将尺寸保持在设定值,但是我希望它是
public class PhotoView extends JDialog {
private final JPanel contentPanel = new JPanel();
public static void main(String[] args) {
PhotoView dialog = new
PhotoView();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
public PhotoView() {
setBounds(100, 100, 801, 851);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
ImageIcon image = new ImageIcon("src/icon.png");
JLabel label = new JLabel("", image, JLabel.CENTER);
label.setVerticalAlignment(SwingConstants.TOP);
contentPanel.add(label, BorderLayout.CENTER );
GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
gl_contentPanel.setHorizontalGroup(
gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addComponent(label, GroupLayout.DEFAULT_SIZE, 434,
Short.MAX_VALUE)
.addContainerGap())
);
gl_contentPanel.setVerticalGroup(
gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addContainerGap()
.addComponent(label, GroupLayout.DEFAULT_SIZE, 217,
Short.MAX_VALUE).addContainerGap())
);
contentPanel.setLayout(gl_contentPanel);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("Confirm Vote");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}