Java 帧复制但未应用更改

Java Frame duplicated without applying change

我有 2 个来自不同 class 的框架,一个是仅包含 jbutton 和 jtextfield 的主框架,另一个包含更改第一帧背景颜色的设置,所以我我遇到了一个问题,因为当我单击“确定”按钮时,颜色发生了变化,但它创建了一个具有新更改的新主框架,而不是在活动主框架中应用更改。

这里是主要框架代码

package com.srccodes.example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Cheb extends JFrame implements ActionListener {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Cheb cheb = new Cheb();

}

JTextArea about = new JTextArea(3,30);

JButton callB = new JButton("Settings");
Cheb(){
    setLayout(new FlowLayout(FlowLayout.CENTER, 20,25));
    setSize(400,400);
    setTitle("Check Box");
    

    add(new Label("About you"));
    JScrollPane aboutScroll= new JScrollPane(about);
    add(aboutScroll);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(callB);
    setVisible(true);
    callB.addActionListener(this);
    
    
}


public void actionPerformed(ActionEvent e)
{
    
     
    //Countinent selection with Check Box
     if(e.getSource() == callB)
     {
         CollorClass cl =new CollorClass();
     }
    
    
}

}

这里是修改主框颜色的代码

package com.srccodes.example;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class CollorClass extends JFrame implements ActionListener {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    CollorClass cc = new CollorClass();

}
 JLabel bg = new JLabel("Background Color"); 
 String colorName[] = {"None","RED","GREEN","BLACK","BLUE","YELLOW","WHITE"};
 JComboBox bgColor = new JComboBox(colorName);
 JButton ok = new JButton("OK");
CollorClass()
{
    setLayout(new FlowLayout(FlowLayout.CENTER, 20,25));
    setSize(400,400);
    setTitle("Check Box");
    add(bg);
    add(bgColor);
    add(ok);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    ok.addActionListener(this);
    
}
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    
    if(e.getSource() == ok)
    {
        Cheb cheb = new Cheb();
        String SelectedValue = bgColor.getSelectedItem().toString();
        switch (SelectedValue){
            case "RED" :
                cheb.about.setBackground(Color.red);
                System.out.print(SelectedValue);
                break;
            case "YELLOW" :
                cheb.about.setBackground(Color.yellow);
                System.out.print(SelectedValue);
                break;
            case "BLUE" :
                cheb.about.setBackground(Color.blue);
                System.out.print(SelectedValue);
                break;
        
            case "GREEN" :
                cheb.about.setBackground(Color.green);
                System.out.print(SelectedValue);
                break;
            case "BLACK" :
                cheb.about.setBackground(Color.black);
                System.out.print(SelectedValue);
                break;
            case "WHITE" :
                cheb.about.setBackground(Color.white);
                System.out.print(SelectedValue);
                break;
        
        
            default:
                
                System.out.print(SelectedValue);
        }
    }
    
}

}

这里的问题是,当您 select 一种颜色时,您正在调用 Cheb class 的一个新实例。所以你不是在改变初始 window 的颜色,而是改变新 window.

的颜色

正是 CollorClassactionPerformed 方法中的这一行导致了问题:

Cheb cheb = new Cheb();

您可能想要做的是将 Cheb class 作为依赖项注入 CollorClass。这样,您可以调用方法并获取初始 Cheb class.

的属性

一种方法如下(省略号“...”点用于隐藏不必要的代码):

颜色类别:

public class CollorClass extends JFrame implements ActionListener {

    private final Cheb cheb;

    ...

    CollorClass(Cheb cheb) // important addition
    {
        this.cheb = cheb; // important addition

        setLayout(new FlowLayout(FlowLayout.CENTER, 20,25));
        setSize(400,400);
        setTitle("Check Box");
        add(bg);
        add(bgColor);
        add(ok);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        ok.addActionListener(this);

    }

    ...
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == ok)
        {
            // No instantiation of new Cheb here anymore.
            // Using the dependency instead.

            String SelectedValue = bgColor.getSelectedItem().toString();
            switch (SelectedValue){
                case "RED" :
                    cheb.about.setBackground(Color.red);
                    System.out.print(SelectedValue);
                    break;
                case "YELLOW" :

                ...
        }

切布:

public class Cheb extends JFrame implements ActionListener {

    ...

    public void actionPerformed(ActionEvent e)
    {


        //Countinent selection with Check Box
        if(e.getSource() == callB)
        {
            CollorClass cl = new CollorClass(this); // Pass this Cheb instance as argument
        }


    }

希望清楚。它仍然可以使用改进(例如,关闭一个 window 将关闭两个 windows),但这可能是下一个需要解决的挑战!