在java中使用textfields更改类的详细信息

在java中使用textfields更改类的详细信息,java,Java,我需要使用java中的textfields更改类属性值的帮助,下面我有3个简化版本的代码来演示我遇到的问题。我在alpha、UIalpha和mainGUI类下面有3个类。当我运行mainGUI类时,我需要用户单击窗口上的pc映像。这将创建打开新框架的UIaplha对象。此框架有一个textfield,它应该能够更改mainGUI类中aplha类对象的name属性。我的代码工作到某一点,我不知道如何修复它。我已经搜索了很多次,尝试了很多种方法,但是没有一种对我有效 Alpha.java publi

我需要使用java中的textfields更改类属性值的帮助,下面我有3个简化版本的代码来演示我遇到的问题。我在alpha、UIalpha和mainGUI类下面有3个类。当我运行mainGUI类时,我需要用户单击窗口上的pc映像。这将创建打开新框架的UIaplha对象。此框架有一个textfield,它应该能够更改mainGUI类中aplha类对象的name属性。我的代码工作到某一点,我不知道如何修复它。我已经搜索了很多次,尝试了很多种方法,但是没有一种对我有效

Alpha.java

public class alpha{
String name;
public void setName(String n){
  name=n;
  }
  public void print(){
    System.out.println("name= "+name);
  }
  public static void main(String[] args){
    alpha a=new alpha();
  }
}
import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; 
import javax.swing.*;
public class mainGUI extends JComponent{
 private static final int WIDTH = 400;
 private static final int HEIGHT = 300;
 private int PCHeight=40;
 private int PCWidth=41;
 int y=20,x=30;
 private JButton pc1button;
 private PCButtonHandler pcbHandler;
 public mainGUI(){
   JFrame frame=new JFrame("NETWORK EXAMPLE");
   ImageIcon image = new ImageIcon("pc.png");
   pc1button =new JButton(image);
  //////////////////////////////////////////////////////////
   pc1button.setOpaque(false);
   pc1button.setContentAreaFilled(false);
   pc1button.setBorderPainted(false);
   pc1button.setBounds(y,x,PCHeight,PCWidth);
  ////////////////////////////////////////////////////////
   pcbHandler=new PCButtonHandler();
   pc1button.addActionListener(pcbHandler);
   frame.setTitle("GUI example");
   Container pane= frame.getContentPane();
   pane.setLayout(null);
   pane.setBackground(Color.WHITE);
   pane.add(pc1button);
   frame.setSize(WIDTH, HEIGHT);
   frame.setVisible(true);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 public class PCButtonHandler implements ActionListener
 {
   String name;
    public void actionPerformed(ActionEvent e)
    {
      alpha test=new alpha();
      test.setName("WRONG");
      test.print();
      UIalpha help=new UIalpha(test);
      test=help.updateObjects();
      test.print();
    }
  }
 public static void main(String[] args){
   mainGUI work=new mainGUI();
 }
}
import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; 
import javax.swing.*;
public class UIalpha extends JFrame{
  String Name;
  private JLabel nameL;
  private JTextField nameTF;
  boolean pressed=false;
  private JButton enterbutton=new JButton("Enter");
  private JFrame frame=new JFrame("PC");
  alpha refrence;
  public UIalpha(alpha test){
    this.refrence=test;
   nameL= new JLabel("Enter the name of alpha: ", SwingConstants.CENTER);
   nameTF=new JTextField(10);
   nameTF.setMaximumSize(new Dimension(500,20));
   enterbutton.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e) {
       refrence.setName(nameTF.getText());
    //   test.setName(nameTF.getText());
       //test.print();
       pressed=true;
     }
   });
   Container pane= frame.getContentPane();
   frame.setLayout(new BoxLayout(pane,BoxLayout.PAGE_AXIS));
   pane.add(nameL);
   pane.add(nameTF);
   pane.add(enterbutton);
   frame.setMinimumSize(new Dimension(300,200));
   frame.setPreferredSize(new Dimension(600,200));
   frame.setMaximumSize(new Dimension(660,200));
   frame.setVisible(true);
  }
  public alpha updateObjects(){

   return refrence;
  }
}
mainGUI.java

