簡體   English   中英

單擊不同類別的按鈕時更新JFrame

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

當前,嘗試使用新信息更新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);
    }
}

這是用來切換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();
        }
    }
}

現在,您會注意到在makeCenter方法的第一部分代碼中,注釋掉了。 如果我取消評論,則信息會更改,但每次都會創建一個新窗口。

現在即使dispose()當前不存在,我也將其放在makeCenter方法中,因為那是從按鈕中調用的。 我可以判斷該按鈕正常工作,因為

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

每次單擊都會更新並列出新框架。

必須有非常簡單的東西。

我還要說的是,我在這里遺漏了另一個名為Clock.java的類。

這確實有一個計時器,但是我不確定這是否在打斷任何東西。

編輯:我猜這是你的意思嗎?

 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();
        }
    }
}

那是我最好的解釋,但是

bNextYear_Method中的bNextYear_Method(Main)不能應用於()

現在我知道這意味着什么,但是我該如何解決呢?

看看您在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();
    }

此Main與正在顯示的Main沒有任何關系。 對這個新創建的主對象的狀態所做的任何更改都不會反映在顯示的對象中,因為它們是完全不同的唯一對象。

解決方案:不要這樣做。 將對真實 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
  }
}

編輯

現在,當您調用偵聽器的構造函數時,必須將正確的參數傳遞給它。 還請研究並遵循Java命名約定。 所有類名都應以大寫字母開頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM