【问题标题】:how can i send jframe as argument to a method of another class?我如何将 jframe 作为参数发送给另一个类的方法?
【发布时间】:2023-03-15 05:59:01
【问题描述】:

我想将 J-frame 的对象从 Action Listener 发送到另一个类的方法,以便被调用方法的内容在同一帧中。但我无法在同一个框架中获得该方法。请帮助..

     public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
    initComponents(this);
}                      
public void initComponents(NewJFrame ob) {

    scrollbar1 = new java.awt.Scrollbar();
    jScrollBar1 = new javax.swing.JScrollBar(); //all the GUI**inside this method i created button(jMenuItem2) and called method of another class in action listener as shown below**   

jMenuItem2.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent arg) {
        MenuExp obb=new MenuExp();
       // System.out.println(arg.getSource().getClass());
        obb.cross(ob);
         } });
  }
public class MewExp{
public void cross(NewJFrame obbb)
{
    //final JFrame frame = new JFrame("JToolBar Demo");
    JToolBar toolbar = new JToolBar();

    JButton btnCalendar = new JButton(new ImageIcon("C:\\Users\\vishnu\\Documents\\NetBeansProjects\\imazer\\src\\imazer\\kick.jpg"));
    btnCalendar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
           // JOptionPane.showMessageDialog(, "Calendar clicked");
        }
    });
    toolbar.add(btnCalendar);
   // setLayout(new BorderLayout());
    obbb.getContentPane().add(toolbar, BorderLayout.WEST);
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //setSize(200, 500);
    //setVisible(true);
    //System.out.print(obbb);

    obbb.add(toolbar);
   // setVisible(true);
   // obbb.pack();

}    

【问题讨论】:

  • 到目前为止您尝试了什么?向我们展示您的代码

标签: java swing jframe


【解决方案1】:

假设Menu 是另一个类

并且那个类有方法changeContent(JFrame sameFrame)

然后

     Menu menu=new Menu();
     menu.changeContent(this);

将为您完成这项工作。

【讨论】:

  • 它不起作用。我需要changeContent 的内容在同一帧中,但方法是打开一个新帧
  • 发布你的 changeContent() 方法
猜你喜欢
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
相关资源
最近更新 更多