|
There are thousands of helpful tutorials online. But you have probably left it too late to make use of any of them.
|
|
|
|
|
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();
}
});
}
}
|
|
|
|
|
You are creating local variables in one method, and then trying to access them from other methods. You need to understand the scope rules, i.e whether variables are local or not. So txtNumOne and txtNumTwo should be class level variables. Alternatively they could be passed as parameters to the showAnswer method.
You have a potential mistake in the following code:
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);
}
|
|
|
|
|
Thanks Richard after many hours of head scratching and internet searching I think I have it.
Below is the class level variables and the method showAnswer.
double decNum1, decNum2, decAnswer;
String strAnswer;
void showAnswer(int dPlaces){
Double numAnswer;
numAnswer = decNum1 * decNum2;
strAnswer = String.format("%,." + dPlaces + "f", numAnswer);
}
And below is the 3 radiobuttons and the sum button that call the method when clicked.
rbOneDecimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
decNum1 = Double.parseDouble(txtNumOne.getText());
decNum2 = Double.parseDouble(txtNumTwo.getText());
showAnswer(1);
lblDPAnswer.setText(strAnswer);
}
});
rbTwoDecimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
decNum1 = Double.parseDouble(txtNumOne.getText());
decNum2 = Double.parseDouble(txtNumTwo.getText());
showAnswer(2);
lblDPAnswer.setText(strAnswer);
}
});
rbThreeDecimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
decNum1 = Double.parseDouble(txtNumOne.getText());
decNum2 = Double.parseDouble(txtNumTwo.getText());
showAnswer(3);
lblDPAnswer.setText(strAnswer);
}
});
btnSum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
decNum1 = Double.parseDouble(txtNumOne.getText());
decNum2 = Double.parseDouble(txtNumTwo.getText());
showAnswer(0);
lblDPAnswer.setText(strAnswer);
}
});
Could it be done a better way? I seem to be repeating these 2 lines.
decNum1 = Double.parseDouble(txtNumOne.getText());
decNum2 = Double.parseDouble(txtNumTwo.getText());
|
|
|
|
|
Rather than make these variables at class level, make them parameters to showAnswer (actually calcAnswer would be a better name) method. And the final result shoud be the return value. Something like:
String showAnswer(double decNum1, double decNum2, int dPlaces){
Double numAnswer = decNum1 * decNum2;
String strAnswer = String.format("%,." + dPlaces + "f", numAnswer);
return strAnswer;
}
That keeps everything neatly within the method.
You can reduce all the duplication by using the same handler for all the buttons.
public void actionPerformed(ActionEvent e) {
int numDecimals =
double decNum1 = Double.parseDouble(txtNumOne.getText());
double decNum2 = Double.parseDouble(txtNumTwo.getText());
String strResult = showAnswer(decNum1, decNum2, numDecimals);
lblDPAnswer.setText(strResult);
}
Then add that to each button:
rbOneDecimal.addActionListener(actionPerformed);
rbTwoDecimal.addActionListener(actionPerformed);
You may need to check the syntax requirements for these last two/three items
|
|
|
|
|
Thought it wouldn't work as easy as that.
errors for the actionPerformed method,
Quote: Multiple markers at this line
- void is an invalid type for the variable actionPerformed
- Syntax error on token "(", ; expected
- Syntax error on token ")", ; expected
errors for all the buttons .addActionListener(ActionPerformed);
Quote: ActionPerformed cannot be resolved to a variable
|
|
|
|
|
I hesitate to say this yet again, but please go to The Java™ Tutorials[^] and study the language properly. You are not going to learn it piecemeal by posting questions here, as each issue is looked at in isolation. You need to follow a structured learning path to get a full understanding of, not just the language, but its framework support as well.
|
|
|
|
|
|
Thanks for all the help, will go and look at the link that you have given.
Just realised all the examples can be downloaded and run in netbeans or using notepad++ / eclipse.
So not only can you read the tutorials you can run and walk through them.
I think I made the fundamental mistake of looking at various forums and java related sites picking up bits of code from here and there, only half of which I understand.
So I have deleted all links, and will now go and read the tutorials.
See you in about 6months time.
modified 31-Mar-20 6:10am.
|
|
|
|
|
Below is the code for 12 radiobuttons it compiles and runs but when I test out the first 2 buttons nothing happens. I have setActionCommand in the loop for each button so What is it I've not done?
ButtonGroup bgTableButtons = new ButtonGroup();
JRadioButton[] rbTable = new JRadioButton[13];
int y = 20;
for (int i = 1; i < rbTable.length; i++)
{
rbTable[i] = new JRadioButton(Integer.toString(i) + " Times");
rbTable[i].setActionCommand(Integer.toString(i));
rbTable[i].setBounds(320, y, 80, 20);
bgTableButtons.add(rbTable[i]);
panel.add(rbTable[i]);
y = y + 20;
}
if(rbTable[1].isSelected())
{
userText.setText("Button 1");
}
else if(rbTable[2].isSelected())
{
userText.setText("Button 2");
}
|
|
|
|
|
Do you have code for that implements the action command?
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
I have the following imports that's all
What other code do I need? the examles I've looked just show isSelected and that's all.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
|
|
|
|
|
Why are you setting setActionCommand to a number?
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Because I tried setting it to the name of the radiobutton but that didn't work either.
rbTable[i].setActionCommand(Integer.toString(i) + " Times");
|
|
|
|
|
Do you know what the command is for?
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
That is not the way to do it I am afraid. Visual components use event handlers which are signalled when the events (e.g. pushing a button) occurs. So you need to use the Button (Java Platform SE 7 ) addActionListener[^] method so your code gets called at the relevant time.
I would again strongly suggest you go to The Java™ Tutorials[^] and work through them a few times. Trying to learn a language and framework by posting questions here (or any other forum) will take you much longer to get to where you want.
|
|
|
|
|
Thanks Richard
I have and do look at the Java Tutorials but don't always find them helpful even though they explain the concepts of components.
I did eventually figure it out, the code is below for anyone else to look through,
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())) {
userText.setText(rbTable[i].getText());
}
}
}
}
};
|
|
|
|
|
MallardsReach wrote: don't always find them helpful even though they explain the concepts of components. What about all the sample code that's included? Seriously, you really should persevere with them. They teach things in a structured order and you get all the basic concepts well established before moving on to the more advanced stuff. I used the published version of these years ago when I first started working in Java, and have gone back and repeated the online versions a number of times.
|
|
|
|
|
Hi Richard,
Can't argue and have no wish to, with the points you make about the sample code,
The thing is they are stand alone ie
Here's a method, here's how to call it.
Here' some radio buttons, here's how to find the one selected.
And they work fine the problem is when your project gets more added to it and includes many components and then it's trying to get them to work with each other.
I am going to get 'Java: The Complete Reference, Eleventh Edition' as soon as things get back to normal.
Until then I have to rely on the tutorials and forums to guide me.
|
|
|
|
|
Talk about making things complicated!
Here is the final code, no user defined method was needed, I just had to place the results for pressing a button in the 'itemStateChanged method'
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("");
int firstNum, answerNum;
for (firstNum = 1; firstNum <=12; firstNum ++)
{
answerNum = firstNum * i;
displayTable.append(" " + firstNum + " x " + i + " = " + answerNum +"\n");
}
}
}
}
}
};
|
|
|
|
|
Looked at many examples of Method and they all seem easy until I start to place them in my code.
No matter where I insert the method or how I rewrite it I still get a problem.
Below is my Method followed by my code. What am I not doing?
void getSecond(int secondNum){
displayTable.setText("\n");
int firstNum, answerNum;
for (firstNum = 1; firstNum <=12; firstNum ++){
answerNum = firstNum * secondNum;
displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
}
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyTables
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Tables");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.RED));
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel)
{
panel.setLayout(null);
JTextArea displayTable = new JTextArea("Text Area");
displayTable.setEditable(false);
displayTable.setColumns(20);
displayTable.setFont(new Font("Times New Roman", 1, 14));
displayTable.setLineWrap(true);
displayTable.setRows(14);
displayTable.setWrapStyleWord(true);
displayTable.setBounds(200,20,230,260);
displayTable.setBorder(BorderFactory.createLineBorder(Color.GREEN));
displayTable.setMaximumSize(new java.awt.Dimension(5, 22));
panel.add(displayTable);
JLabel userLabel = new JLabel("Enter a Number");
userLabel.setHorizontalAlignment(SwingConstants.CENTER);
userLabel.setFont(new Font("Verdana", Font.ITALIC, 16));
userLabel.setBounds(10,20,180,25);
userLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(10,50,165,25);
panel.add(userText);
JButton loginButton = new JButton("Enter");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
displayTable.setText("Button Pressed");
}
});
}
}
|
|
|
|
|
MallardsReach wrote: I still get a problem. That is one of the most common reports that we see here, and also the most useless. Unless you tell us exactly what the problem is (including exact error messages), and where it occurs, then we can do very little. Please try to help us to help you.
|
|
|
|
|
Sorry Richard, honestly thought I had given the error.
If I place the code here
public class MyTables
{
void getSecond(int secondNum){
displayTable.setText("\n");
int firstNum, answerNum;
for (firstNum = 1; firstNum <=12; firstNum ++){
answerNum = firstNum * secondNum;
displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
}
public static void main(String[] args)
I get this message
Quote: c:\Java>javac MyTables.java
MyTables.java:18: error: illegal start of expression
public static void main(String[] args)
^
1 error
If I put it at the end
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
displayTable.setText("Button Pressed");
}
void getSecond(int secondNum){
displayTable.setText("\n");
int firstNum, answerNum;
for (firstNum = 1; firstNum <=12; firstNum ++){
answerNum = firstNum * secondNum;
displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
}
});
}
}
I get 3 error messages
Quote: c:\Java>javac MyTables.java
MyTables.java:69: error: illegal start of type
});
^
MyTables.java:70: error: ')' expected
}
^
MyTables.java:71: error: reached end of file while parsing
}
^
3 errors
|
|
|
|
|
Look at the code, it has two opening brace characters ({ ), one for the method, and one for the for loop. But it only has one closing brace, for the for loop. So you just need to add the one that closes the method block.
|
|
|
|
|
That's done it, although I am annoyed as I knew the curly bracket was missing but when I added it I got different errors.
After your reply I removed the method placed it in a new page and added the bracket so that it had 2 opening and 2 closing, placed it back in my project and it worked.
I do remember that every opening ( or { had to have a matching one closing.
Can now carry on with the project.
Thank you
|
|
|
|
|