【问题标题】:Create a custom function that returns JLabel to main JFrame?创建一个将 JLabel 返回到主 JFrame 的自定义函数?
【发布时间】:2012-06-06 20:13:13
【问题描述】:

我希望我能记得包括我的所有详细信息,所以就这样吧。

我正在使用 Java,我正在创建自己的界面,主窗口是 JFrame,背景附加到 JFrame,背景图像(如 JLabel)与窗口完全吻合。然后我有一个附加到背景 Jlabel 的图像作为“开始”按钮。我的问题是我想创建一个自定义类,以方便每个按钮在其自己的类中发挥作用。

所以我不想在主类中拥有对按钮做出反应的函数,而是创建一个可以添加到背景的类...这是我的代码,也许有人可以给我一个例子,我可以弄清楚其余部分或为我指明正确的方向。

以下代码包含原始代码,请提供一个示例,说明如何更改它以将事件侦听器封装在 Jlabel 按钮的自己的类中

    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;



    public class pipboy{

    public static void main(String[] args){
    pipboy pipboy_os = new pipboy();
    pipboy_os.runmain();
}

public void runmain(){
    //Create and set up the window.
    JFrame frame = new JFrame("PIPBOY Research v0.0.03");

    //background
        ImageIcon background = new ImageIcon("UI/background/default.png");
        JLabel label=new JLabel(background);


    //radiation animated
    ImageIcon animated_loading = new ImageIcon("UI/icon/loading/loading.png");
    JLabel animated_icon = new JLabel(animated_loading);
    animated_icon.setSize(128, 128);
    animated_icon.setLocation(300, 125);

    //Buff arm guy icon
    ImageIcon icon_loading = new ImageIcon("UI/icon/34.png");
    JLabel icon = new JLabel(icon_loading);
    icon.setSize(128, 128);
    icon.setLocation(300, 50);


    label.add(icon);
    label.add(animated_icon);
    frame.add(label);

    //Display the window.
    frame.pack();
    frame.setVisible(true);

    //Full screen
     frame.setSize(800,480);
    frame.setExtendedState(Frame.MAXIMIZED_BOTH); 

    //Set default close operation for JFrame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    //This is the handler class i made but i want it in its own class with the buff arm icon picure so it can do something specific when the image is pressed
    HandlerClass handler = new HandlerClass();
    frame.addMouseListener(handler);
    frame.addMouseMotionListener(handler);
}

private static class HandlerClass implements MouseListener, MouseMotionListener{
    public void mouseClicked(MouseEvent event){
        System.out.println(String.format("Clicked at %d,%d", event.getX(), event.getY()));
    }

    public void mousePressed(MouseEvent event){

    }

    public void mouseReleased(MouseEvent event){

    }

    public void mouseEntered(MouseEvent event){

    }

    public void mouseExited(MouseEvent event){

    }

    public void mouseDragged(MouseEvent event){

    }

    public void mouseMoved(MouseEvent event){

    }
}

}

【问题讨论】:

    标签: java oop scope jframe jlabel


    【解决方案1】:

    显然我不知道多态性在 Java 中是如何工作的,但这是我的解决方案(更改了一些名称)

    import javax.swing.*;
    
    class GUItests{
        public JFrame mainContainerFrame;
    
        public static void main(String[] args){
            GUItests mainController = new GUItests();
            mainController.startGUI();
        }
    
        public void startGUI(){
            mainContainerFrame = new JFrame("test Frame");
    
            mainContainerFrame.setSize(800,480);
            mainContainerFrame.pack();
            mainContainerFrame.setVisible(true);
    
            mainContainerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JLabel customButtonVar = customButton();
            mainContainerFrame.getContentPane().add(customButtonVar);
    
        }
    
    
        public JLabel customButton(){
            JLabel icon = new JLabel("testing");
            icon.setSize(128, 128);
            icon.setLocation(300, 50);
            return icon;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 2015-04-27
      • 1970-01-01
      • 2021-12-18
      • 2018-12-27
      • 1970-01-01
      • 2013-11-25
      相关资源
      最近更新 更多