Java 调用从另一个JPanel中执行的操作获得的实例变量
我用cardLayout创建了一个JFrame,第一个可见的JPanel有一个Jbutton,我添加了一个动作监听器来执行一个动作。该操作创建了一个字符串变量“hhhh”,我想在另一个JPanel中使用它。这就是我在做什么时遇到的问题Java 调用从另一个JPanel中执行的操作获得的实例变量,java,swing,Java,Swing,我用cardLayout创建了一个JFrame,第一个可见的JPanel有一个Jbutton,我添加了一个动作监听器来执行一个动作。该操作创建了一个字符串变量“hhhh”,我想在另一个JPanel中使用它。这就是我在做什么时遇到的问题 Class 1 import java.awt.*; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; impor
Class 1
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class NHome extends JFrame{
JPanel Bucket= new JPanel(), Start= new JPanel(), Cashier = new csView(), Manager = new JPanel();
JButton stbtn= new JButton("Start"), mnbtn= new JButton("Manager"), csbtn= new JButton("Cashier");
CardLayout cl= new CardLayout();
private final JTextField textField = new JTextField();
private JPasswordField passwordField;
public NHome() {
textField.setBounds(322, 141, 158, 31);
textField.setColumns(10);
Bucket.setLayout(cl);
Bucket.add(Start, "1");
Bucket.add(Cashier, "2");
Bucket.add(Manager, "3");
Start.setLayout(null);
stbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cl.show(Bucket, "2");
/*
* I want to use this value of this String in the another class (csView)
*/
String hhhhh=new String("Peter");
System.out.println(hhhhh);
}
});
stbtn.setBounds(353, 245, 76, 23);
Start.add(stbtn);
Start.add(textField);
passwordField = new JPasswordField();
passwordField.setBounds(322, 183, 158, 31);
Start.add(passwordField);
Cashier.setLayout(null);
csbtn.setBounds(197, 139, 116, 23);
Cashier.add(csbtn);
Manager.setBackground(Color.BLUE);
Manager.add(mnbtn);
cl.show(Bucket, "1");
setTitle("NOVA PHARM");
getContentPane().add(Bucket);
setBounds(300, 300, 566, 482);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new CardLayout(5, 5));
setResizable(true);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
NHome window = new NHome();
window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
/*
*this is the second class where I want to use the variable
*/
第二类
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
public class csView extends JPanel {
/**
* Create the panel.
*/
public csView() {
setLayout(null);
/**
* I want to display the String hhhhh in the JLabel Uniqlbl below
*
*/
JLabel Uniqlbl = new JLabel("Cashier Name:");
Uniqlbl.setFont(new Font("Tahoma", Font.PLAIN, 16));
Uniqlbl.setBounds(227, 62, 253, 46);
add(Uniqlbl);
}
}
一种方法是在NHome类中创建一个充当全局变量的静态变量。这意味着than csView将能够读取和写入变量 这是一个如何实施的示例:
JPanel Bucket = new JPanel(), Start = new JPanel(), Cashier = new csView(), Manager = new JPanel();
JButton stbtn = new JButton("Start"), mnbtn = new JButton("Manager"), csbtn = new JButton("Cashier");
CardLayout cl = new CardLayout();
private final JTextField textField = new JTextField();
private JPasswordField passwordField;
static String hhhhh; //create the variable here
public NHome() {
textField.setBounds(322, 141, 158, 31);
textField.setColumns(10);
Bucket.setLayout(cl);
Bucket.add(Start, "1");
Bucket.add(Cashier, "2");
Bucket.add(Manager, "3");
Start.setLayout(null);
stbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cl.show(Bucket, "2");
/*
* I want to use this value of this String in the another class (csView)
*/
hhhhh = new String("Peter"); //set the value here
System.out.println(hhhhh);
}
});
因此,您可以在csView中实现它,方法是:
JLabel Uniqlbl = new JLabel(NHome.hhhhh);
尝试修改csView
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
public class csView extends JPanel {
// Declare those variables here
String hhhhh;
JLabel Uniqlbl;
public csView() {
setLayout(null);
Uniqlbl = new JLabel("Cashier Name:");
Uniqlbl.setFont(new Font("Tahoma", Font.PLAIN, 16));
Uniqlbl.setBounds(227, 62, 253, 46);
add(Uniqlbl);
}
// Add a setter here
public void setHhhhh(String hhhhh) {
this.hhhhh = hhhhh;
// Edit: Add this line to update the Uniqlbl text
Uniqlbl.setText(hhhhh);
}
}
在ActionListener中调用sethhhh()方法
我建议您不要在csView构造函数中使用“hhhh”变量,否则可能会遇到NullPointerException。或者,如果您想这样做,首先像这样初始化“hhhh”变量
String hhhhh = "";
我们能否停止推荐
静态
作为跨班级交流的解决方案。更好的解决方案可能是在两个类和/或观察者模式之间使用模型<代码>静态是一个不好的主意,它会鼓励坏的和懒惰的人habits@MadProgrammer是的,我知道你是从哪里来的。在再次推荐之前,我会考虑这一点。公平地说,她是否想养成这些习惯取决于OP。她可能更想解决这个问题,而不是考虑该方法的长期后果。抱歉,该值给了我null@Benjamin lowry如果您不初始化静态变量,并且它没有通过actionPerformed方法,那么当您在另一个类中调用它时,它将为null。我建议用“默认”或“没有名字”之类的东西初始化它,首先看一下。其想法是创建一个模型,该模型使用null
layouts来维护需要在viewsAvoid之间共享的结构,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计宗旨是与布局管理器一起工作,丢弃这些布局管理器将导致无休止的问题,您将花费越来越多的时间来纠正这些问题。您可能希望通读一遍,这将使人们更容易阅读您的代码,也使您更容易阅读其他布局管理器。如果此问题得到解决,请表示此问题已解决。这样做还可以获得2点声誉积分。当我运行csView时,它仍然不会在csView中显示“hhhh”的值@ALEB2000请尝试使用已编辑的代码。我忘记调用Uniqlbl
的setText()
方法来更新标签中的文本,现在应该可以了@哈里特巴尼
String hhhhh = "";