I created a basic notepad to store anything i need and to load it after, just for learning purposes mainly and can be useful later on. I got everything basically working now, but I want to add a background. It won't show, I'm using a .png file. Also, If you could tell me how I could change the text inside my gui to blue or red? I can only change the background's color, but not the text.
Any help is appreciated.
Linking me to any useful tutorial will help.
Here's the code:
package gui; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Scanner; import javax.swing.*; import javax.swing.GroupLayout.Alignment; import javax.swing.GroupLayout.ParallelGroup; import javax.swing.GroupLayout.SequentialGroup; /** * TODO: add some more buttons including clearing the whole screen and file? with a warning message. * possibly add something of a search feature. * * * @author Rodrigo */ @SuppressWarnings("serial") public class NoteMaker extends JFrame implements ActionListener { private static final String NOTES_DIR = "./Notes/notes.txt"; private JTextArea text; private JButton save; private JScrollPane jScrollPane1; private String[] loaded = new String[50]; private JButton red; ImageIcon img = new ImageIcon("./icons/background.png"); private boolean load = false; private static void load() { JOptionPane.showConfirmDialog(null, "Would you like to load you're previous texts?"); //TODO: make sure the person clicked yes. loadNP(); } private NoteMaker() { super("NotePad"); loadCrap(); /* * Setting all necessities. */ setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800, 700); JLabel label = new JLabel(img); add(label); /* * Setting the text area. */ text = new JTextArea(); if(load) { for(int j = 0; j < loaded.length; j++) { if(loaded[j] == null) continue; text.append(loaded[j]); text.append("\n"); } } text.setColumns(20); text.setLineWrap(true); text.setRows(5); text.setFont(new Font("Serif", Font.BOLD, 20)); text.setFont(new Font("Serif", Font.ITALIC, 20)); text.setWrapStyleWord(true); /* * Now setting the save button. */ save = new JButton("Click me to save."); save.addActionListener(this); /* * Now to set the color red button. */ red = new JButton("Red"); red.addActionListener(this); /* * Sets the scrolling bar. */ jScrollPane1 = new JScrollPane(text); /* * Now position the components. */ GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); ParallelGroup hgroup = layout.createParallelGroup(GroupLayout.Alignment.LEADING); SequentialGroup h1 = layout.createSequentialGroup(); ParallelGroup h2 = layout.createParallelGroup(GroupLayout.Alignment.TRAILING); h2.addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE); h2.addComponent(save, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 212, Short.MAX_VALUE); h2.addComponent(red, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 212, Short.MAX_VALUE); h1.addContainerGap(); h1.addGroup(h2); h1.addContainerGap(); hgroup.addGroup(Alignment.TRAILING, h1); layout.setHorizontalGroup(hgroup); ParallelGroup vgroup = layout.createParallelGroup(GroupLayout.Alignment.LEADING); SequentialGroup v1 = layout.createSequentialGroup(); v1.addContainerGap(); v1.addComponent(save); v1.addContainerGap(); v1.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE); v1.addContainerGap(); v1.addComponent(red); v1.addContainerGap(); vgroup.addGroup(v1); layout.setVerticalGroup(vgroup); } public void loadCrap() { try { Scanner scan = new Scanner(new File(NOTES_DIR)); String[] crap = new String[5]; int in = 0; while(scan.hasNext()) { crap[0] = scan.next(); loaded[in] = crap[0]; in++; crap[0] = null; } } catch (FileNotFoundException e) { e.printStackTrace(); } } public void saveCrap(String text) { System.out.println(text); BufferedWriter bw; try { bw = new BufferedWriter(new FileWriter(new File(NOTES_DIR))); bw.newLine(); bw.write(text); if(bw != null) bw.close(); } catch (IOException e) { e.printStackTrace(); } } /** * This will search the files and the text inside the gui already. */ public void search(String text) { File file = new File(NOTES_DIR); if(file.canRead()) { try { Scanner scan = new Scanner(file); while(scan.hasNext()) { if(scan.next().contains(text)) { //TODO: make the search crap feature show up. System.out.println(scan.next()); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } } public static void loadNP() { try { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { new NoteMaker(); } }); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String...args) { load(); } private boolean redon = false; @Override /** * Handle all the actions here. */ public void actionPerformed(ActionEvent e) { if(e.getSource() == red && !redon) { text.setBackground(Color.RED); redon = true; } else if (e.getSource() == red && redon) { redon = false; text.setBackground(Color.BLUE); } if(e.getSource() == save) { saveCrap(text.getText()); /*String[] old = new String[10]; old[0] = text.getText(); text.append(old[0]);*/ } } }