英文:
Display image chosen from Jfilechooser on Jpanel
问题
以下是您要翻译的内容:
我正在尝试创建一个具有按钮的JPanel,您可以单击该按钮以上传图像。单击按钮后,它会弹出一个带有JFilechooser的对话框,当您单击打开时,它会将文件路径名称存储在ArrayList中。我希望所选择的图像在选择后显示在JPanel上。我知道许多人以前已经问过这个问题,但是我已经尝试了所有我能找到的解决方案,但无法使图像显示出来。这是我单击上传按钮时actionperformed的代码(AddImages是Jpanel):
private void pic1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
int result = fc.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String sname = file.getAbsolutePath();
ImageIcon icon = new ImageIcon(sname);
JLabel label = new JLabel(icon, JLabel.CENTER);
AddImages.add(label);
}
}
当我尝试上传图像时,图像会存储在ArrayList中(通过添加在添加图像后打印ArrayList大小的行进行测试),但不会显示在JPanel上。有人知道我做错了什么吗?提前谢谢。
编辑:这是面板的完整代码(可见)。pic1和pic2的actionperformed是相同的代码,如上所示:
addImagePrompt.setText("Add some pics");
pic2.setText("add pic");
pic2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pic2ActionPerformed(evt);
}
});
pic1.setText("add pic");
pic1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pic1ActionPerformed(evt);
}
});
javax.swing.GroupLayout AddImagesLayout = new javax.swing.GroupLayout(AddImages);
AddImages.setLayout(AddImagesLayout);
AddImagesLayout.setHorizontalGroup(
AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(AddImagesLayout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(AddImagesLayout.createSequentialGroup()
.addComponent(pic2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pic2name, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(pic1)
.addComponent(addImagePrompt))
.addContainerGap(161, Short.MAX_VALUE))
);
AddImagesLayout.setVerticalGroup(
AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(AddImagesLayout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(addImagePrompt)
.addGap(18, 18, 18)
.addComponent(pic1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(pic2)
.addComponent(pic2name))
.addContainerGap(377, Short.MAX_VALUE))
);
mainPanel.add(AddImages, "card16");
英文:
I'm trying to make a JPanel that has a button that you click to upload an image. It brings up a dialog box with a JFilechooser that stores the file path name in an ArrayList when you click open. I want the chosen image to appear on the JPanel after it is chosen. I know lots of people have asked this question before, but I've tried all the solutions I can find and I can't get the image to appear. Here's my code for the actionperformed when I click the upload button (AddImages is the Jpanel):
private void pic1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
int result = fc.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String sname = file.getAbsolutePath();
ImageIcon icon = new ImageIcon(sname);
JLabel label = new JLabel(icon, JLabel.CENTER);
AddImages.add(label);
}
}
When I try to upload an image, the image gets stored in the ArrayList (tested this by adding a line that prints the size of the ArrayList after I add the image), but it doesn't show up on the JPanel. Does anyone know what I'm doing wrong? Thanks in advance.
EDIT: Here's the full code for the panel (which is visible). The actionperformed for pic1 and pic2 is the same code, which I've shown above:
addImagePrompt.setText("Add some pics");
pic2.setText("add pic");
pic2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pic2ActionPerformed(evt);
}
});
pic1.setText("add pic");
pic1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pic1ActionPerformed(evt);
}
});
javax.swing.GroupLayout AddImagesLayout = new javax.swing.GroupLayout(AddImages);
AddImages.setLayout(AddImagesLayout);
AddImagesLayout.setHorizontalGroup(
AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(AddImagesLayout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(AddImagesLayout.createSequentialGroup()
.addComponent(pic2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pic2name, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(pic1)
.addComponent(addImagePrompt))
.addContainerGap(161, Short.MAX_VALUE))
);
AddImagesLayout.setVerticalGroup(
AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(AddImagesLayout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(addImagePrompt)
.addGap(18, 18, 18)
.addComponent(pic1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(pic2)
.addComponent(pic2name))
.addContainerGap(377, Short.MAX_VALUE))
);
mainPanel.add(AddImages, "card16");
答案1
得分: 1
添加组件到容器后,您需要调用revalidate()
方法,以便其布局管理器重新计算子组件的位置并重新绘制它们:
JLabel label = new JLabel(icon, JLabel.CENTER);
AddImages.add(label);
AddImages.revalidate();
如果您在同一个“AddImages”面板中上传多个图像,您会发现它们作为单独的组件添加到面板中。如果这不符合您的预期,您需要重新考虑您的方法。
英文:
After adding a component to a container, you have to call revalidate()
so that its layout manager (re)computes the positions for child components and repaints them:
JLabel label = new JLabel(icon, JLabel.CENTER);
AddImages.add(label);
AddImages.revalidate();
If you upload multiple images using the same "AddImages" panel you'll see that they are all added the panel as separate components. If that's not what you expect, you'll need to rethink how you approach this.
答案2
得分: 0
这里唯一缺少的是适当的布局管理器。
你可以在这里阅读相关信息:布局管理器可视化指南
所以,例如添加以下内容应该可以帮助你实现你想要的效果:
AddImages.setLayout(new BoxLayout(AddImages, BoxLayout.Y_AXIS));
AddImages.add(label);
AddImages.revalidate();
this.pack();
英文:
The only thing you are missing here is a proper layout manager.
You can read up on this right here: A Visual Guide to Layout Managers
So adding something like this for example should help you achieve what you desire:
AddImages.setLayout(new BoxLayout(AddImages, BoxLayout.Y_AXIS));
AddImages.add(label);
AddImages.revalidate();
this.pack();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论