Below is the full code I am using but I get the following error messages showing at the start of my code
Quote: Multiple markers at this line
- txtNumOne cannot be resolved
- txtNumTwo cannot be resolved
and
Quote: lblDPAnswer cannot be resolved
package components;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MathsLesson
{
String strAnswer;
void showAnswer(int dPlaces){
Double numAnswer; String strAnswer;
numAnswer = (Double.parseDouble(txtNumOne.getText()))* (Double.parseDouble(txtNumTwo.getText()));
strAnswer = String.format("%,." + dPlaces + "f", numAnswer);
lblDPAnswer.setText(strAnswer);
}
public MathsLesson()
{
JFrame frame = new JFrame("Tables");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, new java.awt.Color(255, 51, 51), new java.awt.Color(153, 0, 51)));
frame.add(panel);
frame.setVisible(true);
panel.setLayout(null);
JPanel tablepanel = new JPanel();
tablepanel.setLayout(null);
tablepanel.setBounds(10,20,320,300);
tablepanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Times Tables"));
tablepanel.setOpaque(false);
panel.add(tablepanel);
panel.setLayout(null);
JPanel decimalpanel = new JPanel();
decimalpanel.setLayout(null);
decimalpanel.setBounds(10,320,320,100);
decimalpanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Decimal Places"));
decimalpanel.setOpaque(false);
panel.add(decimalpanel);
JTextArea displayTable = new JTextArea();
displayTable.setEditable(false);
displayTable.setFont(new java.awt.Font("Times New Roman", 0, 18));
displayTable.setText("Choose a button on the right to view the table you want to see");
displayTable.setLineWrap(true);
displayTable.setWrapStyleWord(true);
displayTable.setBounds(10,20,150,260);
displayTable.setBorder(BorderFactory.createLineBorder(Color.GREEN));
tablepanel.add(displayTable);
JLabel userLabel = new JLabel();
userLabel.setIcon(new ImageIcon("images/bird.gif"));
userLabel.setHorizontalAlignment(SwingConstants.CENTER);
userLabel.setFont(new Font("Verdana", Font.ITALIC, 16));
userLabel.setBounds(500,20,125,125);
userLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
panel.add(userLabel);
JTextField txtNumOne = new JTextField(20);
txtNumOne.setBounds(15,350,70,25);
panel.add(txtNumOne);
JTextField txtNumTwo = new JTextField(20);
txtNumTwo.setBounds(130,350,70,25);
panel.add(txtNumTwo);
JLabel lblDPAnswer = new JLabel();
lblDPAnswer.setHorizontalAlignment(SwingConstants.CENTER);
lblDPAnswer.setBounds(220,350,100,25);
lblDPAnswer.setBackground(new java.awt.Color(255, 255, 255));
lblDPAnswer.setFont(new java.awt.Font("Times New Roman", 1, 18));
lblDPAnswer.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
lblDPAnswer.setOpaque(true);
panel.add(lblDPAnswer);
ButtonGroup bgDecimalButtons = new ButtonGroup();
JRadioButton rbOneDecimal = new JRadioButton("1 DP");
bgDecimalButtons.add(rbOneDecimal);
rbOneDecimal.setFont(new java.awt.Font("Times New Roman", 0, 14));
rbOneDecimal.setBounds(15, 380, 60, 30);
panel.add(rbOneDecimal);
JRadioButton rbTwoDecimal = new JRadioButton("2 DP");
bgDecimalButtons.add(rbTwoDecimal);
rbTwoDecimal.setFont(new java.awt.Font("Times New Roman", 0, 14));
rbTwoDecimal.setBounds(85, 380, 60, 30);
panel.add(rbTwoDecimal);
JRadioButton rbThreeDecimal = new JRadioButton("3 DP");
bgDecimalButtons.add(rbThreeDecimal);
rbThreeDecimal.setFont(new java.awt.Font("Times New Roman", 0, 14));
rbThreeDecimal.setBounds(155, 380, 60, 30);
panel.add(rbThreeDecimal);
JButton btnSum = new JButton("Enter");
btnSum.setBounds(220, 380, 100, 25);
btnSum.setFont(new java.awt.Font("Times New Roman", 1, 36));
btnSum.setText("=");
btnSum.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
panel.add(btnSum);
JRadioButton[] rbTable = new JRadioButton[13];
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
AbstractButton aButton = (AbstractButton)itemEvent.getSource();
int state = itemEvent.getStateChange();
String name = aButton.getText();
for (int i = 1; i < rbTable.length; i++)
{
if (state == ItemEvent.SELECTED) {
if (name.equals(rbTable[i].getText())) {
displayTable.setText("");
tablepanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),+ i + " Times Table"));
int firstNum, answerNum;
for (firstNum = 1; firstNum <=12; firstNum ++)
{
answerNum = firstNum * i;
displayTable.append(" " + firstNum + " x " + i + " = " + answerNum +"\n");
}
}
}
}
}
};
ButtonGroup bgTableButtons = new ButtonGroup();
int y = 50, x = 165;
for (int i = 1; i < rbTable.length; i++)
{
rbTable[i] = new JRadioButton("X " + Integer.toString(i));
rbTable[i].setFont(new java.awt.Font("Times New Roman", 0, 18));
rbTable[i].addItemListener(itemListener);
rbTable[i].setBounds(x, y, 70, 20);
if(i == 6){
x = 235;
y = 20;
}
bgTableButtons.add(rbTable[i]);
tablepanel.add(rbTable[i]);
y = y + 30;
}
rbOneDecimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblDPAnswer.setText("Button 1");
}
});
rbTwoDecimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblDPAnswer.setText("Button 2");
}
});
rbThreeDecimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblDPAnswer.setText("Button 3");
}
});
btnSum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAnswer(0);
}
});
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MathsLesson();
}
});
}
}
|