So, I have seen many similar questions but they are all a bit different and don't seem to fit my problem.
So, I have a "Main" class, a "Panel" class and a "Day" class. In my Panel class, I have created a JFrame obejct and inside my Panel class I have created a JButton and a JTextField. When the content of the text field is "Monday" I want to display the name of the day and rather I have school on this day. For that, I want to use JLabel. Since the method of displaying the name of the day etc is inside the Day class, I have to create the JLabel object inside the Panel class but edit it inside the Day class.
However, when I use text.setText("Test"); it does not compile. In Eclipse, it gives the following error: text cannot be resolved (text is my JLabel object). I think that the Day class simply doesn't know the text object (even when I make the text object static, which helped in similar cases).
Edit: adjusted to the proposal from DevilsHnd. Still doesn't work tho
Day Class:
package basicsPack;
import java.awt.Color;
import basicsPack.Panel.*;
import javax.swing.JLabel;
public class Day{
boolean isSchool;
String[] stunden;
String name;
public Day(boolean isSchool, String[] stunden, String name) {
this.isSchool = isSchool;
this.stunden = stunden;
this.name = name;
}
public void sagName() {
System.out.println(this.isSchool);
System.out.println(this.name);
text.setText("Label Text"); //This is what gives an error.
}
}
Panel Class:
package basicsPack;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Panel extends JPanel implements ActionListener {
//Screen Settings
final int originalTileSize = 16;
final int scale = 3;
final int tileSize = originalTileSize * scale;
final int maxScreenCol = 16;
final int maxScreenRow = 12;
final int screenWidth = tileSize * maxScreenCol;
final int screenHeight = tileSize * maxScreenRow;
JLabel text;
JButton knopf;
JTextField field;
Tage days;
String textInhalt;
public Panel(){
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.black);
this.setDoubleBuffered(true);
this.setFocusable(true);
//Initialize Button
knopf = new JButton("klick");
knopf.addActionListener(this);
knopf.setPreferredSize(new Dimension (100, 35));
//Initialize Text field
field = new JTextField();
field.setFont(new Font("Serif",Font.BOLD,30));
field.setPreferredSize(new Dimension (200, 50));
text = new JLabel("asd");
text.setForeground(Color.red);
this.add(field);
this.add(knopf);
this.add(text);
}
public void loop() {
try (Scanner input = new Scanner(System.in))
{
System.out.println("welchen Tag? ");
switch(textInhalt)
{
case "Montag":
Montag.sagName();
break;
case "Dienstag":
Dienstag.sagName();
break;
case "Mittwoch":
Mittwoch.sagName();
break;
case "Donnerstag":
Donnerstag.sagName();
break;
case "Freitag":
Freitag.sagName();
break;
case "Samstag":
Samstag.sagName();
break;
case "Sonntag":
Sonntag.sagName();
break;
}
}
}
static String[] monHours = {"Bio", "Bio", "Mathe", "Mathe", "Chemie", "Chemie", "frei", "Musik", "Musik"};
static String[] dinHours = {"", "", "", "", "", "", "", "", ""};
static String[] mitHours = {"", "", "", "", "", "", "", "", ""};
static String[] donHours = {"", "", "", "", "", "", "", "", ""};
static String[] freHours = {"", "", "", "", "", "", "", "", ""};
static String[] samHours = {"", "", "", "", "", "", "", "", ""};
static String[] sonHours = {"", "", "", "", "", "", "", "", ""};
static Day Montag = new Tage(true, monHours, "Montag");
static Day Dienstag = new Tage(true, dinHours, "Dinestag") ;
static Day Mittwoch = new Tage(true, mitHours, "Mittwoch") ;
static Day Donnerstag = new Tage(true, donHours, "Donnerstag") ;
static Day Freitag = new Tage(true, freHours, "Freitag") ;
static Day Samstag = new Tage(false, samHours, "Samstag") ;
static Day Sonntag = new Tage(false, sonHours, "Sonntag") ;
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==knopf) {
textInhalt = field.getText();
loop();
}
}
}
CodePudding user response:
You haven't declared the JLabel as a class member to make it class global, for example:
public class Panel extends JPanel implements ActionListener {
//Screen Settings
final int originalTileSize = 16;
final int scale = 3;
final int tileSize = originalTileSize * scale;
final int maxScreenCol = 16;
final int maxScreenRow = 12;
final int screenWidth = tileSize * maxScreenCol;
final int screenHeight = tileSize * maxScreenRow;
JButton knopf;
JTextField field;
JLabel text; // <-- Added!!
Tage days;
String textInhalt;
public Panel(){
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.black);
this.setDoubleBuffered(true);
this.setFocusable(true);
//Initialize Button
knopf = new JButton("klick");
knopf.addActionListener(this);
knopf.setPreferredSize(new Dimension (100, 35));
//Initialize Text field
field = new JTextField();
field.setFont(new Font("Serif",Font.BOLD,30));
field.setPreferredSize(new Dimension (200, 50));
text = new JLabel("asd"); // <-- Modified!!
text.setForeground(Color.red);
this.add(field);
this.add(knopf);
CodePudding user response:
Your Day
class is a data class. It shouldn't have any references to the JPanel
at all.
Here's what the Day
class should look like:
package basicspack;
public class Day {
boolean isSchool;
String[] stunden;
String name;
public Day(boolean isSchool, String[] stunden, String name) {
this.isSchool = isSchool;
this.stunden = stunden;
this.name = name;
}
public boolean isSchool() {
return isSchool;
}
public void setSchool(boolean isSchool) {
this.isSchool = isSchool;
}
public String[] getStunden() {
return stunden;
}
public void setStunden(String[] stunden) {
this.stunden = stunden;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
You change the text of your JLabel
in your ActionListener
by calling a Day
getter method.
Don't name your classes (Panel
) the same as the Java standard classes (java.awt.Panel
).