Click here to Skip to main content
15,885,670 members
Home / Discussions / Java
   

Java

 
GeneralRe: Java Pin
Richard MacCutchan27-Apr-20 1:32
mveRichard MacCutchan27-Apr-20 1:32 
GeneralRe: Java Pin
Babayomi27-Apr-20 11:30
Babayomi27-Apr-20 11:30 
RantRe: Java Pin
ZurdoDev27-Apr-20 1:23
professionalZurdoDev27-Apr-20 1:23 
QuestionAndroid Studio submission Pin
Gammill18-Apr-20 10:34
Gammill18-Apr-20 10:34 
AnswerRe: Android Studio submission Pin
Gammill18-Apr-20 13:52
Gammill18-Apr-20 13:52 
QuestionIntroduction to object oriented programming Pin
Member 1479832810-Apr-20 6:59
Member 1479832810-Apr-20 6:59 
AnswerRe: Introduction to object oriented programming Pin
Richard MacCutchan10-Apr-20 21:17
mveRichard MacCutchan10-Apr-20 21:17 
Questioncannot resolve Pin
MallardsReach30-Mar-20 3:32
MallardsReach30-Mar-20 3:32 
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

Java
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"); // Creating instance of JFrame
        frame.setSize(800, 600); // Setting the width and height of frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       	JPanel panel = new JPanel(); //Creating panel. Inside panels we can add textfields, buttons and other components.
		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); // Add panel to frame
       	frame.setVisible(true); // Setting the frame visibility to 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);
			
			//ImageIcon imageIcon = new ImageIcon("images/bird.gif");
			JLabel userLabel = new JLabel();  // Creating JLabel
			userLabel.setIcon(new ImageIcon("images/bird.gif"));
			userLabel.setHorizontalAlignment(SwingConstants.CENTER);
			userLabel.setFont(new Font("Verdana", Font.ITALIC, 16)); //set font
			userLabel.setBounds(500,20,125,125); 
			userLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));	
			panel.add(userLabel); // Add Label to panel
			
			JTextField txtNumOne = new JTextField(20);
			txtNumOne.setBounds(15,350,70,25);
			panel.add(txtNumOne); // Add TextField to panel
			
			JTextField txtNumTwo = new JTextField(20);
			txtNumTwo.setBounds(130,350,70,25);
			panel.add(txtNumTwo); // Add TextField to panel
			
			JLabel lblDPAnswer = new JLabel();  // Creating 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)); // NOI18N
	        lblDPAnswer.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
	        lblDPAnswer.setOpaque(true);
			panel.add(lblDPAnswer); // Add Label to panel
			
			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"); // Create button
			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); // Add btnSum to panel
		
			
			JRadioButton[] rbTable = new JRadioButton[13]; //set an array for 12 radioButtons			
			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(""); //Clear the displayTable
								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; //Add 12 tables radio buttons
				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); //call method
				        	 }
				      });
	}//End of Constructor
		
	public static void main(String[] args) 
	{
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MathsLesson();	
                
            }
        });
     } //End of void main
            
} // End of Class

AnswerRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 3:50
mveRichard MacCutchan30-Mar-20 3:50 
GeneralRe: cannot resolve Pin
MallardsReach30-Mar-20 6:29
MallardsReach30-Mar-20 6:29 
GeneralRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 6:45
mveRichard MacCutchan30-Mar-20 6:45 
GeneralRe: cannot resolve Pin
MallardsReach30-Mar-20 10:32
MallardsReach30-Mar-20 10:32 
GeneralRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 21:34
mveRichard MacCutchan30-Mar-20 21:34 
GeneralRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 22:52
mveRichard MacCutchan30-Mar-20 22:52 
GeneralRe: cannot resolve Pin
MallardsReach30-Mar-20 23:22
MallardsReach30-Mar-20 23:22 
QuestionRadio Button Selected Pin
MallardsReach25-Mar-20 9:50
MallardsReach25-Mar-20 9:50 
QuestionRe: Radio Button Selected Pin
ZurdoDev25-Mar-20 10:10
professionalZurdoDev25-Mar-20 10:10 
AnswerRe: Radio Button Selected Pin
MallardsReach25-Mar-20 10:33
MallardsReach25-Mar-20 10:33 
QuestionRe: Radio Button Selected Pin
ZurdoDev25-Mar-20 10:39
professionalZurdoDev25-Mar-20 10:39 
AnswerRe: Radio Button Selected Pin
MallardsReach25-Mar-20 11:11
MallardsReach25-Mar-20 11:11 
QuestionRe: Radio Button Selected Pin
ZurdoDev25-Mar-20 12:19
professionalZurdoDev25-Mar-20 12:19 
AnswerRe: Radio Button Selected Pin
Richard MacCutchan26-Mar-20 0:16
mveRichard MacCutchan26-Mar-20 0:16 
GeneralRe: Radio Button Selected Pin
MallardsReach26-Mar-20 0:28
MallardsReach26-Mar-20 0:28 
GeneralRe: Radio Button Selected Pin
Richard MacCutchan26-Mar-20 0:36
mveRichard MacCutchan26-Mar-20 0:36 
GeneralRe: Radio Button Selected Pin
MallardsReach26-Mar-20 2:55
MallardsReach26-Mar-20 2:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.