मैं कर्मचारी बैठने का ट्रैक रखने में मदद के लिए एक प्रोग्राम लिख रहा हूं, हालांकि, मुझे उपयोगकर्ता की जानकारी प्राप्त करने में मुश्किल हो रही है जिसे जावा स्विंग जीयूआई द्वारा एक्सेल शीट पर मुद्रित करने के लिए कब्जा कर लिया गया था। मैं एक्सेल को भी लिखने के लिए अपाचे पीओआई का उपयोग कर रहा हूं।
कार्यक्रम एक्सेल शीट पर पंक्तियों के माध्यम से सही ढंग से पुनरावृत्त होता है, लेकिन ऐसा लगता है कि उपयोगकर्ता द्वारा अधिक जानकारी दर्ज करना जारी रखने के बाद पहली पंक्ति पर माना जाने वाला डेटा हटा दिया जाता है। यदि उपयोगकर्ता केवल ONCE (डेस्क नंबर, कर्मचारी का नाम, उस डेस्क पर कर्मचारियों की संख्या) में जानकारी दर्ज करता है, तो JButton "Add To List" को दबाता है, यह पहली पंक्ति की जानकारी को प्रिंट करता है जैसा कि माना जाता है। यदि उपयोगकर्ता एक से अधिक बार जानकारी दर्ज करता है, तो यह "सूची में जोड़ें" बटन दबाए जाने की मात्रा में वृद्धि करता है, लेकिन उपयोगकर्ता द्वारा दर्ज किए गए अंतिम इनपुट को केवल एक्सेल पंक्तियों पर खाली होने वाली जानकारी के अन्य पिछले उदाहरणों को छोड़कर प्रिंट करता है।
जिस तरह से काउंटर बढ़ाया जा रहा है, क्या यह एक समस्या है? या यह टेक्स्टफील्ड है जिसे समायोजित करने की आवश्यकता है? मैं इतनी लंबी पोस्ट के लिए माफी मांगता हूं, लेकिन मैं खुद को जावा में प्रोग्राम करना सिखा रहा हूं और यह समस्या मुझे कुछ समय से सता रही है।
यह मेरा अब तक का कोड है:
import java.awt.EventQueue;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.*;
import javax.swing.DropMode;
public class userMenu {
private JFrame frmUtilizationSeatingReport; //JFrame being used
private JTextField txtDeskNum; //Text box that will hold the desk number
private JTextField txtEmployeeName; //Text box for employee name
private JTextField txtNumAtDesk; //Text box for the number of employees at that desk
public int rownum = 1; //My counter being used to increment the row in which input is being stored in
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
userMenu window = new userMenu();
window.frmUtilizationSeatingReport.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public userMenu() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmUtilizationSeatingReport = new JFrame();
frmUtilizationSeatingReport.setTitle("Utilization Seating Report Program");
frmUtilizationSeatingReport.setBounds(100, 100, 436, 210);
frmUtilizationSeatingReport.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmUtilizationSeatingReport.getContentPane().setLayout(null);
JLabel lblDeskNumber = new JLabel("Desk Number:");
lblDeskNumber.setBounds(20, 11, 163, 21);
frmUtilizationSeatingReport.getContentPane().add(lblDeskNumber);
txtDeskNum = new JTextField();
txtDeskNum.setBounds(42, 30, 121, 20);
frmUtilizationSeatingReport.getContentPane().add(txtDeskNum);
txtDeskNum.setColumns(10);
JLabel lblEmployeeName = new JLabel("Employee Name:");
lblEmployeeName.setBounds(20, 55, 163, 21);
frmUtilizationSeatingReport.getContentPane().add(lblEmployeeName);
txtEmployeeName = new JTextField();
txtEmployeeName.setBounds(42, 73, 121, 20);
frmUtilizationSeatingReport.getContentPane().add(txtEmployeeName);
txtEmployeeName.setColumns(10);
JLabel lblNumberOfEmployees = new JLabel("Number of Employees at Desk:");
lblNumberOfEmployees.setBounds(20, 96, 281, 21);
frmUtilizationSeatingReport.getContentPane().add(lblNumberOfEmployees);
txtNumAtDesk = new JTextField();
txtNumAtDesk.setBounds(42, 115, 121, 20);
frmUtilizationSeatingReport.getContentPane().add(txtNumAtDesk);
txtNumAtDesk.setColumns(10);
JButton btnAdd = new JButton("Add To List");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent b) {
String deskNumber = ""; //Will take the desk number in as a string
int empsAtDesk = 0; //The number of employees at the desk as an int
String employeeName = ""; //Employee Name
boolean keepRunning = true;
deskNumber = txtDeskNum.getText();
empsAtDesk = Integer.parseInt(txtNumAtDesk.getText());
employeeName = txtEmployeeName.getText();
//Blank workbook
HSSFWorkbook workbook = new HSSFWorkbook();
//Blank sheet
HSSFSheet sheet = workbook.createSheet("Seating Details");
//create heading
Row rowHeading = sheet.createRow(0);
rowHeading.createCell(0).setCellValue("Desk:");
rowHeading.createCell(1).setCellValue("Employee(s)Name:");
rowHeading.createCell(2).setCellValue("Number At Desk:");
//Create 'total' headings
Row rowtotal = sheet.createRow(71);
rowtotal.createCell(0).setCellValue("Total:");
CellStyle stylerowtotal = workbook.createCellStyle();
Font totalfonts = workbook.createFont();
totalfonts.setBold(true);
totalfonts.setFontName(HSSFFont.FONT_ARIAL);
totalfonts.setFontHeightInPoints((short) 11);
stylerowtotal.setFont(totalfonts);
stylerowtotal.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowtotal.getCell(0).setCellStyle(stylerowtotal);
Row rowpercent = sheet.createRow(72);
rowpercent.createCell(0).setCellValue("Total %/Day:");
stylerowtotal = workbook.createCellStyle();
totalfonts = workbook.createFont();
totalfonts.setBold(true);
totalfonts.setFontName(HSSFFont.FONT_ARIAL);
totalfonts.setFontHeightInPoints((short) 11);
stylerowtotal.setFont(totalfonts);
stylerowtotal.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowpercent.getCell(0).setCellStyle(stylerowtotal);
Row percentMTh = sheet.createRow(73);
percentMTh.createCell(0).setCellValue("Total %/Week M-Th:");
stylerowtotal = workbook.createCellStyle();
totalfonts = workbook.createFont();
totalfonts.setBold(true);
totalfonts.setFontName(HSSFFont.FONT_ARIAL);
totalfonts.setFontHeightInPoints((short) 11);
stylerowtotal.setFont(totalfonts);
stylerowtotal.setVerticalAlignment(CellStyle.ALIGN_CENTER);
percentMTh.getCell(0).setCellStyle(stylerowtotal);
Row percentMFri = sheet.createRow(74);
percentMFri.createCell(0).setCellValue("Total %/Week M-F:");
stylerowtotal = workbook.createCellStyle();
totalfonts = workbook.createFont();
totalfonts.setBold(true);
totalfonts.setFontName(HSSFFont.FONT_ARIAL);
totalfonts.setFontHeightInPoints((short) 11);
stylerowtotal.setFont(totalfonts);
stylerowtotal.setVerticalAlignment(CellStyle.ALIGN_CENTER);
percentMFri.getCell(0).setCellStyle(stylerowtotal);
Row seatsAvai = sheet.createRow(75);
seatsAvai.createCell(0).setCellValue("Total Seats Available:");
stylerowtotal = workbook.createCellStyle();
totalfonts = workbook.createFont();
totalfonts.setBold(true);
totalfonts.setFontName(HSSFFont.FONT_ARIAL);
totalfonts.setFontHeightInPoints((short) 11);
stylerowtotal.setFont(totalfonts);
stylerowtotal.setVerticalAlignment(CellStyle.ALIGN_CENTER);
seatsAvai.getCell(0).setCellStyle(stylerowtotal);
//Create Cell Formulas
//Total number of employees at a desk cell formula
rowtotal.createCell(2).setCellFormula("SUM(C2:C71)");
//Total percentage for the day
rowpercent.createCell(2).setCellFormula("(SUM(C2:C71) / 78) * 100");
//Font size and style loop for my headers
for(int i = 0; i < 3; i++)
{
CellStyle stylerowHeading = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short) 11);
stylerowHeading.setFont(font);
stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowHeading.getCell(i).setCellStyle(stylerowHeading);
}
//This data needs to be written (Object[])
Map <String, Object[]> data = new TreeMap<String, Object[]>();
data.put("5", new Object[] {deskNumber, employeeName, empsAtDesk});
if(keepRunning){
//Iterate over data and write to sheet
Set<String> keyset = data.keySet();
for(String Key : keyset)
{
Row row = sheet.createRow(rownum++);
Object [] objArr = data.get(Key);
int cellnum = 0;
for(Object obj : objArr)
{
Cell cell = row.createCell(cellnum++);
if(obj instanceof String)
{
cell.setCellValue((String)obj);
}
else if(obj instanceof Integer)
{
cell.setCellValue((Integer)obj);
}
}
//Auto size my columns that will be filled out with user input info.
for (int i = 0; i < 3; i++)
{
sheet.autoSizeColumn(i);
}
} //top for loop brace
try{
//save to excel file
FileOutputStream out = new FileOutputStream(new File("Employee Seating Report.xls"));
workbook.write(out);
out.flush();
out.close();
workbook.close();
System.out.println("Excel Written Succesfully..." + '\n');
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1){
e1.printStackTrace();
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
//Empty text fields once user presses "Add To List" button
txtDeskNum.setText("");
txtEmployeeName.setText("");
txtNumAtDesk.setText("");
}//If statement end brace
}//Public void end brace
});
btnAdd.setBounds(214, 42, 129, 23);
frmUtilizationSeatingReport.getContentPane().add(btnAdd);
JButton btnExit = new JButton("End Program");
btnExit.addActionListener(new ActionListener() {
//If the user presses the"End Program" button, close the program.
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnExit.setBounds(214, 95, 129, 23);
frmUtilizationSeatingReport.getContentPane().add(btnExit);
}
}
2 जवाब
आप अपने addActionListener
के अंदर अपनी कार्यपुस्तिका बना रहे हैं और उसमें एक नई एक्सेल फ़ाइल लिख रहे हैं, इसलिए यह हमेशा पिछले लिखित डेटा को अधिलेखित कर देगी।
अपने बटन btnAdd
के क्लिक पर वस्तुओं की सूची EmployeeDesk
में सभी डेटा एकत्र करने का प्रयास करें और एक नया बटन बनाएं जो कार्यपुस्तिका बनाता है और आपकी सूची से डेटा को आपके एक्सेल में लिखता है, इसलिए आपके पास होगा एक नया बटन btnSave
जो एक्सेल फाइल बनाता है और कुछ भी लिखता है।
मैं .xls प्रारूप का विशेषज्ञ नहीं हूं और इसका उपयोग नहीं किया है लेकिन मैंने सीएसवी फाइलों को लिखा है और आप जावा मानक पुस्तकालय का उपयोग काफी सरलता से करने के लिए कर सकते हैं। हो सकता है कि एक jtable पंक्तियों के कॉलम और हेडर बनाने के साथ आपके पास मौजूद कुछ अस्पष्टता को दूर कर दे। यह निश्चित रूप से कम वर्बोज़ होगा।
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।