Java 如何单击图片,然后使该图片显示在新的JFrame中?

Java 如何单击图片,然后使该图片显示在新的JFrame中?,java,swing,Java,Swing,我有一个JFrame的开头,里面有12张不同的图片。我想点击一个,然后在新的JFrame中,显示相同的图片。在我的代码中,shirts是我的JFrame,包含12张图片,我希望单击的图片显示在名为sizes的新JFrame中。我制作了一个名为select的JLabel类型的ArrayList。这是我的密码: final JFrame shirts = new JFrame("T-shirts"); JPanel panel = new JPanel(new GridLayo

我有一个JFrame的开头,里面有12张不同的图片。我想点击一个,然后在新的JFrame中,显示相同的图片。在我的代码中,shirts是我的JFrame,包含12张图片,我希望单击的图片显示在名为sizes的新JFrame中。我制作了一个名为select的JLabel类型的ArrayList。这是我的密码:

final JFrame shirts = new JFrame("T-shirts");

          JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));

          for (int i = 1; i < 13; i++) {
            l = new JLabel(new ImageIcon("T-shirts/"+i+".jpg"), JLabel.CENTER);

            l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            l.setFont(l.getFont().deriveFont(20f));
            panel.add(l);
            select.add(l);
          }//end of for loop

          panel.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
              sizes = new JFrame("Shopping");
              sizes.setVisible(true);
              sizes.setSize(500, 500);
              sizes.setLocation(100,200);
              shirts.dispose();


            for(int i=0; i<12; i++){
                if(e.getSource()==select.get(i)){
            JLabel addition = newJLabel(newImageIcon(select.get(i).getText()));
                  //sizes.add(select.get(i));//Picture isn't added!!
               }//end of if

            }//end of for
            }//end of method  
          });//end of listener


          shirts.setContentPane(panel);
          shirts.setSize(1000, 1000);
          shirts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          shirts.setVisible(true);
最终JFrame衬衫=新JFrame(“T恤”);
JPanel面板=新JPanel(新网格布局(4,4,3,3));
对于(int i=1;i<13;i++){
l=新的JLabel(新图像图标(“T-shirts/”+i+“.jpg”)、JLabel.CENTER;
l、 setBordOrder(BorderFactory.createBevelOrder(BevelOrder.RAISED));
l、 setFont(l.getFont().deriveFont(20f));
小组.添加(l);
选择。添加(l);
}//循环结束
panel.addMouseListener(新的MouseAdapter(){
公共无效mouseClicked(MouseEvent e){
尺寸=新JFrame(“购物”);
大小。设置为可见(真);
尺寸。设置尺寸(500500);
尺寸。设置位置(100200);
衬衫。处理();

对于(int i=0;i尝试将每张图片设置为JButton,或者至少在每张图片后面放置一个不可见的按钮。然后,当在ActionListener扩展类中按下按钮时,创建新的JFrame,图片变大。

从开始

基本上,您希望使用表示图片的
JLabel
注册一个
MouseListener
,并实现它的
mouseClicked
方法

然后,您需要获取单击的
JLabel
图标
属性,并将其传递到新帧,您只需使用它设置另一个
JLabel的
图标
属性即可

类似于

addition.addMouseListener(new MouseAdapter() {

    @Override
    public void mouseClicked(MouseEvent e) {
        JLabel label = (JLabel) e.getComponent();
        Icon icon = label.getIcon();
        // Create new frame and pass it the icon value
    }

});
例如


您还可以考虑使用“<代码>卡布局< /代码>或<代码> jCalue<代码>,而不是另一个<代码> jFrase>代码>……这可能会变得凌乱

< p>您可以使用<代码> JButton < /代码>,使其看起来像<代码> Jabel> <代码>:

JButton button = new JButton( new ImageIcon("..." ));
button.setBorderPainted( false );
button.setContentFilled( false );
button.setFocusPainted( false );
button.addActionListener( new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        JButton button = (JButton)e.getSource();
        Icon icon = button.getIcon();
        // do something with the Icon.
    }
});
然后,您可以向按钮添加一个
ActionListener


ActionListener
比使用
MouseListener
并处理
mouseClicked
事件更可靠。
mouseClicked
事件仅在
mousePressed
mouseReleased
事件在同一鼠标点生成时才会生成e两个事件你不会得到一个用户认为是随机问题的
鼠标点击事件。

@HovercraftFullOfEels By'related'DYM'replicate'?我看不出两者之间的区别。@AndrewThompson:是的,我没有看到是同一个人问这些问题。没有什么比忽略你的答案更能激励你去做再次帮助那个家伙。以重复的形式结束这个问题。你如何将图标传递到JFrame?是的,我两天前在他们的专栏上向OP建议了这个…-/你可以把马牵到水里…@AndrewThompson,谢谢你的提醒。关于这个主题的三个问题…和计数。下次发布问题时不会这么快帮到你。