gpt4 book ai didi

java - 从另一个方法获取变量

转载 作者:行者123 更新时间:2023-12-01 13:40:39 25 4
gpt4 key购买 nike

//Import packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;

//Main class
public class gui1{
//Declare variables
static JFrame frame1;
static Container pane;
static JScrollPane Scroll;
static JButton Submit;
static JLabel Client, DateServiced, NameofPC, Manufacturer, CPUBIT, CPUTYPE, SerialNumber, ProductNumber, Harddriveplug, TypeofHD, MotherboardModel, TypeofRam, ModelofHardDrive, SpeedofHarddrive, AmountofRam, ThingsDone, CustomerStatements, OtherComments, Technician;
static JTextField TClient, TDateServiced, TNameofPC, TManufacturer, TCPUBIT, TCPUTYPE, TSerialNumber, TProductNumber, THarddriveplug, TTypeofHD, TMotherboardModel, TTypeofRam, TModelofHardDrive, TSpeedofHarddrive, TAmountofRam, TTechnician;
static Insets insets;
static JTextArea TThingsDone, TCustomerStatements, TOtherComments;
public static void main (String args[]){
//Set Look and Feel
try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch (ClassNotFoundException e) {}
catch (InstantiationException e) {}
catch (IllegalAccessException e) {}
catch (UnsupportedLookAndFeelException e) {}

//Create the frame
frame1 = new JFrame ("Trouble Ticket");
frame1.setSize (800,800);
pane = frame1.getContentPane();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
insets = pane.getInsets();
pane.setLayout (null);

//Create controls
Submit = new JButton ("Submit");
Client = new JLabel ("Client");
DateServiced = new JLabel ("Date Serviced");
NameofPC = new JLabel ("Designated Name of PC");
Manufacturer = new JLabel ("Manufacturer of Entire PC Such as HP or Dell");
CPUBIT = new JLabel ("64 or 32 Bit CPU");
CPUTYPE = new JLabel ("Model of CPU");
SerialNumber = new JLabel ("Serial Number on Case");
ProductNumber = new JLabel ("Product Number on Case");
Harddriveplug = new JLabel ("Type Of Harddrive connector");
TypeofHD = new JLabel ("HDD or SDD");
MotherboardModel = new JLabel ("Moderboard Model");
TypeofRam = new JLabel ("Type of Ram");
AmountofRam = new JLabel ("Amount of Ram");
ModelofHardDrive = new JLabel ("Model of Hard Drive");
SpeedofHarddrive = new JLabel ("Speed of Hard Drive");
CustomerStatements = new JLabel ("Customer Statements");
ThingsDone = new JLabel ("What was done to the Machine");
OtherComments = new JLabel ("Comments");
Technician = new JLabel ("Working Technician");

TClient = new JTextField (25);
TDateServiced = new JTextField (8);
TNameofPC = new JTextField (25);
TManufacturer = new JTextField (25);
TCPUBIT = new JTextField (2);
TSerialNumber = new JTextField (20);
TProductNumber = new JTextField (22);
THarddriveplug = new JTextField (4);
TTypeofHD = new JTextField (3);
TModelofHardDrive = new JTextField (25);
TSpeedofHarddrive = new JTextField (10);
TCustomerStatements = new JTextArea (5,20);
TThingsDone = new JTextArea (5,20);
TOtherComments = new JTextArea (5,20);
TTechnician = new JTextField (25);
TCustomerStatements.setLineWrap(true);
TOtherComments.setLineWrap(true);
TThingsDone.setLineWrap(true);

//Add all components to panel
pane.add (Client);
pane.add (DateServiced);
pane.add (NameofPC);
pane.add (Manufacturer);
pane.add (CPUBIT);
pane.add (SerialNumber);
pane.add (ProductNumber);
pane.add (Harddriveplug);
pane.add (TypeofHD);
pane.add (ModelofHardDrive);
pane.add (SpeedofHarddrive);
pane.add (ThingsDone);
pane.add (CustomerStatements);
pane.add (OtherComments);
pane.add (Technician);
pane.add (TClient);
pane.add (TDateServiced);
pane.add (TNameofPC);
pane.add (TManufacturer);
pane.add (TCPUBIT);
pane.add (TSerialNumber);
pane.add (TProductNumber);
pane.add (THarddriveplug);
pane.add (TTypeofHD);
pane.add (TModelofHardDrive);
pane.add (TSpeedofHarddrive);
pane.add (TThingsDone);
pane.add (TCustomerStatements);
pane.add (TOtherComments);
pane.add (TTechnician);

pane.add(Submit);

//Place all components
Client.setBounds (insets.left + 5, insets.top + 5, Client.getPreferredSize().width, Client.getPreferredSize().height);
TClient.setBounds (Client.getX() + Client.getPreferredSize().width + 35, insets.top + 5, TClient.getPreferredSize().width, TClient.getPreferredSize().height);
DateServiced.setBounds (Client.getX(), insets.top + 40, DateServiced.getPreferredSize().width, DateServiced.getPreferredSize().height);
TDateServiced.setBounds (Client.getX() + DateServiced.getPreferredSize().width + 35, insets.top + 40, TDateServiced.getPreferredSize().width, TDateServiced.getPreferredSize().height);
Manufacturer.setBounds (Client.getX(), DateServiced.getY() + 35, Manufacturer.getPreferredSize().width, Manufacturer.getPreferredSize().height);
TManufacturer.setBounds (Client.getX() + Manufacturer.getPreferredSize().width + 35, DateServiced.getY() + 35, TManufacturer.getPreferredSize().width, TManufacturer.getPreferredSize().height);
CPUBIT.setBounds (Client.getX(), Manufacturer.getY() + 35, CPUBIT.getPreferredSize().width, CPUBIT.getPreferredSize().height);
TCPUBIT.setBounds (Client.getX() + CPUBIT.getY() + 35, Manufacturer.getY() + 35, TCPUBIT.getPreferredSize().width, TCPUBIT.getPreferredSize().height);
SerialNumber.setBounds (Client.getX(), CPUBIT.getY() + 35, SerialNumber.getPreferredSize().width, SerialNumber.getPreferredSize().height);
TSerialNumber.setBounds (Client.getX() + SerialNumber.getPreferredSize().width + 35, CPUBIT.getY() + 35, TSerialNumber.getPreferredSize().width, TSerialNumber.getPreferredSize().height);
ProductNumber.setBounds (Client.getX(), SerialNumber.getY() + 35, ProductNumber.getPreferredSize().width, ProductNumber.getPreferredSize().height);
TProductNumber.setBounds (Client.getX() + ProductNumber.getPreferredSize().width + 35, SerialNumber.getY() + 35, TProductNumber.getPreferredSize().width, TProductNumber.getPreferredSize().height);
Harddriveplug.setBounds (Client.getX(), ProductNumber.getY() + 35, Harddriveplug.getPreferredSize().width, Harddriveplug.getPreferredSize().height);
THarddriveplug.setBounds (Client.getX() + ProductNumber.getPreferredSize().width + 35, ProductNumber.getY() + 35, THarddriveplug.getPreferredSize().width, THarddriveplug.getPreferredSize().height);
TypeofHD.setBounds (Client.getX(), Harddriveplug.getY() + 35, TypeofHD.getPreferredSize().width, TypeofHD.getPreferredSize().height);
TTypeofHD.setBounds (Client.getX() + Harddriveplug.getPreferredSize().width + 35, Harddriveplug.getY() + 35, TTypeofHD.getPreferredSize().width, TTypeofHD.getPreferredSize().height);
ModelofHardDrive.setBounds (Client.getX(), TypeofHD.getY() + 35, ModelofHardDrive.getPreferredSize().width, ModelofHardDrive.getPreferredSize().height);
TModelofHardDrive.setBounds (Client.getX() + ModelofHardDrive.getPreferredSize().width + 35, TypeofHD.getY() + 35, TModelofHardDrive.getPreferredSize().width, TModelofHardDrive.getPreferredSize().height);
SpeedofHarddrive.setBounds (Client.getX(), ModelofHardDrive.getY() +35, SpeedofHarddrive.getPreferredSize().width, SpeedofHarddrive.getPreferredSize().height);
TSpeedofHarddrive.setBounds (Client.getX() + SpeedofHarddrive.getPreferredSize().width + 35, ModelofHardDrive.getY() +35, TSpeedofHarddrive.getPreferredSize().width, TSpeedofHarddrive.getPreferredSize().height);
Technician.setBounds (Client.getX(), SpeedofHarddrive.getY() +35, Technician.getPreferredSize().width, Technician.getPreferredSize().height);

TTechnician.setBounds (Client.getX() + Technician.getPreferredSize().width + 35, TSpeedofHarddrive.getY() +35, TTechnician.getPreferredSize().width, TTechnician.getPreferredSize().height);

ThingsDone.setBounds (Client.getX(), Technician.getY() +35, ThingsDone.getPreferredSize().width, ThingsDone.getPreferredSize().height);

TThingsDone.setBounds (Client.getX(), ThingsDone.getY() +35, TThingsDone.getPreferredSize().width, TThingsDone.getPreferredSize().height);

CustomerStatements.setBounds (Client.getX(), TThingsDone.getY() + TThingsDone.getPreferredSize().height +35, CustomerStatements.getPreferredSize().width, CustomerStatements.getPreferredSize().height);

TCustomerStatements.setBounds (Client.getX(),CustomerStatements.getY() +CustomerStatements.getPreferredSize().height +35, TCustomerStatements.getPreferredSize().width, TCustomerStatements.getPreferredSize().height);

OtherComments.setBounds (Client.getX() + TCustomerStatements.getPreferredSize().width + 35, CustomerStatements.getY(), OtherComments.getPreferredSize().width, OtherComments.getPreferredSize().height);

TOtherComments.setBounds (Client.getX() + TCustomerStatements.getPreferredSize().width + 35, TCustomerStatements.getY(), TOtherComments.getPreferredSize().width, TOtherComments.getPreferredSize().height);
Submit.setBounds(TClient.getX() + TClient.getPreferredSize().width + 65, Client.getY(), Submit.getPreferredSize().width, Submit.getPreferredSize().height);
//Set frame visible
frame1.setVisible (true);

//Button's action
Submit.addActionListener(new Submitaction()); //Register action
}

public static class Submitaction implements ActionListener{
public void actionPerformed (ActionEvent e){
String Clientname = TClient.getText();
String DateofService = TDateServiced.getText();
String Nameofpc = TNameofPC.getText();
String manufacturer = TManufacturer.getText();
String CPUBit = TCPUBIT.getText();
String Serialnumber = TSerialNumber.getText();
String Productnumber = TProductNumber.getText();
String HardDriveplug = THarddriveplug.getText();
String Typeofhd = TTypeofHD.getText();
String ModelofHarddrive = TModelofHardDrive.getText();
String SpeedofHardDrive = TSpeedofHarddrive.getText();
String WhatDidtheCustomerSay = TCustomerStatements.getText();
String WorkDone = TThingsDone.getText();
String TechniciansComments = TOtherComments.getText();
String TechnicianWorking = TTechnician.getText();
}
public static void Save(String arg[]) throws IOException
{
File save = new File("/home/bekk/Outline.txt");
PrintWriter out = new PrintWriter("home/bekk/Outline.txt");
PrintWriter.println(Clientname);

}
}
}

