我有 2 個來自不同類的框架,一個是只包含一個 jbutton 和 jtextfield 的主框架,另一個包含一個更改第一框架背景顏色的設定,所以我遇到了一個問題,因為當我點擊按鈕 OK 按鈕顏色發生了變化,但它創建了一個新的主框架,其中包含新的更改,而不是在活動主框架中應用更改。
這是主框架代碼
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);
}
}
}
}
uj5u.com熱心網友回復:
Cheb這里的問題是,當您選擇一種顏色時,您正在呼叫該類的一個新實體。因此,您不會更改初始視窗的顏色,而是更改新視窗的顏色。
正是CollorClass'sactionPerformed方法中的這一行導致了問題:
Cheb cheb = new Cheb();
相反,您可能想要做的是將Cheb類作為依賴項注入到CollorClass. 這樣,您可以呼叫方法并獲取初始Cheb類的屬性。
一種方法如下(省略號“...”點用于隱藏不必要的代碼):
顏色類:
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
}
}
希望這很清楚。它仍然可以使用改進(例如關閉一個視窗將關閉兩個視窗),但這可能是下一個挑戰!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/467668.html
