Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFatimah Anwar
Android Development Techdegree Student 10,416 PointsI need a small help with Java JFrame
I work on my project and I stuck here, I don't understand why I have to hide something to show the error message that associate to calculateButton. if i dont hide any component the error message never appear. I don't know where is the wrong
package com.company;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Lona");
JPanel panel = new JPanel();
JLabel label = new JLabel();
JTextField textField = new JTextField();
JButton clickButton = new JButton();
JButton calculateButton = new JButton();
JLabel label1 = new JLabel();
panel.setBackground(Color.decode("#CFB095"));
panel.setBorder(new EmptyBorder(30, 30, 30, 30));
frame.getContentPane().add(panel);
frame.setSize(new Dimension(600, 300));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Lona Salem");
frame.setResizable(false);
//sets elements
label.setText("Please enter the number of courses that you complete in Full 17/18 ");
label.setFont(new Font("Arial", Font.BOLD, 16));
label.setBorder(new EmptyBorder(20,0,10,0));
textField.setPreferredSize(new Dimension(300, 30));
clickButton.setText("CLICK");
calculateButton.setText("CALCULATE THE AVAREGE");
label1.setText("");
clickButton.addActionListener(new ActionListener() {
public JLabel headLabel = new JLabel();
public JTextField nameTextField1;
public JTextField gradeTextField;
public JLabel courseNameLabel;
public JLabel courseGradeLabel;
public JLabel error;
@Override
public void actionPerformed(ActionEvent actionEvent) {
label.setVisible(false);
textField.setVisible(false);
calculateButton.setVisible(false);
clickButton.setVisible(false);
headLabel.setText("Enter your courses names and your grades according to SIS");
headLabel.setFont(new Font("Arial", Font.BOLD, 16));
headLabel.setBorder(new EmptyBorder(0,0,0,0));
panel.add(headLabel);
for( int i = 0 ; i < Integer.parseInt(textField.getText()); i++){
nameTextField1 = new JTextField();
gradeTextField = new JTextField();
courseNameLabel = new JLabel();
courseGradeLabel = new JLabel();
courseNameLabel.setText("Course name");
nameTextField1.setPreferredSize(new Dimension(150,30));
courseGradeLabel.setText("Course Grade");
gradeTextField.setPreferredSize(new Dimension(150,30));
panel.add(courseNameLabel);
panel.add(nameTextField1);
panel.add(courseGradeLabel);
panel.add(gradeTextField);
}
}
});
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
label1.setVisible(false);
JLabel errorLabel = new JLabel();
errorLabel.setText("Opes .. You must enter your grades first");
errorLabel.setFont(new Font("Arial", Font.BOLD, 20));
errorLabel.setForeground(Color.decode("#800000"));
panel.add(errorLabel);
}
});
//add elements to JPanel
panel.add(label);
panel.add(textField);
panel.add(clickButton);
panel.add(calculateButton);
panel.add(label1);
}
}