这是我的全部代码

我想讨论的部分如下

public static class Submit implements ActionListener{
public void actionPerformed (ActionEvent e){
String Clientname = TClient.getText();
String DateofService = TDateServiced.getText();
String Nameofpc = TNameofPC.getText();
String manufacturer = TManufacturer.getText();
String CPUBit = TCPUBIT.getText();
String Serialnumber = TSerialNumber.getText();
String Productnumber = TProductNumber.getText();
String HardDriveplug = THarddriveplug.getText();
String Typeofhd = TTypeofHD.getText();
String ModelofHarddrive = TModelofHardDrive.getText();
String SpeedofHardDrive = TSpeedofHarddrive.getText();
String WhatDidtheCustomerSay = TCustomerStatements.getText();
String WorkDone = TThingsDone.getText();
String TechniciansComments = TOtherComments.getText();
String TechnicianWorking = TTechnician.getText();
}
public static void Save(String arg[]) throws IOException
{
File save = new File("/home/bekk/Outline.txt");
PrintWriter out = new PrintWriter("home/bekk/Outline.txt");
PrintWriter.println(Clientname);

}
}
}

我正在尝试从文本字段/文本区域获取字符串,然后将它们保存到文件中。我将将输入保存到 .txt 文件的代码部分放在其自己的方法中,因为它会抛出异常,表示不存在文件。我如何访问在方法提交中获取值的变量,然后让它在方法保存中将该值保存到文本文件中。
或者找到一种替代方法来实现我上面提到的总体目标。

