Java 要在单击按钮时向JPanel添加图片吗
我必须创建一个程序,在该程序中,我将在对话框中显示一些选项供用户选择。 根据用户选择的选项,我必须在另一个之前为空的对话框中显示该图片 例如:Java 要在单击按钮时向JPanel添加图片吗,java,swing,jpanel,jdialog,Java,Swing,Jpanel,Jdialog,我必须创建一个程序,在该程序中,我将在对话框中显示一些选项供用户选择。 根据用户选择的选项,我必须在另一个之前为空的对话框中显示该图片 例如: 用户可以看到对话框“一”和“二”。对话框“一”上显示了许多按钮。其中as对话框“2”为空 用户点击对话框“一”上的可用按钮,然后我必须在对话框“二”上显示该图片 用户单击对话框“一”上的另一个按钮B,然后我必须在对话框“二”上显示该图片以及旧图片 这可以在不创建新对话框“2”或不为对话框“2”创建新JPanel的情况下动态完成吗 到目前为止,我已经创
- 用户可以看到对话框“一”和“二”。对话框“一”上显示了许多按钮。其中as对话框“2”为空
- 用户点击对话框“一”上的可用按钮,然后我必须在对话框“二”上显示该图片
- 用户单击对话框“一”上的另一个按钮B,然后我必须在对话框“二”上显示该图片以及旧图片
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.plaf.metal.MetalIconFactory.FolderIcon16;
public class Launcher {
JDialog keyboardDialog;
JDialog nameViewDialog;
JPanel nameViewJPanel;
JDialog FinalNameViewDialog;
private final transient ActionListener keyButtonListener =
new ActionListener() {
@Override public void actionPerformed(final ActionEvent event) {
System.out.println( ((JButton) event.getSource()).getActionCommand());
String buttonType=((JButton) event.getSource()).getActionCommand();
ImageIcon iconA = new ImageIcon(this.getClass().getResource("\\Icons\\A1.PNG"));
JLabel la=new JLabel(iconA);
nameViewJPanel.add(la);
nameViewJPanel.repaint();
}
};
public Launcher()
{
nameViewDialog=new JDialog();
nameViewDialog.setLayout(new BorderLayout());
nameViewJPanel=new JPanel();
nameViewJPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
nameViewDialog.setSize(430, 490);
ImageIcon iconA1 = new ImageIcon(this.getClass().getResource("\\Icons\\A1.PNG"));
JLabel la=new JLabel(iconA1);
nameViewJPanel.add(la);
ImageIcon iconA2 = new ImageIcon(this.getClass().getResource("\\Icons\\B1.PNG"));
JLabel lb=new JLabel(iconA2);
nameViewJPanel.add(lb);
nameViewDialog.add(nameViewJPanel);
keyboardDialog=new JDialog(nameViewDialog,ModalityType.MODELESS);
keyboardDialog.setLocationRelativeTo(nameViewDialog);
keyboardDialog.setSize(230,190);
keyboardDialog.setLayout(new GridLayout(2,3));
ImageIcon iconA = new ImageIcon(this.getClass().getResource("\\JaLetters\\A.PNG"));
ImageIcon iconB = new ImageIcon(this.getClass().getResource("\\JaLetters\\B.PNG"));
ImageIcon iconC = new ImageIcon(this.getClass().getResource("\\JaLetters\\C.PNG"));
ImageIcon iconD = new ImageIcon(this.getClass().getResource("\\JaLetters\\D.PNG"));
ImageIcon iconE = new ImageIcon(this.getClass().getResource("\\JaLetters\\E.PNG"));
ImageIcon iconF = new ImageIcon(this.getClass().getResource("\\JaLetters\\F.PNG"));
JButton ba=new JButton();
ba.setIcon(iconA);
ba.setActionCommand("A");
ba.addActionListener(keyButtonListener);
JButton bb=new JButton();
bb.setIcon(iconB);
bb.setActionCommand("B");
bb.addActionListener(keyButtonListener);
JButton bc=new JButton();
bc.setIcon(iconC);
bc.setActionCommand("C");
bc.addActionListener(keyButtonListener);
JButton bd=new JButton();
bd.setIcon(iconD);
bd.setActionCommand("D");
bd.addActionListener(keyButtonListener);
JButton be=new JButton();
be.setIcon(iconE);
be.setActionCommand("E");
be.addActionListener(keyButtonListener);
JButton bf=new JButton();
bf.setIcon(iconF);
bf.setActionCommand("F");
bf.addActionListener(keyButtonListener);
keyboardDialog.add(ba);
keyboardDialog.add(bb);
keyboardDialog.add(bc);
keyboardDialog.add(bd);
keyboardDialog.add(be);
keyboardDialog.add(bf);
nameViewDialog.setVisible(true);
keyboardDialog.setVisible(true);
}
public static void main(String args[])
{
new Launcher();
}
}
我在对话框2中添加了一个CustomJPanel。每个actionlistener加载不同的图像并将其发送到绘制图像的CustomJPanel 以下是it的MVC: 主类:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
public class Main {
public static void main(String[] args) {
JDialog dialog = new JDialog();
dialog.setSize(600, 400);
dialog.setLocationRelativeTo(null);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
CustomJPanel customJDialog = new CustomJPanel();
dialog.add(customJDialog);
JDialog dialog2 = new JDialog();
dialog2.setLayout(new FlowLayout());
dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog2.setLocationRelativeTo(dialog);
JButton button1 = new JButton("Image 1");
JButton button2 = new JButton("Image 2");
dialog2.add(button1);
dialog2.add(button2);
dialog2.pack();
dialog2.setVisible(true);
button1.addActionListener(new ActionListener() {
BufferedImage image = null;
@Override
public void actionPerformed(ActionEvent e) {
try {
image = ImageIO.read(getClass().getResource("test1.jpg"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// TODO Auto-generated method stub
customJDialog.setImage(image);
}
});
button2.addActionListener(new ActionListener() {
BufferedImage image = null;
@Override
public void actionPerformed(ActionEvent e) {
try {
image = ImageIO.read(getClass().getResource("test2.jpg"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// TODO Auto-generated method stub
customJDialog.setImage(image);
}
});
}
}
CustomJPanel
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class CustomJPanel extends JPanel {
BufferedImage image = null;
public CustomJPanel() {
}
@Override
public void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
System.out.println(image);
}
public void setImage(BufferedImage image) {
this.image = image;
repaint();
}
}
这让模特和模特尖叫不已。但是,我不太愿意使用两个对话框来实现这一点,我更愿意使用
JSplitPane
中的面板,或者使用第二个对话框作为弹出窗口来收集信息,当关闭时,允许第一个窗口更新自己是否有任何机制可以使用新图片重新绘制窗口,但仍将旧图片保留在CustomPanel上。你的答案对我来说很有用,但我需要将旧图片也保留在屏幕上。我可以更改你的程序,将所有图片存储在列表中,并通过循环绘制所有图片,每次使用列表重新绘制窗口。但我正在寻找是否有其他有效的方法,我不需要绘制所有图片,而是只绘制最后一张图片,旧图片保持原样。@HimJEL我想不出其他方法,只能为列表中的每个图像添加CustomJPanel,而不是添加它们并从对话框中删除。(你也可以使用其他组件,比如JLabel,不需要是JPanel)但是我想这不适合你,因为你在这个问题上说了。您仅限于哪种优化?