public class alpha{
String name;
public void setName(String n){
  name=n;
  }
  public void print(){
    System.out.println("name= "+name);
  }
  public static void main(String[] args){
    alpha a=new alpha();
  }
}
import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; 
import javax.swing.*;
public class mainGUI extends JComponent{
 private static final int WIDTH = 400;
 private static final int HEIGHT = 300;
 private int PCHeight=40;
 private int PCWidth=41;
 int y=20,x=30;
 private JButton pc1button;
 private PCButtonHandler pcbHandler;
 public mainGUI(){
   JFrame frame=new JFrame("NETWORK EXAMPLE");
   ImageIcon image = new ImageIcon("pc.png");
   pc1button =new JButton(image);
  //////////////////////////////////////////////////////////
   pc1button.setOpaque(false);
   pc1button.setContentAreaFilled(false);
   pc1button.setBorderPainted(false);
   pc1button.setBounds(y,x,PCHeight,PCWidth);
  ////////////////////////////////////////////////////////
   pcbHandler=new PCButtonHandler();
   pc1button.addActionListener(pcbHandler);
   frame.setTitle("GUI example");
   Container pane= frame.getContentPane();
   pane.setLayout(null);
   pane.setBackground(Color.WHITE);
   pane.add(pc1button);
   frame.setSize(WIDTH, HEIGHT);
   frame.setVisible(true);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 public class PCButtonHandler implements ActionListener
 {
   String name;
    public void actionPerformed(ActionEvent e)
    {
      alpha test=new alpha();
      test.setName("WRONG");
      test.print();
      UIalpha help=new UIalpha(test);
      test=help.updateObjects();
      test.print();
    }
  }
 public static void main(String[] args){
   mainGUI work=new mainGUI();
 }
}
import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; 
import javax.swing.*;
public class UIalpha extends JFrame{
  String Name;
  private JLabel nameL;
  private JTextField nameTF;
  boolean pressed=false;
  private JButton enterbutton=new JButton("Enter");
  private JFrame frame=new JFrame("PC");
  alpha refrence;
  public UIalpha(alpha test){
    this.refrence=test;
   nameL= new JLabel("Enter the name of alpha: ", SwingConstants.CENTER);
   nameTF=new JTextField(10);
   nameTF.setMaximumSize(new Dimension(500,20));
   enterbutton.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e) {
       refrence.setName(nameTF.getText());
    //   test.setName(nameTF.getText());
       //test.print();
       pressed=true;
     }
   });
   Container pane= frame.getContentPane();
   frame.setLayout(new BoxLayout(pane,BoxLayout.PAGE_AXIS));
   pane.add(nameL);
   pane.add(nameTF);
   pane.add(enterbutton);
   frame.setMinimumSize(new Dimension(300,200));
   frame.setPreferredSize(new Dimension(600,200));
   frame.setMaximumSize(new Dimension(660,200));
   frame.setVisible(true);
  }
  public alpha updateObjects(){

   return refrence;
  }
}
UIalpha.java

public class alpha{
String name;
public void setName(String n){
  name=n;
  }
  public void print(){
    System.out.println("name= "+name);
  }
  public static void main(String[] args){
    alpha a=new alpha();
  }
}
import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; 
import javax.swing.*;
public class mainGUI extends JComponent{
 private static final int WIDTH = 400;
 private static final int HEIGHT = 300;
 private int PCHeight=40;
 private int PCWidth=41;
 int y=20,x=30;
 private JButton pc1button;
 private PCButtonHandler pcbHandler;
 public mainGUI(){
   JFrame frame=new JFrame("NETWORK EXAMPLE");
   ImageIcon image = new ImageIcon("pc.png");
   pc1button =new JButton(image);
  //////////////////////////////////////////////////////////
   pc1button.setOpaque(false);
   pc1button.setContentAreaFilled(false);
   pc1button.setBorderPainted(false);
   pc1button.setBounds(y,x,PCHeight,PCWidth);
  ////////////////////////////////////////////////////////
   pcbHandler=new PCButtonHandler();
   pc1button.addActionListener(pcbHandler);
   frame.setTitle("GUI example");
   Container pane= frame.getContentPane();
   pane.setLayout(null);
   pane.setBackground(Color.WHITE);
   pane.add(pc1button);
   frame.setSize(WIDTH, HEIGHT);
   frame.setVisible(true);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 public class PCButtonHandler implements ActionListener
 {
   String name;
    public void actionPerformed(ActionEvent e)
    {
      alpha test=new alpha();
      test.setName("WRONG");
      test.print();
      UIalpha help=new UIalpha(test);
      test=help.updateObjects();
      test.print();
    }
  }
 public static void main(String[] args){
   mainGUI work=new mainGUI();
 }
}
import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; 
import javax.swing.*;
public class UIalpha extends JFrame{
  String Name;
  private JLabel nameL;
  private JTextField nameTF;
  boolean pressed=false;
  private JButton enterbutton=new JButton("Enter");
  private JFrame frame=new JFrame("PC");
  alpha refrence;
  public UIalpha(alpha test){
    this.refrence=test;
   nameL= new JLabel("Enter the name of alpha: ", SwingConstants.CENTER);
   nameTF=new JTextField(10);
   nameTF.setMaximumSize(new Dimension(500,20));
   enterbutton.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e) {
       refrence.setName(nameTF.getText());
    //   test.setName(nameTF.getText());
       //test.print();
       pressed=true;
     }
   });
   Container pane= frame.getContentPane();
   frame.setLayout(new BoxLayout(pane,BoxLayout.PAGE_AXIS));
   pane.add(nameL);
   pane.add(nameTF);
   pane.add(enterbutton);
   frame.setMinimumSize(new Dimension(300,200));
   frame.setPreferredSize(new Dimension(600,200));
   frame.setMaximumSize(new Dimension(660,200));
   frame.setVisible(true);
  }
  public alpha updateObjects(){

   return refrence;
  }
}

