| | |
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 |
actionlistener android api applet application array arrays automation binary blackberry block bluetooth character chat class classes client code compile component consumer database desktop developmenthelp draw eclipse error event exception fractal freeze ftp game givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie number online oracle page print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing template textfields threads time title tree tutorial-sample update windows working





