//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);
}
}
}
我正在尝试从textfields / textareas中获取一个字符串,然后将它们保存到文件中。 我将用于保存输入的代码部分放在自己的方法中的.txt文件中,因为它抛出了一个异常,说明没有文件存在。 如何访问方法Submit中获取值的变量,然后将其保存到方法Save中的文本文件中。 或者找到另一种方法来完成我上面提到的所有目标。
答案 0 :(得分:1)
您有几个选择。
如果发生错误,只需处理错误......
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
,但提供了可重用的代码片段,可以在将来扩展/覆盖以提供其他可能性。
将您要保存的属性封装到Object
中并使用类似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");
//...
}
答案 1 :(得分:0)
只需将其设为全局/类变量,即可在班级的任何位置访问它。