Java框架复制而无需应用更改

发布于 2025-01-25 05:35:14 字数 3490 浏览 2 评论 0原文

我有2个来自不同类的帧,一个是只包含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);
        }
    }
    
}

}

I have 2 frames from different class, one is the main frame containing just a jbutton and jtextfield, and the other one contains a the setting to change the background color of the first frame, so i've a problem because when i clicked the the button OK button the color changed but it created a new main frame with new change instead of applying the change in the active main frame.

here is the main frame code

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

}

here is the code to change the color of the main frame

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2025-02-01 05:35:14

这里的问题是,当您选择颜色时,您正在调用cheb类的新实例。因此,您不会更改初始窗口的颜色,而是在新窗口的颜色。

collorClass's ActionPerformed方法中,该行特别是引起问题的方法:

Cheb cheb = new Cheb();

您可能想做的就是注入cheb 将类作为collorClass的依赖项。这样,您可以调用方法并进入初始cheb类的属性。

一种方法可能是如下(省略号的……'点用于隐藏不必要的代码):

CollorClass:

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" :

                ...
        }

Cheb: cheb:

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
        }


    }

希望它很清楚。它仍然可以使用改进(例如关闭一个窗口将关闭两个窗口),但这可能是要弄清楚的下一个挑战!

The issue here is, you are invoking a new instance of the Cheb class when you select a color. So you are not changing the color of the initial window, but of the new window.

It is specifically this line in the CollorClass's actionPerformed method that is causing the issue:

Cheb cheb = new Cheb();

What you instead might want to do is inject the Cheb class as a dependency into the CollorClass. This way, you can invoke methods and get to properties of the initial Cheb class.

One way of doing that could be as follows (the ellipsis '...' dots are used to hide unnecessary code):

CollorClass:

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" :

                ...
        }

Cheb:

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
        }


    }

Hope that it's clear. It still could use improvements (e.g. closing one window will close both windows), but that might be a next challenge to figure out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文