Reputation: 111
so I'm writing a program for a class and it needs to be runnable via command prompt. IE javac filename.java then java filename. I wrote the code in eclipse which is why I'm having trouble. My code is as follows:
import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.Font;
public class QA extends JApplet implements ActionListener{
int y=0;
int x=0;
int a=0;
int b=0;
static int q=-1;
JButton btnYes = new JButton("YES");
JButton btnNo = new JButton("NO");
static JLabel lblNewLabel = new JLabel("Use the buttons to answer this question: Do you like pizza?");
public QA() {
getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
ColumnSpec.decode("220px"),
FormFactory.UNRELATED_GAP_COLSPEC,
ColumnSpec.decode("220px"),},
new RowSpec[] {
RowSpec.decode("97px"),
RowSpec.decode("50px"),
RowSpec.decode("73px"),
RowSpec.decode("42px"),}));
btnYes.setFont(new Font("Tahoma", Font.PLAIN, 10));
getContentPane().add(btnYes, "1, 4, right, fill");
btnYes.setActionCommand("Yes");
btnYes.addActionListener(this);
btnNo.setFont(new Font("Tahoma", Font.PLAIN, 10));
getContentPane().add(btnNo, "3, 4, left, fill");
btnNo.setActionCommand("No");
btnNo.addActionListener(this);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add(lblNewLabel, "1, 2, 3, 1, fill, fill");
}
public void actionPerformed(ActionEvent evt) {
Object cmd = evt.getActionCommand();
if (cmd == "Yes")
{
++q;
}
else if (cmd == "No")
{
++q;
}
}
}
}
the errors I get are along the lines of:
C:\Users\*****\Desktop>javac QA.java
QA.java:8: error: package com.jgoodies.forms.layout does not exist
import com.jgoodies.forms.layout.FormLayout;
^
QA.java:9: error: package com.jgoodies.forms.layout does not exist
import com.jgoodies.forms.layout.ColumnSpec;
^
QA.java:10: error: package com.jgoodies.forms.factories does not exist
import com.jgoodies.forms.factories.FormFactory;
^
QA.java:11: error: package com.jgoodies.forms.layout does not exist
import com.jgoodies.forms.layout.RowSpec;
^
QA.java:32: error: cannot find symbol
getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
^
symbol: class FormLayout
location: class QA
QA.java:32: error: cannot find symbol
getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
^
symbol: class ColumnSpec
location: class QA
QA.java:33: error: cannot find symbol
ColumnSpec.decode("220px"),
^
symbol: variable ColumnSpec
location: class QA
QA.java:34: error: cannot find symbol
FormFactory.UNRELATED_GAP_COLSPEC,
^
symbol: variable FormFactory
location: class QA
QA.java:35: error: cannot find symbol
ColumnSpec.decode("220px"),},
^
symbol: variable ColumnSpec
location: class QA
QA.java:36: error: cannot find symbol
new RowSpec[] {
^
symbol: class RowSpec
location: class QA
QA.java:37: error: cannot find symbol
RowSpec.decode("97px"),
^
symbol: variable RowSpec
location: class QA
QA.java:38: error: cannot find symbol
RowSpec.decode("50px"),
^
symbol: variable RowSpec
location: class QA
QA.java:39: error: cannot find symbol
RowSpec.decode("73px"),
^
symbol: variable RowSpec
location: class QA
QA.java:40: error: cannot find symbol
RowSpec.decode("42px"),}));
^
symbol: variable RowSpec
location: class QA
14 errors
Is there anything I can do? Thanks for any help!
Upvotes: 0
Views: 1625
Reputation: 159754
You're missing the JGoodies Forms jar from your classpath. You can download it from here
To compile:
javac .;forms-1.2.1.jar QA.java
To run:
appletviewer my-qa-test.html
where the HTML document contains an applet tag
<applet code="QA.class" width=400 height=75 arhive="forms-1.2.1.jar"> </applet>
Aside: When comparing String
contents, use String#equals
The ==
operator compares Object references. In this case apply separation of concerns by using anonymous ActionListener
classes:
btnYes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
q++;
}
});
Upvotes: 2