The following code is supposed to create a small gui that will allow for text to be read from, and written to an external file. I believe that the code is correct, at least as best as I know, but when I try to compile it I get errors complaining about the main class that I do not understand....
I will post the code I have here, and hope for some advice on this.
I commented out two lines of code near the end of the program that were giving me a world of trouble, but it still will not recognize my main class.import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class Newclass2 extends JFrame implements ActionListener { private String s1, s2; static String s3; private JFrame f = new JFrame("text testing"); private JTextField txt = new JTextField(24); private JTextField txt2 = new JTextField(24); private JButton b1 = new JButton("Submit"); private JButton b2 = new JButton("Exit"); public Newclass2(){ f.setLayout(new BorderLayout()); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.add(new JLabel("text1")); p.add(txt); p.add(new JLabel("text2")); p.add(txt2); f.add(p, BorderLayout.CENTER); JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout()); p2.add(b1); p2.add(b2); add.addActionListener(this); open.addActionListener(this); f.add(p2, BorderLayout.SOUTH); f.setVisible(true); f.setSize(480,480); } @Override public void actionPerformed(ActionEvent e){ if(e.getSource()==b1){ String txtemp1 = ""; String txttemp2 = ""; String txttemp1 = txt.getText().trim(); txttemp2 = txt2.getText().trim(); String txttemps2 = null; s3 =("text here"+txttemp1) + (txttemps2); String data = Newclass2.s3; try{ try (BufferedWriter readz = BufferedWriter(new FileWriter(new File ("C:/users/zlloyd1/desktop/test1.doc")))) { readz.write(data); readz.newLine(); } System.out.println("Done"); }catch(Exception e2){System.out.println("Error message text");} } if(e.getSource()==b2){ JFileChooser choosa = new JFileChooser("C:/users/zlloyd1/desktop/test1.doc"); int x = choosa.showOpenDialog(null); if(x == JFileChooser.APPROVE_OPTION){ File file = choosa.getSelectedFile();}}}} //try( //Desktop.getDesktop().open(file);}catch(Exception e3){}}}} private static void main(String[] args){ new Newclass2(); } } }
This is the errors that the compiler spits at me:
newclass2.java:64: error: class, interface, or enum expected
private static void main(String[] args){
^
newclass2.java:66: error: class, interface, or enum expected
}}
^
It also seems to have a problem with add, open, and BufferedWriter, when I try to load this code into NetBeans, but I thought those were built in Java methods. It seems that is NOT the case, because it is telling me "cannot find symbol" for all three of them and suggesting that I create a method for them.
here is the lines where this is happening:
for the BufferedWriter, but it is strange, because it does not have an issue with the first one, just the second BufferedWriter??try{ try (BufferedWriter readz = BufferedWriter(new FileWriter(new File ("C:/users/zlloyd1/desktop/test1.doc")))) {
and for the add, and open it is these two lines that are a problem:
I am not sure if I have misused this here for these, but it is not recognizing add, nor open as legitimate commands, or methods, even though I have all the necessary imported libraries, I believe????add.addActionListener(this); open.addActionListener(this);
Can someone PLEASE tell me what I am doing wrong here, in Java for Dummies language??