事实上,你的代码大部分是有效的。呸,alpha的变化反映在mainGUI中。只是你试图过早地打印它-创建一个新的框架不是阻塞。在您有机会输入新值之前,
PCButtonHandler
中的两个
打印都会立即进行。比如说,如果您在mainGUI中添加一个新按钮来打印alpha的当前值,您将能够看到它的更改

还有,既然我在这里:

  • Java类名应该以大写字母开头
  • 为了在应用程序增长时不致发疯,请使用布局管理器(例如MigLayout)
-编辑-

当窗口关闭时,可以尝试使用新值做一些事情(例如,在需要时更新整个mainGUI)。您可以在下面的mainGUI中这样做,创建UIalpha:

help.addWindowListener(new WindowAdapter(){
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("window closing");
                    test.print();
                }
            });
或者,当你修复你的UIalpha时,这应该会起作用:

  • 删除
    变量
  • 无论在何处调用该变量上的方法,都应改为在UIalpha上调用它(当前对象)

因为否则,您将把侦听器设置在与您使用的不同的框架上。

事实上,您的代码大部分都是有效的。呸,alpha的变化反映在mainGUI中。只是你试图过早地打印它-创建一个新的框架不是阻塞。在您有机会输入新值之前,
PCButtonHandler
中的两个
打印都会立即进行。比如说,如果您在mainGUI中添加一个新按钮来打印alpha的当前值,您将能够看到它的更改

还有,既然我在这里:

  • Java类名应该以大写字母开头
  • 为了在应用程序增长时不致发疯,请使用布局管理器(例如MigLayout)
-编辑-

当窗口关闭时,可以尝试使用新值做一些事情(例如,在需要时更新整个mainGUI)。您可以在下面的mainGUI中这样做,创建UIalpha:

help.addWindowListener(new WindowAdapter(){
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("window closing");
                    test.print();
                }
            });
或者,当你修复你的UIalpha时,这应该会起作用:

  • 删除
    变量
  • 无论在何处调用该变量上的方法,都应改为在UIalpha上调用它(当前对象)

因为否则,您将把侦听器设置在不同于您使用的帧上。

我的代码工作到某个特定点,我不知道如何修复它。
这不是很具体。什么实际起作用,什么不起作用?因此,当我按下enter按钮时,它会更改传入的aplha对象。但是这个变化并没有反映回mainGUI类中的alpha对象。我试图实现的是允许用户按下enter按钮,然后我可以在按下后在mainGUI类中使用更新对象。
我的代码工作到某个点,我不知道如何修复它。
这不是很具体。什么实际起作用,什么不起作用?因此,当我按下enter按钮时,它会更改传入的aplha对象。但是这个变化并没有反映回mainGUI类中的alpha对象。我试图实现的是允许用户按下enter按钮,然后我可以在按下按钮后在mainGUI类中使用更新对象。除了按钮之外,还有其他方法可以看到更改被反映出来吗。我怎样才能等到按下回车按钮。我尝试了类似while(enterpressed==false)的方法;但这并没有给我发短信和按钮。我也尝试过使用线程,但我不知道如何使用它们,因为在尝试这种事情之前我只做过基本的java。@DillonWaters扩展了answer@DillonWaters如果答案解决了你的问题,你可以自由地接受它。除了一个按钮之外,还有其他方法可以看到变化的反映吗。我怎样才能等到按下回车按钮。我尝试了类似while(enterpressed==false)的方法;但这并没有给我发短信和按钮。我也尝试过使用线程,但我不知道如何使用它们,因为在尝试这种事情之前我只做过基本的java。@DillonWaters扩展了answer@DillonWaters如果答案解决了你的问题,你可以自由地接受它