dtwconsulting
02-08-2006, 04:52 PM
First off let me just say up front that I am a newbie and trying to learn Java and this code was taken from a tutorial. It was set to run as a java applet but I changed that. The following code will not run from Eclipse. It gives the following error: The selection does not contain a main type. At a complete loss here. There are 3 class files with the project.
MultiPanelWithEvents.java
package GUIDemo1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MultiPanelWithEvents implements ActionListener {
/**
* @param args
*/
InputsPanel fInputsPanel;
JTextArea fTextOutput;
public void main(String[] args) {
// TODO Auto-generated method stub
Container content_pane = new Container();
// Set the layout as before with 1 row of 3 columns
// but now in one step.
content_pane.setLayout (new GridLayout (1, 3));
// First create a panel of buttons
ActionButtonsPanel buttons_panel =
new ActionButtonsPanel (this);
// Next create a panel of input fields and labels
fInputsPanel =
new InputsPanel ("Input x ", "1.5",
"Input y ", "3.14");
// Use a JTextArea for the output of the calculations.
fTextOutput = new JTextArea ();
fTextOutput.setBackground (Color.LIGHT_GRAY);
fTextOutput.setEditable (false);
// The grid fills the 3 columns sequentially.
content_pane.add (buttons_panel);
content_pane.add (fInputsPanel);
content_pane.add (fTextOutput);
} // init
/** Handle the button events.**/
public void actionPerformed (ActionEvent ae) {
String str1 = fInputsPanel.fTextfieldTop.getText ();
String str2 = fInputsPanel.fTextfieldBot.getText ();
double val1=0.0;
double val2=0.0;
try {
val1 = Double.parseDouble (str1);
val2 = Double.parseDouble (str2);
} catch (NumberFormatException nfe){
System.out.println ("Improper input");
}
if (ae.getActionCommand ().equals ("Add") ){
fTextOutput.setText ("x + y = " + (val1+val2));
} else {
fTextOutput.setText ("x * y = " + (val1*val2));
}
} // actionPerformed
} // class MultiPanelWithEvents
ActionButtonsPanel.java
package GUIDemo1;
import java.awt.event.*;
import javax.swing.*;
/** JPanel subclass with two buttons. **/
public class ActionButtonsPanel extends JPanel {
/** Constructor adds 2 buttons to the panel and
* adds the listener passed as an argument.
**/
ActionButtonsPanel (ActionListener listener) {
// Create two buttons
JButton add_but = new JButton ("Add" );
JButton mult_but = new JButton ("Mult");
add_but.addActionListener (listener);
mult_but.addActionListener (listener);
// Put a button in each grid cell
add (add_but);
add (mult_but);
} // ctor
} // class ActionButtonsPanel
InputsPanel.java
package GUIDemo1;
import java.awt.*;
import javax.swing.*;
/** Panel to hold input textfields. **/
public class InputsPanel extends JPanel {
JTextField fTextfieldTop=null;
JTextField fTextfieldBot=null;
/** Constructor builds panel with labels and text fields. **/
InputsPanel (String label_str_top, String init_top,
String label_str_bot, String init_bot) {
// Set the layout with 2 rows by 2 columns
setLayout (new GridLayout (2,2));
// Create two text fields with the initial values
fTextfieldTop = new JTextField (init_top);
fTextfieldBot = new JTextField (init_bot);
// Create the first label and right justify the text
JLabel label_top =
new JLabel (label_str_top, SwingConstants.RIGHT);
// Insert the label and textfield into the top grid row
add (label_top);
add (fTextfieldTop);
// Create the second label and right justify the text
JLabel label_bot =
new JLabel (label_str_bot, SwingConstants.RIGHT);
// Insert the second label and textfield into the bottom grid row
add (label_bot);
add (fTextfieldBot);
} // ctor
} // class InputsPanel
The build went just fine but it refuses to run. Stumped :(
MultiPanelWithEvents.java
package GUIDemo1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MultiPanelWithEvents implements ActionListener {
/**
* @param args
*/
InputsPanel fInputsPanel;
JTextArea fTextOutput;
public void main(String[] args) {
// TODO Auto-generated method stub
Container content_pane = new Container();
// Set the layout as before with 1 row of 3 columns
// but now in one step.
content_pane.setLayout (new GridLayout (1, 3));
// First create a panel of buttons
ActionButtonsPanel buttons_panel =
new ActionButtonsPanel (this);
// Next create a panel of input fields and labels
fInputsPanel =
new InputsPanel ("Input x ", "1.5",
"Input y ", "3.14");
// Use a JTextArea for the output of the calculations.
fTextOutput = new JTextArea ();
fTextOutput.setBackground (Color.LIGHT_GRAY);
fTextOutput.setEditable (false);
// The grid fills the 3 columns sequentially.
content_pane.add (buttons_panel);
content_pane.add (fInputsPanel);
content_pane.add (fTextOutput);
} // init
/** Handle the button events.**/
public void actionPerformed (ActionEvent ae) {
String str1 = fInputsPanel.fTextfieldTop.getText ();
String str2 = fInputsPanel.fTextfieldBot.getText ();
double val1=0.0;
double val2=0.0;
try {
val1 = Double.parseDouble (str1);
val2 = Double.parseDouble (str2);
} catch (NumberFormatException nfe){
System.out.println ("Improper input");
}
if (ae.getActionCommand ().equals ("Add") ){
fTextOutput.setText ("x + y = " + (val1+val2));
} else {
fTextOutput.setText ("x * y = " + (val1*val2));
}
} // actionPerformed
} // class MultiPanelWithEvents
ActionButtonsPanel.java
package GUIDemo1;
import java.awt.event.*;
import javax.swing.*;
/** JPanel subclass with two buttons. **/
public class ActionButtonsPanel extends JPanel {
/** Constructor adds 2 buttons to the panel and
* adds the listener passed as an argument.
**/
ActionButtonsPanel (ActionListener listener) {
// Create two buttons
JButton add_but = new JButton ("Add" );
JButton mult_but = new JButton ("Mult");
add_but.addActionListener (listener);
mult_but.addActionListener (listener);
// Put a button in each grid cell
add (add_but);
add (mult_but);
} // ctor
} // class ActionButtonsPanel
InputsPanel.java
package GUIDemo1;
import java.awt.*;
import javax.swing.*;
/** Panel to hold input textfields. **/
public class InputsPanel extends JPanel {
JTextField fTextfieldTop=null;
JTextField fTextfieldBot=null;
/** Constructor builds panel with labels and text fields. **/
InputsPanel (String label_str_top, String init_top,
String label_str_bot, String init_bot) {
// Set the layout with 2 rows by 2 columns
setLayout (new GridLayout (2,2));
// Create two text fields with the initial values
fTextfieldTop = new JTextField (init_top);
fTextfieldBot = new JTextField (init_bot);
// Create the first label and right justify the text
JLabel label_top =
new JLabel (label_str_top, SwingConstants.RIGHT);
// Insert the label and textfield into the top grid row
add (label_top);
add (fTextfieldTop);
// Create the second label and right justify the text
JLabel label_bot =
new JLabel (label_str_bot, SwingConstants.RIGHT);
// Insert the second label and textfield into the bottom grid row
add (label_bot);
add (fTextfieldBot);
} // ctor
} // class InputsPanel
The build went just fine but it refuses to run. Stumped :(