最佳答案

您有几个选择。

你可以

如果发生错误,只需处理...

public void actionPerformed (ActionEvent e){
//...

File save = new File("/home/bekk/Outline.txt");
PrintWriter out = null;
try {
out = new PrintWriter(save);
out.println(Clientname);
//...
} catch (IOException exp) {
JOptionPane.showMessageDialog(null, "Failed to save file", "Error", JOptionPane.ERROR_MESSAGE);
} finally {
try {
out.close();
} catch (Exception exp) {
}
}
}

这是“好的”,但是您确实倾向于将许多可能可重用的代码封装在一个方法中......

你可以

创建一个保存方法并将要保存的值传递给它...

public void actionPerformed (ActionEvent e){
//...

File save = new File("/home/bekk/Outline.txt");
try {
save(save, Clientname,
DateofService,
Nameofpc,
manufacturer,
CPUBit,
Serialnumber,
Productnumber,
HardDriveplug,
Typeofhd,
ModelofHarddrive,
SpeedofHardDrive,
WhatDidtheCustomerSay,
WorkDone,
TechniciansComments,
TechnicianWorking);
} catch (IOException exp) {
JOptionPane.showMessageDialog(null, "Failed to save file", "Error", JOptionPane.ERROR_MESSAGE);
}
}

public void save(File save, String... values) throws IOException {
PrintWriter out = null;
try {
out = new PrintWriter(save);
for (String value : values) {
out.println(value);
}
} finally {
try {
out.close();
} catch (Exception exp) {
}
}
}

