I am new here and this is a very simple code since I am just on my first year in college. By the way, I am very sorry if this code is absurd because honestly Im not yet familliar with all of the terms of compiler. Well, my question is How can i add a background image for this code without disappearing the button and label? I want a frame that has a background and still shows the buttons and label that I place. and how can I insert Buffered Reader in this code, this is a file organizer, but it only shows first line of a text file. I want to show the whole text file. And is it possible to also show path of the file? Thank you so much in advance for your help guys!!
import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import javax.swing.JOptionPane; import javax.swing.ImageIcon; import java.awt.*; import javax.swing.*; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; public class project extends JFrame implements ActionListener{ public static final int WIDTH= 700; public static final int HEIGHT = 600; public static final int NUMBER_OF_CHAR=30; JLabel labelresult; private JTextField filenameField; public project () { setSize(WIDTH, HEIGHT); Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); JFrame frame = new JFrame (); setTitle("A Simple File Organizer"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton showButton = new JButton("Show Results"); showButton.addActionListener(this); showButton.setForeground(Color.GREEN); showButton.setBackground(Color.BLACK); contentPane.add(showButton); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(this); resetButton.setForeground(Color.GREEN); resetButton.setBackground(Color.BLACK); contentPane.add(resetButton); filenameField = new JTextField(NUMBER_OF_CHAR); contentPane.add(filenameField); filenameField.setText("Enter File Name Here"); labelresult = new JLabel (""); labelresult.setSize(500, 400); labelresult.setForeground(Color.BLUE); getContentPane().add(labelresult); } public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if(actionCommand.equals("Show Results")) showfirstLine(); else if(actionCommand.equals("Reset")) resetFields(); else labelresult.setText("Unexpected Error"); } private void showfirstLine () { Scanner fileInput = null; String filename = filenameField.getText(); File fileObject = new File(filename); if(!fileObject.exists()) labelresult.setText("Sorry, this file does not exist. Click reset button and try again."); else if (!fileObject.canRead()) labelresult.setText("That File is not Readable"); else{ try { fileInput = new Scanner (fileObject); } catch(FileNotFoundException e) { labelresult.setText("Error opening the file" + " " + filename); } String firstline = fileInput.nextLine(); labelresult.setText(firstline); fileInput.close(); } } private void resetFields() { filenameField.setText("Enter file name here:"); labelresult.setText(""); } public static void main (String [ ] args) { project gui = new project (); gui.setVisible(true); project frame = new project (); } }