简体   繁体   English

单击不同类别的按钮时更新JFrame

[英]Update JFrame when button from different class is clicked

Currently I am having a major issue when trying to update my JFrame with new information. 当前,尝试使用新信息更新JFrame时遇到主要问题。 I tried the usual invalidate, validate, repaint, ect. 我尝试了通常的无效,验证,重涂等。

Here is the main code that creates the JFrame: 这是创建JFrame的主要代码:

package Calendar;

import javax.swing.*;
import java.awt.*;
import java.util.GregorianCalendar;
import java.awt.Frame;

public class Main extends JFrame
{


    public Main()
    {

    }

    public void makeFrame()
    {
        JFrame frame = new JFrame("Programming II Project: Calendar");

        setLayout(new BorderLayout(0, 5));

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setMinimumSize(new Dimension(640, 384));
    }

    public void makeCenter(int year, int month)
    {
        // Add center
        //new JFrame();
        //this.setVisible(true);
        JPanel p = new JPanel(new BorderLayout());
        p.add(calendarPanel.makeCalendar(year, month));
        add(p, BorderLayout.CENTER);
        System.out.println("----- FRAME WINDOWS -----");
        Frame[] frames = Frame.getFrames();
        for (Frame frame : frames)
            System.out.println(frame.getName() + ": " + frame.getClass());

        //this.setMinimumSize(new Dimension(512, 384));
    }



    public void makeEast()
    {
        JPanel p = new JPanel(new BorderLayout());
        Clock myClock = new Clock();
        p.add(myClock);
        p.setForeground(Color.red);
        this.add(p, BorderLayout.EAST);
    }

    public void makeNorth()
    {
        // Add north
        northPanel np = new northPanel();
        this.add(np, BorderLayout.NORTH);
    }

    public static void main(String[] args)
    {

        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (ClassNotFoundException e)
        {
            System.out.println("ClassNotFoundException");
        }
        catch (InstantiationException e)
        {
            System.out.println("InstantiationException");
        }
        catch (IllegalAccessException e)
        {
            System.out.println("IllegalAccessException");
        }
        catch (UnsupportedLookAndFeelException e)
        {
            System.out.println("UnsupportedLookAndFeelException");
        }

        Main m = new Main();
        m.makeFrame();
        m.makeCenter(GregorianCalendar.YEAR, GregorianCalendar.MONTH);
        m.makeNorth();
        m.makeEast();
        m.setVisible(true);
    }
}

And this is the button code that is suppose to switch the JFrame: This is in a totally differnet class 这是用来切换JFrame的按钮代码:这是在完全不同的net类中

static class bNextMonth_Method implements ActionListener{
    public void actionPerformed (ActionEvent e)
    {
        if (clickm == 11)
        {
            clicky = clicky + 1;
            clickm = 0;
            System.out.println(clickm);
            System.out.println(clicky);
            Main m = new Main();
            m.makeCenter(clicky, clickm);
            m.makeNorth();
            m.makeEast();
        }
        else
        {
            clickm = clickm + 1;
            System.out.println(clickm);
            System.out.println(clicky);
            Main m = new Main();
            m.makeCenter(clicky, clickm);
            m.makeNorth();
            m.makeEast();
        }
    }
}

Now you'll notice that in the first bit of code in the makeCenter method the commented out stuff. 现在,您会注意到在makeCenter方法的第一部分代码中,注释掉了。 If I un-comment that it the information changes but it creates a new window every time. 如果我取消评论,则信息会更改,但每次都会创建一个新窗口。

Now even though dispose() is not in there currently, I put it in the makeCenter method since that was what was called from the button. 现在即使dispose()当前不存在,我也将其放在makeCenter方法中,因为那是从按钮中调用的。 I can tell that the button is working properly because 我可以判断该按钮正常工作,因为

System.out.println("----- FRAME WINDOWS -----"); System.out.println(“ ----- FRAME WINDOWS -----”);

updates and lists a new frame every time I click. 每次单击都会更新并列出新框架。

There has to be something terribly simple. 必须有非常简单的东西。

Also I would like to say that there is another class I left out here called Clock.java. 我还要说的是,我在这里遗漏了另一个名为Clock.java的类。

This does have a timer but I'm not sure if that was interrupting anything. 这确实有一个计时器,但是我不确定这是否在打断任何东西。

EDIT: I guess this is what you mean? 编辑:我猜这是你的意思吗?

 public class bNextMonth_Method implements ActionListener{
    private Main main;

    public bNextMonth_Method(Main main) {
        this.main = main;
    }
    public void actionPerformed (ActionEvent e)
    {
        if (clickm == 11)
        {
            clicky = clicky + 1;
            clickm = 0;
            System.out.println(clickm);
            System.out.println(clicky);
            main.makeCenter(clicky, clickm);
            main.makeNorth();
            main.makeEast();
        }
        else
        {
            clickm = clickm + 1;
            System.out.println(clickm);
            System.out.println(clicky);
            Main m = new Main();
            main.makeCenter(clicky, clickm);
            main.makeNorth();
            main.makeEast();
        }
    }
}

That was my best interpretation but that throws 那是我最好的解释,但是

bNextYear_Method(Main) in bNextYear_Method cannot be applied to () bNextYear_Method中的bNextYear_Method(Main)不能应用于()

now i know what that means but how do I go about fixing it? 现在我知道这意味着什么,但是我该如何解决呢?

Look what you're doing with Main inside of the ActionListener -- you're creating a completely new and different Main inside of this method! 看看您在ActionListener内部使用Main做什么—您正在此方法内部创建一个全新的 ,不同的Main!

public void actionPerformed (ActionEvent e)
{
    if (clickm == 11)
    {
        clicky = clicky + 1;
        clickm = 0;
        System.out.println(clickm);
        System.out.println(clicky);
        Main m = new Main();   // ********* here **************
        m.makeCenter(clicky, clickm);
        m.makeNorth();
        m.makeEast();
    }

This Main has nothing to do with the one that is being displayed. 此Main与正在显示的Main没有任何关系。 Any changes made to the state of this newly created Main object will not be reflected in the displayed one because they are completely different unique objects. 对这个新创建的主对象的状态所做的任何更改都不会反映在显示的对象中,因为它们是完全不同的唯一对象。

Solution: don't do this. 解决方案:不要这样做。 Pass in a valid reference to the real Main object into the listener and call methods on it. 将对真实 Main对象的有效引用传递给侦听器并在其上调用方法。

public class MyListener implements ActionListener {
  private Main main;

  public MyListener(Main main) {
    this.main = main;
  }

  public void actionPerformed(ActionEvent e) {
     // now in here we can call methods on the main variable
     // and it will be called on the actual displayed main
  }
}

Edit 编辑

Now when you call the listener's constructor you must pass the correct parameter into it. 现在,当您调用侦听器的构造函数时,必须将正确的参数传递给它。 Also pleas study and follow Java naming conventions. 还请研究并遵循Java命名约定。 All class names should start with an upper-case letter. 所有类名都应以大写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM