| | |
Runtime Error help needed
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I have some code that I need help with. The code compiles but I get a runtime error. Here is the error and the code:
ERROR:
Exception in thread "main" java.lang.Error: Do not use MessageLog.setLayout() us
e MessageLog.getContentPane().setLayout() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.setLayout(JFrame.java:531)
at MessageLog.<init>(MessageLog.java:23)
at MessageLog.main(MessageLog.java:90)
Press any key to continue . . .
ERROR:
Exception in thread "main" java.lang.Error: Do not use MessageLog.setLayout() us
e MessageLog.getContentPane().setLayout() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.setLayout(JFrame.java:531)
at MessageLog.<init>(MessageLog.java:23)
at MessageLog.main(MessageLog.java:90)
Press any key to continue . . .
Java Syntax (Toggle Plain Text)
import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MessageLog extends JFrame implements ActionListener { JLabel lblMessage = new JLabel(); JTextField txtMessage = new JTextField(); JLabel lblTitle = new JLabel(); JTextField txtTitle = new JTextField(); JButton btnSave = new JButton(); JButton btnReset = new JButton(); JLabel lblHeader = new JLabel(); String fileName = "messages.log"; PrintWriter writer; //Constructor //it will create and place the controls on frame public MessageLog() { this.setLayout(null); lblMessage.setText("Message"); lblMessage.setBounds(new Rectangle(54, 99, 55, 16)); txtMessage.setText(""); txtMessage.setBounds(new Rectangle(116, 98, 169, 20)); txtTitle.setBounds(new Rectangle(116, 74, 169, 21)); lblTitle.setText("Title"); lblTitle.setBounds(new Rectangle(53, 74, 54, 16)); btnSave.setBounds(new Rectangle(179, 141, 65, 22)); btnSave.setText("Save"); btnSave.addActionListener(this); btnReset.setBounds(new Rectangle(108, 141, 69, 22)); btnReset.setText("Reset"); btnReset.addActionListener(this); this.setDefaultCloseOperation(EXIT_ON_CLOSE); lblHeader.setFont(new java.awt.Font("Verdana", Font.BOLD, 22)); this.add(txtTitle); lblHeader.setText("Message Log"); lblHeader.setBounds(new Rectangle(85, 10, 172, 46)); this.add(txtMessage); this.add(lblTitle); this.add(lblMessage, null); this.add(btnReset); this.add(btnSave); this.add(lblHeader); this.setSize(340,220); this.setTitle("Message Log"); this.setVisible(true); this.setLocation(400,300); } //To handle the action event of the buttons public void actionPerformed(ActionEvent actionEvent) { if(actionEvent.getSource() == btnReset) { txtTitle.setText(""); txtMessage.setText(""); } else if(actionEvent.getSource() == btnSave) { try { File file = new File(fileName); //Check if file exists if(file.exists()) { //append the message at the end of file writer = new PrintWriter(new FileWriter(fileName, true)); writer.println(txtTitle.getText() + "," + txtMessage.getText()); writer.close(); JOptionPane.showMessageDialog(null,"Message logged to file."); } else { //create new file and save message (if file not exist) writer = new PrintWriter(new FileWriter(fileName)); writer.println(txtTitle.getText() + "," + txtMessage.getText()); writer.close(); JOptionPane.showMessageDialog(null,"Message logged to file."); } }catch(Exception e){} } } public static void main (String[] args) { MessageLog mLog = new MessageLog(); } }
Actually I changed that but then I get another runtime error as follows and cannot see where it is asking me to make the change:
ERROR:
Exception in thread "main" java.lang.Error: Do not use MessageLog.add() use Mess
ageLog.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.addImpl(JFrame.java:491)
at java.awt.Container.add(Container.java:307)
at MessageLog.<init>(MessageLog.java:39)
at MessageLog.main(MessageLog.java:90)
Press any key to continue . . .
ERROR:
Exception in thread "main" java.lang.Error: Do not use MessageLog.add() use Mess
ageLog.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.addImpl(JFrame.java:491)
at java.awt.Container.add(Container.java:307)
at MessageLog.<init>(MessageLog.java:39)
at MessageLog.main(MessageLog.java:90)
Press any key to continue . . .
![]() |
Similar Threads
- Runtime Error -21470024770 (8007007e) (Windows NT / 2000 / XP)
- Microsoft VBScript runtime error 800a0005 (ASP)
- "The Sims" visual C++ runtime error, what shall i do? (Windows Software)
- Visual Runtime Error, Sound Problem, Disabled Norton and more! (Windows NT / 2000 / XP)
- Runtime Error 424 object required (was: Please Help!) (Visual Basic 4 / 5 / 6)
- Runtime error using Incredimail on XP (Windows NT / 2000 / XP)
- Runtime error ??!! (Windows 95 / 98 / Me)
Other Threads in the Java Forum
- Previous Thread: Uploading
- Next Thread: Registering a data source with Windows ODBC manager
| Thread Tools | Search this Thread |
Tag cloud for Java
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) calculator card chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing threads time tree unlimited webservices windows





