import java.awt.*; import java.awt.event.*; import java.io.FileNotFoundException; import javax.swing.*; public class TekstGUI { private static JLabel tekst = new JLabel("Velg en fil saa skal jeg vise deg den"); public static void main(String[] args) { JFrame vindu = new JFrame("Klokke"); vindu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); vindu.add(panel); panel.add(tekst); class VelgFilAction implements ActionListener{ @Override public void actionPerformed(ActionEvent e){ JFileChooser velger = new JFileChooser(); int resultat = velger.showOpenDialog(null); if(resultat == JFileChooser.APPROVE_OPTION){ try{ FilLeser leser = new FilLeser(velger.getSelectedFile()); System.out.println(leser.hentString()); tekst.setText(leser.hentString()); }catch(FileNotFoundException error){ tekst.setText("No gikk galt, prov paa nytt"); } }else{ tekst.setText("No gikk galt, prov paa nytt"); } } } JButton velgFilKnapp = new JButton("Velg fil"); velgFilKnapp.addActionListener(new VelgFilAction()); panel.add(velgFilKnapp); vindu.pack(); vindu.setVisible(true); } }