这仍然需要您自己处理IOException,但提供了一段可重用的代码,将来可以扩展/覆盖该代码以提供其他可能性。

你可以

将您尝试保存的属性封装到对象中并使用类似Java Architecture for XML Binding (JAXB)的内容管理它...

但是...

这些都无法修复“它抛出异常,表示不存在文件”,因为该文件根本无法写入。

最可能的原因是文件路径不存在,而不是文件不存在。系统无法在您尝试创建新文件的位置创建新文件。

你可以尝试类似...

File save = new File("/home/bekk");
if (!save.exists()) {
JOptionPane.showMessageDialog(this, save + " does not exist", "Error", JOptionPane.ERROR_MESSAGE);
} else if (!save.isDirectory()) {
JOptionPane.showMessageDialog(this, save + " is not a directory", "Error", JOptionPane.ERROR_MESSAGE);
}

检查它。

虽然有许多可能的解决方案,但就您的情况而言,最好使用...

File save = new File(System.getProperty("user.home"));

这将为您提供用户的主目录...

如果需要,您还可以使用File#mkdirs创建所需的目录结构...

File save = new File("/home/bekk");
if (save.exists() || save.mkdirs()) {
save = new File(save + File.separator + "Outline.txt");
//...
}

关于java - 从另一个方法获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20821111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com