以下是我的菜单类。我想在单击“保存”按钮时从masterdetails类的文本字段中获取值。我该怎么办? 我已经尝试创建第二个类的对象并使用getText()来检索数据,但我无法以某种方式获取它
import java.awt.*;
import javax.swing.*;
import javax.swing.JOptionPane.*;
import java.awt.event.*;
public class menu extends JFrame implements ActionListener
{
public JMenu jb1,jb7,jb8;
public JPanel panel;
public ImageIcon i,j;
public JFrame jf;
private JMenuBar menubar=new JMenuBar();
String str = null;
String str1=null;
public menu()
{
setTitle("Main_Screen");
setJMenuBar(menubar);
jb1=new JMenu("File");`enter code here`
jb7=new JMenu("Details");
jb8=new JMenu("Report");
JMenuItem e=new JMenuItem("Exit");
menubar.add(jb1);
menubar.add(jb7);
menubar.add(jb8);
JMenuItem jm1=new JMenuItem("Save");
jb1.add(jm1);
JMenuItem jm3=new JMenuItem("Save As");
jb1.add(jm3);
JMenuItem jm4=new JMenuItem("Update");
jb1.add(jm4);
JMenuItem jm5=new JMenuItem("Retrieve");
jb1.add(jm5);
JMenuItem jm6=new JMenuItem("Exit");
jb1.add(jm6);
JMenuItem jm8=new JMenuItem("Master Details");
jb7.add(jm8);
JMenuItem jm12=new JMenuItem("Performance Testing");
jb7.add(jm12);
JMenuItem jm13=new JMenuItem("Raw Materials");
jb7.add(jm13);
JMenuItem jm81=new JMenuItem("Generate");
jb8.add(jm81);
panel=(JPanel)getContentPane( );
panel.setLayout(new BorderLayout());
JLabel lb1=new JLabel("",i,JLabel.CENTER);
panel.add(lb1,BorderLayout.CENTER);
e.addActionListener(this);
jm1.addActionListener(this);
jm3.addActionListener(this);
jm4.addActionListener(this);
jm5.addActionListener(this);
jm6.addActionListener(this);
jm13.addActionListener(this);
jm12.addActionListener(this);
jm8.addActionListener(this);
jm81.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
String str=a.getActionCommand();
if(str.equals("Master Details"))
{
masterdetails f=new masterdetails();
f.setSize(800,600);
f.setVisible(true);
f.show();
}
if(str.equals("Save"))
{
masterdetails m=new masterdetails();
//m.setSize(800,600);
//m.setVisible(true);
//m.show();
}
}
public static void main(String args[])
{
menu m=new menu();
m.setSize(800,600);
m.setVisible(true);
m.show();
}
}
Master详细信息类如下所示。
import java.awt.Font;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.awt.*;
import javax.swing.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.wb.swt.SWTResourceManager;
public class masterdetails extends JFrame implements ActionListener {
public JPanel panel;
public JLabel title,lblnumber,lblPSINo,lblmodel,lblcapacity,lblpressure,lblplungerdia;
public JLabel lblnostrk,lblclient,lblpono,lblsize,lblstrokelen;
public JLabel lblmotorhp,lblmotorrpm,lblmotorno,lblmake,lblpumptagno,lblitemno,lbldate;
public JButton b1;
public String number1="";
public JTextField number,PSINo,model,capacity,pressure,plungerdia;
public JTextField nostrk,client,pono,size,strokelen;
public JTextField motorhp,motorrpm,motorno,make,pumptagno,itemno;
DateTime date;
Font g;
String str=null;
public masterdetails()
{ g=new Font("",Font.BOLD,18);
panel=(JPanel)getContentPane();
panel.setLayout(null);
title=new JLabel("Master Details");
title.setFont(g);
title.setBounds(325, 10, 150, 40);
panel.add(title);
lblnumber=new JLabel("No.");
lblnumber.setBounds(10, 57, 87, 21);
number=new JTextField("");
number.setBounds(121, 57, 116, 21);
panel.add(lblnumber);
panel.add(number);
lbldate=new JLabel("Date");
lbldate.setBounds(10, 97, 87, 21);
panel.add(lbldate);
lblPSINo=new JLabel("Pump SI No.");
PSINo=new JTextField("");
PSINo.setBounds(121, 137, 116, 21);
lblPSINo.setBounds(10, 137, 87, 21);
panel.add(lblPSINo);
panel.add(PSINo);
lblmodel=new JLabel("Model");
model=new JTextField("");
model.setBounds(121, 175, 116, 21);
lblmodel.setBounds(10, 177, 87, 21);
panel.add(lblmodel);
panel.add(model);
lblcapacity=new JLabel("Capacity");
capacity=new JTextField("");
lblcapacity.setBounds(10, 217, 87, 21);
capacity.setBounds(121, 213, 116, 21);
panel.add(lblcapacity);
panel.add(capacity);
lblpressure=new JLabel("Pressure");
pressure=new JTextField("");
lblpressure.setBounds(10, 257, 87, 21);
pressure.setBounds(121, 252, 116, 21);
panel.add(lblpressure);
panel.add(pressure);
lblplungerdia=new JLabel("Plunger Dia.");
plungerdia=new JTextField("");
lblplungerdia.setBounds(10, 297, 87, 21);
plungerdia.setBounds(121, 291, 116, 21);
panel.add(lblplungerdia);
panel.add(plungerdia);
lblnostrk=new JLabel("No.of Strk/Min");
nostrk=new JTextField("");
lblnostrk.setBounds(10, 337, 87, 21);
nostrk.setBounds(121, 330, 116, 21);
panel.add(nostrk);
panel.add(lblnostrk);
lblpumptagno=new JLabel("Pump Tag No.");
pumptagno=new JTextField("");
pumptagno.setBounds(121, 369, 116, 21);
panel.add(pumptagno);
lblpumptagno.setBounds(10, 369, 87, 21);
panel.add(lblpumptagno);
lblclient=new JLabel("Client.");
client=new JTextField("");
client.setBounds(430, 57, 214, 21);
panel.add(client);
lblclient.setBounds(335, 57, 87, 21);
panel.add(lblclient);
lblpono=new JLabel("P.O. No.");
pono=new JTextField("");
pono.setBounds(430, 96, 214, 21);
panel.add(pono);
lblpono.setBounds(335, 96, 87, 21);
panel.add(lblpono);
lblsize=new JLabel("Size");
size=new JTextField("");
size.setBounds(430, 137, 214, 21);
panel.add(size);
lblsize.setBounds(335, 137, 87, 21);
panel.add(lblsize);
lblstrokelen=new JLabel("Stroke Length");
strokelen=new JTextField("");
strokelen.setBounds(430, 175, 214, 21);
panel.add(strokelen);
lblstrokelen.setBounds(335, 175, 87, 21);
panel.add(lblstrokelen);
lblmotorhp=new JLabel("Motor H.P.");
motorhp=new JTextField("");
motorhp.setBounds(430, 213, 214, 21);
panel.add(motorhp);
lblmotorhp.setBounds(335, 213, 87, 21);
panel.add(lblmotorhp);
lblmotorrpm=new JLabel("Motor RPM");
motorrpm=new JTextField("");
motorrpm.setBounds(430, 252, 214, 21);
panel.add(motorrpm);
lblmotorrpm.setBounds(335, 252, 87, 21);
panel.add(lblmotorrpm);
lblmotorno=new JLabel("Motor No.");
motorno=new JTextField("");
motorno.setBounds(430, 291, 214, 21);
panel.add(motorno);
lblmotorno.setBounds(335, 291, 87, 21);
panel.add(lblmotorno);
lblmake=new JLabel("Make");
make=new JTextField("");
make.setBounds(430, 330, 214, 21);
panel.add(make);
lblmake.setBounds(335, 330, 87, 21);
panel.add(lblmake);
lblitemno=new JLabel("Item No.");
itemno=new JTextField("");
itemno.setBounds(430, 369, 214, 21);
panel.add(itemno);
lblitemno.setBounds(335, 369, 87, 21);
panel.add(lblitemno);
b1=new JButton("Next");
b1.setBounds(671, 507, 75, 25);
panel.add(b1);
b1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent a) {
// TODO Auto-generated method stub
String str=a.getActionCommand();
if(str.equals("Next"))
{
performancetest f=new performancetest();
f.setSize(800,600);
f.setVisible(true);
f.show();
}
}
public static void main(String args[])
{
masterdetails f=new masterdetails();
f.setVisible(true);
f.setSize(800,600);
f.show();
}
}
答案 0 :(得分:1)
为了保持封装,我建议你在具有Textfield
的类中为有问题的String创建一个getter。此方法应该是公开的,只返回Textfield
具有的文本。
然后,只需在相应的对象上调用它,即可从其他类访问它。
示例:
public class A{
private B b;
private void someMethod(){
//This will receive the Text of the Textfield!
String text = b.getTextfieldValue();
}
}
public class B{
private JTextField text;
public String getTextfieldValue(){
return text.getText();
}
}
答案 1 :(得分:0)
我建议你在masterdetails
类中添加一个公共方法getDetails()
,它将文本字段的值作为数组或对象返回(无论你想要什么)。
现在actionPerformed
类的menu
方法在if语句之外提取局部变量f
并初始化为null
。现在在if(str.equals("Save"))
部分首先检查f
是否不是null
并调用f.getDetails()
以获取文本字段的值以执行您想要执行的任何操作。