I have a Jlabel called status that is empty. When I do status.setText() the first time it works normally but when I change it again it overlaps the first change instead of replacing it like it should. What is going on?
package panda.org;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;
public class NumberGame implements ActionListener{
JFrame frame;
JLabel rules;
JLabel rulesText;
JLabel rulesText2;
JLabel lets;
JButton play;
JButton exit;
JPanel panel;
Font myFont = new Font("Serif Plain", Font.BOLD, 15);
NumberGame() {
frame = new JFrame("NumberGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setResizable(true);
Image icon = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Gaming MSI\\Pictures\\Saved Pictures\\download (1).png");
frame.setIconImage(icon);
rules = new JLabel("Rules: ");
rules.setFont(myFont);
rules.setBounds(50, 100, 100, 75);
rulesText = new JLabel("We will pick a random number in the range of 1 -> 50.");
rulesText.setBounds(100, 100, 315, 75);
rulesText2 = new JLabel("Your job is to guess that number!");
rulesText2.setBounds(100, 120, 315, 75);
play = new JButton("Play");
play.setBounds(150, 300, 100, 75);
play.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int failedAttempts = 0;
JLabel label = new JLabel("Guess the number from 1 till 50");
label.setFont(myFont);
label.setBounds(150, 75, 315, 75);
JLabel hints = new JLabel("");
JTextField text = new JTextField();
text.setBounds(250, 150, 100, 25);
JButton check = new JButton("Check");
check.setBounds(150, 150, 75, 25);
double randomDouble = Math.random();
randomDouble = randomDouble * 50 + 1;
int randomInt = (int) randomDouble;
double finalRandomDouble = randomDouble;
check.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(randomInt);
String nb = text.getText();
int change = Integer.parseInt(nb);
JLabel status = new JLabel("");
status.setBounds(150, 160, 1000, 100);
frame.add(status);
if(randomInt == change) {
status.setText("You chose the correct number!");
status.setForeground(Color.green);
}
if(randomInt != change) {
status.setText("Wrong choice! Try again.");
status.setForeground(Color.red);
}
}
});
rules.setText("");
rulesText.setText("");
rulesText2.setText("");
frame.add(hints);
frame.add(label);
frame.add(check);
frame.add(text);
}
});
exit = new JButton("Exit");
exit.setBounds(350, 300, 100, 75);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(frame,"Are you sure want to exit?", "Exit",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(result == JOptionPane.YES_OPTION){
System.exit(0);
}else if (result == JOptionPane.NO_OPTION){
}else {
}
}
});
frame.add(play);
frame.add(exit);
frame.add(rules);
frame.add(rulesText);
frame.add(rulesText2);
frame.setVisible(true);
}
public static void main(String[] args) {
NumberGame number = new NumberGame();
}
#Override
public void actionPerformed(ActionEvent e) {
}
}
Someone asked for more code so this is all of my code! I hope this helps :D
The problem is in these lines
if(randomInt == change) {
status.setText("You chose the correct number!");
status.setForeground(Color.green);
}
if(randomInt != change) {
status.setText("Wrong choice! Try again.");
status.setForeground(Color.red);
}
}
This is how the result appears:
There are some improvements to do in your code:
You're using null-layout and setBounds(...) which is not advised it will give you more headaches than solutions, it may seem like the best / easiest way to build complex GUIs but it's not, here's an example of why. Swing has to deal with multiple OS, PLAFs, screen sizes and resolutions, let the layout managers do that work for you, all you have to do is combine them.
Every time you call check.addActionListener(new ActionListener() {, you're creating a new instance of your JLabel named status, and because you're using null-layout and you're placing it in the same position, they're overlapping.
Follow the first advice given in this answer, rebuild the whole thing with layout managers, move the status label as a class member and you shouldn't have any problems.
try this:
if(randomInt == change) {
status.setText("You chose the correct number!");
status.setForeground(Color.green);
}else{
status.setText("Wrong choice! Try again.");
status.setForeground(Color.red);
}
Related
I don't see why I would get this error because there is no boolean given, I gave 2 integers but it still tells me that there's an error.
Here's my code:
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class MainMenu
{
JFrame frame= new JFrame();
JFrame frame2 = new JFrame();
JFrame frame3 = new JFrame();
JButton button = new JButton("Singleplayer");
JButton button2 = new JButton("Multiplayer");
JButton b = new JButton("Submit");
JLabel label = new JLabel(" ");
int i;
int i2;
MainMenu(){
prepareGUI();
}
public void prepareGUI(){
frame.setTitle("Game");
frame.getContentPane().setLayout(null);
frame.add(button);
frame.add(button2);
button.setBounds(100,200,100,40);
button2.setBounds(200,200,100,40);
frame.setVisible(true);
frame.setBounds(200,200,400,400);
frame2.getContentPane().setLayout(null);
frame2.setBounds(200,200,400,400);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame3.getContentPane().setLayout(null);
frame3.setBounds(200,200,400,400);
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener buttonlistener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
frame2.setTitle("Multiplayer");
frame2.setVisible(true);
JLabel labelM = new JLabel("Geben sie eine Höhstzahl ein:");
JTextField hZahl = new JTextField();
JLabel labelN= new JLabel("Mit wie vielen Rateversuchen wollen sie `spielen?");`
JTextField rVers = new JTextField();
JButton b = new JButton("Submit");
labelM.setBounds(50, 50, 400, 70);
labelN.setBounds(50, 105, 400, 70);
rVers.setBounds(45, 150, 100, 30);
hZahl.setBounds(45, 95, 100, 30);
b.setBounds(150,250,100,40);
frame2.add(b);
frame2.add(hZahl);
frame2.add(labelM);
frame2.add(labelN);
frame2.add(rVers);
hZahl.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
System.out.println("The entered text is: " + hZahl.getText());
int i = Integer.parseInt(hZahl.getText());
System.out.println(i);
}
});
rVers.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
System.out.println("The entered text is: " + rVers.getText());
int i2 = Integer.parseInt(rVers.getText());
System.out.println(i2);
}
});
ActionListener buttonlistener3 = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
frame3.setTitle("1 Player Game");
frame3.setVisible(true);
}
};
b.addActionListener(buttonlistener3);
}
};
ActionListener buttonlistener2 = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
//frame.getContentPane().setBackground(Color.red);
//System.out.println("Multiplayer Selected");
frame2.setTitle("Multiplayer");
frame2.setVisible(true);
JLabel labelM = new JLabel("Geben sie eine Höhstzahl ein:");
JTextField hZahl = new JTextField();
JLabel labelN= new JLabel("Mit wie vielen Rateversuchen wollen sie spielen?");
JTextField rVers = new JTextField();
JButton b = new JButton("Submit");
labelM.setBounds(50, 50, 400, 70);
labelN.setBounds(50, 105, 400, 70);
rVers.setBounds(45, 150, 100, 30);
hZahl.setBounds(45, 95, 100, 30);
b.setBounds(150,250,100,40);
frame2.add(b);
frame2.add(hZahl);
frame2.add(labelM);
frame2.add(labelN);
frame2.add(rVers);
hZahl.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
System.out.println("The entered text is: " + hZahl.getText());
int i = Integer.parseInt(hZahl.getText());
System.out.println(i);
}
});
rVers.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
System.out.println("The entered text is: " + rVers.getText());
int i2 = Integer.parseInt(rVers.getText());
System.out.println(i2);
}
});
ActionListener buttonlistener3 = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
if(i && i2 < 0){
frame3.setTitle("2 Player Game");
frame3.setVisible(true);
}
}
};
b.addActionListener(buttonlistener3);
}
};
I am unsure as to why this is happening because as far as I am concerned I did not use a single boolean, and both i and i2 are separately and clearly marked as integers.
The error message appears here:
if(i && i2 < 0){
frame3.setTitle("2 Player Game");
frame3.setVisible(true);
}
Thank you in advance!
The < operator has more precedence than the && operator. Hence your statement will be computed as i && (i2 < 0). In this case i2 < 0 will be a boolean and i is an integer.
To solve this issue use the brackets as
(i < 0) && (i2 < 0)
i need a little help with introducing a method or something, that will close the desktop application after i press the default X button in the corner. I struggled with a lot of methods i found online, but i either don't know where to put it or i'm missing something...
Here's my code:
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
public class Calculator implements ActionListener {
Frame frame = new Frame();
Label label1 = new Label("First Number");
Label label2 = new Label("Operator");
Label label3 = new Label("Second Number");
Label label4 = new Label("Result");
TextField text1 = new TextField();
Choice choice = new Choice();
TextField text2 = new TextField();
TextField text3 = new TextField();
Button button = new Button("Calculate");
Calculator() {
label1.setBounds(50, 100, 100, 20);
label2.setBounds(50, 140, 100, 20);
label3.setBounds(50, 180, 100, 20);
label4.setBounds(50, 220, 100, 20);
text1.setBounds(200, 100, 100, 20);
choice.setBounds(200, 140, 100, 20);
text2.setBounds(200, 180, 100, 20);
text3.setBounds(200, 220, 100, 20);
button.setBounds(200, 200, 100, 20);
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.add(label4);
frame.add(text1);
frame.add(choice);
frame.add(text2);
frame.add(text3);
frame.add(button);
button.addActionListener(this);
choice.add("+");
choice.add("-");
choice.add("*");
choice.add("/");
frame.setLayout(null);
frame.setVisible(true);
frame.setSize(400, 350);
}
public void actionPerformed(ActionEvent event) {
try {
double number1 = Double.parseDouble(text1.getText());
double number2 = Double.parseDouble(text2.getText());
String selectedOperation = choice.getSelectedItem();
if (selectedOperation.equals("+"))
text3.setText(String.valueOf(number1 + number2));
else if (selectedOperation.equals("-"))
text3.setText(String.valueOf(number1 - number2));
else if (selectedOperation.equals("*"))
text3.setText(String.valueOf(number1 * number2));
else if (selectedOperation.equals("/")) {
if (number2 != 0)
text3.setText(String.valueOf(number1 / number2));
else System.out.println("Can't do that...");
}
} catch (NumberFormatException ignore) {
System.out.println("You can only introduce numbers!");
}
}
}
public class Main {
public static void main(String[] args) {
new Calculator();
}
}
Some help would be very appreciated, as i'm trying to learn Java!
Not quite sure why you are using AWT rather than Swing. As the latter tends to be recommended over the former.
But nevertheless - you can make the application exit by adding a window listener. E.g.:
frame.addWindowListener(new WindowAdapter()
{
#Override public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
As a side note, if you were using Swing rather than AWT (i.e. a JFrame rather than Frame) you can also do :
JFrame jframe = ....;
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
No one uses the AWT GUI anymore. Use Swing, JFrame, JButton, JLabel, JTextField. And then JFrame.setDefaultCloseOperation(EXIT_ON_CLOSE).
This question already has answers here:
Show an animated BG in Swing
(3 answers)
Closed 6 years ago.
im making a mathematical game and want to use a gif as a background. Its dimensions are 1100*800
I searched many posts how to add a GIF as background, but with no success. Any suggestions for a easy method (if using other components -JPanel,...; could you please show how?)
So far, this is my code of the JFrame:
public class Game extends JFrame implements ActionListener {
private JButton play, endG, tutorial, login, easy, medium, hard, next, checkAnswer;
private JTextArea answer;
int total, goodAnswer = 0;
public Game(String heading) {
super(heading);
this.setSize(1100, 800);
this.setLayout(null);
firstScreen();
setResizable(false);
}
public void firstScreen() {
getContentPane().removeAll();
play = new JButton();
play.setBounds(373, 350, 354, 80);
play.setIcon(new ImageIcon("entrancePlayButton.png"));
play.addActionListener(this);
play.setOpaque(false);
play.setContentAreaFilled(false);
add(play);
tutorial = new JButton("Tutorial");
tutorial.setBounds(345, 520, 150, 50);
tutorial.setFont(new Font("Arial", Font.PLAIN, 20));
tutorial.addActionListener(this);
tutorial.setOpaque(false);
tutorial.setContentAreaFilled(false);
add(tutorial);
endG = new JButton("End Game");
endG.setBounds(605, 520, 150, 50);
endG.setFont(new Font("Arial", Font.PLAIN, 20));
endG.addActionListener(this);
endG.setOpaque(false);
endG.setContentAreaFilled(false);
add(endG);
revalidate();
repaint();
}
public void difficultyScreen() {
getContentPane().removeAll();
easy = new JButton("Easy");
easy.setBounds(450, 310, 200, 80);
easy.setFont(new Font("Arial", Font.PLAIN, 30));
easy.addActionListener(this);
easy.setOpaque(false);
easy.setContentAreaFilled(false);
add(easy);
medium = new JButton("Medium");
medium.setBounds(450, 440, 200, 80);
medium.setFont(new Font("Arial", Font.PLAIN, 30));
medium.addActionListener(this);
medium.setOpaque(false);
medium.setContentAreaFilled(false);
add(medium);
hard = new JButton("Hard");
hard.setBounds(450, 570, 200, 80);
hard.setFont(new Font("Arial", Font.PLAIN, 30));
hard.addActionListener(this);
hard.setOpaque(false);
hard.setContentAreaFilled(false);
add(hard);
endG = new JButton("Exit");
endG.setBounds(1000, 700, 60, 30);
endG.setFont(new Font("Arial", Font.PLAIN, 15));
endG.addActionListener(this);
endG.setOpaque(false);
endG.setContentAreaFilled(false);
add(endG);
revalidate();
repaint();
}
public void playGameScreen() {
getContentPane().removeAll();
revalidate();
repaint();
}
public void tutorialScreen() {
getContentPane().removeAll();
revalidate();
repaint();
}
private static double stringToDouble(String number) {
double num = Double.parseDouble(number);
return num;
}
public static void main() {
Game areaGame = new Game("Area Game");
areaGame.setVisible(true);
}
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getSource() == play) {
difficultyScreen();
}
if (actionEvent.getSource() == tutorial) {
tutorialScreen();
}
if (actionEvent.getSource() == endG) {
int reply = JOptionPane.showConfirmDialog(null, "You are about to exit the game, are you sure?", "Exit game", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
}
There are two ways to do this:
you can override the paintComponent() method of your JPanel.
like this:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(yourImage, 0, 0, this);
}
or you can use a JLabel by loading the image as an ImageIcon and then displaying it in a JLabel.
I'm working on building a program that uses JFrame. What I want for my end result, is to implement an ActionListener which will remove labels when the user clicks a button. For example: when the user clicks the JButton, one of 5 labels is removed from the frame. When they click the button again, one of the remaining 4 labels is removed...and so on a so forth, until 0 labels remain. Technically, I have the program working as required however, I'm trying to see if there is a way to implement the ActionListener event via a loop as opposed to listing an if statement for each individual label. Thank you so much!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//calls for public class to inherit features of JFrame within Java
public class NoPurchaseReason extends JFrame implements ActionListener {
private int removeText = 0;
JButton btn = new JButton("Select");
JLabel lbl = new JLabel("Found better price");
JLabel lbl1 = new JLabel("Not as shown on website");
JLabel lbl2 = new JLabel("Wrong product");
JLabel lbl3 = new JLabel("Damaged upon delivery");
JLabel lbl4 = new JLabel("None of the above");
public static void main(String[] args) {
JFrame f = new NoPurchaseReason("Please tell us why you wish to return your purchase.");
f.setBounds(300, 100, 500, 500);
f.setVisible(true);
f.setBackground(Color.blue);
}
public NoPurchaseReason(String title) {
super(title);
setLayout(null);
lbl.setBounds(40, 40, 600, 40);
btn.setBounds(320, 10, 80, 20);
lbl.setBounds(100, 40, 100, 20);
lbl1.setBounds(100, 70, 100, 20);
lbl2.setBounds(100, 100, 150, 20);
lbl3.setBounds(100, 130, 100, 20);
lbl4.setBounds(100, 160, 100, 20);
add(btn);
add(lbl);
add(lbl);
add(lbl1);
add(lbl2);
add(lbl3);
add(lbl4);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
removeText++;
if (removeText == 1) {
lbl.setVisible(false);
lbl1.setBounds(100, 40, 100, 20);
lbl2.setBounds(100, 70, 100, 20);
lbl3.setBounds(100, 100, 150, 20);
lbl4.setBounds(100, 130, 100, 20);
}
if (removeText == 2) {
lbl1.setVisible(false);
lbl2.setBounds(100, 40, 100, 20);
lbl3.setBounds(100, 70, 150, 20);
lbl4.setBounds(100, 100, 100, 20);
}
if (removeText == 3) {
lbl2.setVisible(false);
lbl3.setBounds(100, 40, 150, 20);
lbl4.setBounds(100, 70, 100, 20);
}
if (removeText == 4) {
lbl3.setVisible(false);
lbl4.setBounds(100, 40, 100, 20);
}
if (removeText == 5) {
lbl4.setVisible(false);
}
}
}
Learning how to properly use layout managers will save you a lot of trouble in the long run.
You'll also find that people will tell you to adhere to the single responsibility principle, and avoid making classes that violate this principle (e.g., extending JFrame and implementing ActionListener).
You'll also hear folks tell you to prefer using actions over action listeners (if you need to share functionality across multiple components, that is).
A simple way would be to dedicate an entire panel to holding your labels, and simply remove the first label in the panel until there are no more labels. Here's an example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class LabelDemo
{
public static void main(String[] args) {
String[] labels = {
"Found better price",
"Not as shown on website",
"Wrong product",
"Damaged upon delivery",
"None of the above"
};
final JFrame frame = new JFrame();
final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for (String s: labels) {
panel.add(new JLabel(s));
}
frame.add(panel);
JButton button = new JButton("Select");
button.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
if (panel.getComponentCount() > 0)
panel.remove(0);
frame.repaint();
}
});
frame.add(button, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
}
Also, you may just have a certain goal in mind that I'm not aware of, but it honestly seems like a list would be better in this case. Here's an example of that as well:
String[] labels = {
"Found better price",
"Not as shown on website",
"Wrong product",
"Damaged upon delivery",
"None of the above"
};
JList<String> list = new JList<>(labels);
int option = JOptionPane.showConfirmDialog(null, list,
"Please tell us why you wish to return your purchase.",
JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
String selectedValue = list.getSelectedValue();
System.out.println(selectedValue); // Do something with it.
}
im doing a java vending machine os and I've just imported my original project into eclipse and added a guy page and since then its been throwing errors everywhere no mater what i do, can i get some help? the main error now is 'Syntax error on token(s), misplaced construct(s)' i do apologise in advance if the code is bad or inefficient of scraps laying around.
package JavaOS;
class OS {
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.util.*;
public class OS extends Frame {
ImageIcon ironman3 = new ImageIcon ("H:\\School\\TECH\\Javaimages\\ironman3");
ImageIcon dredd = new ImageIcon ("H:\\School\\TECH\\Javaimages\\dredd");
ImageIcon indiana = new ImageIcon ("H:\\School\\TECH\\Javaimages\\indiana");
ImageIcon startrek = new ImageIcon ("H:\\School\\TECH\\Javaimages\\startrek");
public static String newline = System.getProperty("line.separator");
private static final long serialVersionUID = 1L;
private static Object activate;
String movie = null;
static boolean rightCreditCard = false;
public static void main(String[] args) throws IOException {
slot.d(activate);
beginProgram();
Welcome_GUI();
System.out.println("Part A - Intalising JFrame Windows");
checkCard();
if (rightCreditCard == false) {
JFrame parent = new JFrame();
JOptionPane.showMessageDialog(parent, "Your Credit Card is invalid!");
checkCard();
} else {
JFrame parent = new JFrame();
JOptionPane.showMessageDialog(parent, "Your Credit Card is valid!");
}
}
//Scanner scanner = new Scanner(System.in);
//System.out.println(scanner.nextLine());
//PrintWriter out = new PrintWriter(new FileWriter("H:\\School\\TECH\\JavaFileOutputs\\outputfile.txt"));
//out.print("Hello ");
//out.println("world");
//out.close();
//BufferedReader in = new BufferedReader(new FileReader("H:\\School\\TECH\\JavaFileOutputs\\outputfile.txt"));
//String text = in.readLine();
//in.close();
//System.out.println(text);
public static void beginProgram() {
Object[] options = { "OK", "Cancel" };
int JOP_Start = JOptionPane.showOptionDialog(null,
"Do you want to begin program?",
"Start?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (JOP_Start == JOptionPane.NO_OPTION)
{
System.out.println("Program Terminated");
System.exit(0);
}
}
public static void checkCard() {
String number = JOptionPane.showInputDialog("Enter your creditcard");;
int sum1=0,sum2=0;
//find the first Sum
for(int i=number.length()-1;i>=0;i=i-2)
{
sum1 = sum1 + Character . getNumericValue( number . charAt(i));
}
//find the second sum
for(int i=number.length()-2;i>=0;i=i-2)
{
int doublenumber = 2 * Character . getNumericValue( number.charAt(i));
String doublestring = Integer.toString(doublenumber);
//System.out.println(doublenumber+ " "+doublestring);
for(int j=0;j<doublestring.length();j++)
{
//System.out.println( Character . getNumericValue( doublestring.charAt(j)));
sum2 = sum2 + Character . getNumericValue( doublestring.charAt(j)) ;
}
}
//System.out.println(sum1+" "+sum2);
//Check the result
if((sum1+sum2)%10 == 0) {
System.out.println("Valid Credit Card!");
JFrame parent = new JFrame();
JOptionPane.showMessageDialog(parent, "Your Credit Card is valid!");
boolean rightCreditCard = true;
}
else
{
System.out.println("Invalid Credit Card!");
boolean rightCreditCard = false;
//Suggesting right check digit
System.out.println(sum1+sum2);
int totalsum = sum1+sum2;
int lastdigitoftotalsum = totalsum%10;
int numbertoaddtocheckdigit = 10 - lastdigitoftotalsum;
int userenteredcheckdigit = Character . getNumericValue( number . charAt(number.length()-1));
int progsuggestedcheckdigit = userenteredcheckdigit + numbertoaddtocheckdigit;
System.out.println(lastdigitoftotalsum + " " + numbertoaddtocheckdigit + " " + userenteredcheckdigit + " "+ progsuggestedcheckdigit);
System.out.println("The check digit should be " + progsuggestedcheckdigit);
////////////////////////////
//CREDIT CARD CHECK /\ END//
////////////////////////////
}
}
public static void rentMovie() {
Object[] options = { "OK", "Cancel" };
int accept = JOptionPane.showOptionDialog(null,
"Are you shoure you wish to rent this movie?",
"???",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
options,
options[0]);
if (accept == JOptionPane.NO_OPTION)
{
System.out.println("You have not rented moive");
}
if (accept == JOptionPane.YES_OPTION)
{
System.out.println("You have rented moive");
slot.d(activate);
System.out.println("Slot dispenser activated");
}
}
public static void overDue() {
}
public static void delay(int i) {
try { //a wait or delay
Thread.sleep(i);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
public static void Welcome_GUI() {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new JPanel());
frame.pack();
frame.setVisible (true);
class MyPanel extends JPanel {
private JLabel jcomp1;
private JLabel jcomp2;
private JButton jcomp3;
private JLabel jcomp4;
private JButton jcomp5;
private JButton jcomp6;
private JButton jcomp7;
private JLabel jcomp8;
private JLabel jcomp9;
private JLabel jcomp10;
private JLabel jcomp11;
private JLabel jcomp12;
private JLabel jcomp13;
private JLabel jcomp14;
private JLabel jcomp15;
private JLabel jcomp16;
public MyPanel() {
//construct components
jcomp1 = new JLabel (" Welcome To Video Pro 2000-XD");
jcomp2 = new JLabel (" Iron Man 3");
jcomp3 = new JButton ("Rent");
jcomp4 = new JLabel ("Dredd 3D");
jcomp5 = new JButton ("Rent");
jcomp6 = new JButton ("Rent");
jcomp7 = new JButton ("Rent");
jcomp8 = new JLabel ("Indiana Jones");
jcomp9 = new JLabel ("Star Trek");
jcomp10 = new JLabel ("Into Darkness");
jcomp11 = new JLabel ("Kindom Of the ");
jcomp12 = new JLabel ("Crystal Skull");
jcomp13 = new JLabel ("PIC _STAR_TREK");
jcomp14 = new JLabel ("PIC_INDINA_JONES");
jcomp15 = new JLabel ("PIC_IRONMAN3");
jcomp16 = new JLabel ("PIC_DREDD_3D");
//adjust size and set layout
setPreferredSize (new Dimension (1241, 746));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
add (jcomp5);
add (jcomp6);
add (jcomp7);
add (jcomp8);
add (jcomp9);
add (jcomp10);
add (jcomp11);
add (jcomp12);
add (jcomp13);
add (jcomp14);
add (jcomp15);
add (jcomp16);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (285, 35, 210, 35);
jcomp2.setBounds (280, 170, 80, 30);
jcomp3.setBounds (280, 205, 100, 25);
jcomp4.setBounds (400, 170, 70, 30);
jcomp5.setBounds (400, 205, 100, 25);
jcomp6.setBounds (280, 580, 100, 25);
jcomp7.setBounds (400, 565, 100, 25);
jcomp8.setBounds (280, 500, 100, 25);
jcomp9.setBounds (400, 510, 100, 25);
jcomp10.setBounds (400, 535, 100, 25);
jcomp11.setBounds (280, 525, 100, 25);
jcomp12.setBounds (280, 550, 100, 25);
jcomp13.setBounds (530, 400, 220, 326);
jcomp14.setBounds (40, 400, 220, 326);
jcomp15.setBounds (40, 40, 220, 326);
jcomp16.setBounds (530, 40, 220, 326);
}
}
}
}
You have
class OS {
and also
public class OS extends Frame {
The first one should be removed.
Other than that, the object 'slot' was not declared and initialized.
After removing
class OS {
and two lines of
slot.d(activate);
your file should compile and run.