- Joined
- Nov 13, 2006
- Messages
- 15,643 (2.35/day)
- Location
- Mid-Atlantic
System Name | Desktop |
---|---|
Processor | i5 13600KF |
Motherboard | AsRock B760M Steel Legend Wifi |
Cooling | Noctua NH-U9S |
Memory | 4x 16 Gb Gskill S5 DDR5 @6000 |
Video Card(s) | Gigabyte Gaming OC 6750 XT 12GB |
Storage | WD_BLACK 4TB SN850x |
Display(s) | Gigabye M32U |
Case | Corsair Carbide 400C |
Audio Device(s) | On Board |
Power Supply | EVGA Supernova 650 P2 |
Mouse | MX Master 3s |
Keyboard | Logitech G915 Wireless Clicky |
Software | Fedora KDE Spin |
ok, in Magic(char letter)
secretWordArray is actually inputString.length() since you cant do a .length on an array. but that is fine since i guess Strings will be easier to work with.
the actionPerformed method is a toughy. when the user clicks the OK button it takes the text from tfInput and puts it in the string textfieldString. then i want to execute Magic. however it doesnt like the arguments for magic.
im using Eclipse !
secretWordArray is actually inputString.length() since you cant do a .length on an array. but that is fine since i guess Strings will be easier to work with.
the actionPerformed method is a toughy. when the user clicks the OK button it takes the text from tfInput and puts it in the string textfieldString. then i want to execute Magic. however it doesnt like the arguments for magic.
Code:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class timebombtest extends JFrame implements ActionListener
{
public String inputString = "";
public String textfieldString = "";
public String inputMask = "*";//mask
public String mask = "";
public String temp = "";
JTextField tfInput = new JTextField(2);
JLabel lblMessage = new JLabel("Guess A Letter: ");
JLabel maskedWord = new JLabel();
JLabel guessedLetter = new JLabel();
JButton btnOK = new JButton("OK");
JLabel timer = new JLabel("*-----");
public timebombtest() //constructor for GUI
{
super("Time Bomb Game");
inputString = JOptionPane.showInputDialog("Enter Secret Word: ");//user enters secret word
for(int i = 0; i < inputString.length(); i++)//gets the length of the secret word
temp += inputMask;//creates a mask of the secret word of the appropriate length
maskedWord = new JLabel(temp);
//set font
Font f1 = new Font("Dialog", Font.PLAIN, 24);//set main font
Font f2 = new Font("Dialog", Font.PLAIN, 32);//set larger font
lblMessage.setFont(f1);
tfInput.setFont(f1);
guessedLetter.setFont(f1);
btnOK.setFont(f1);
timer.setFont(f2);
maskedWord.setFont(f1);
//gui properties
getContentPane().setLayout(new BorderLayout(1,1));
JPanel northPanel = new JPanel();
northPanel.add(lblMessage);
northPanel.add(tfInput);
northPanel.add(btnOK);
btnOK.addActionListener(this);
getContentPane().add(northPanel, "North");
JPanel centerPanel = new JPanel();
centerPanel.add(maskedWord);
centerPanel.add(guessedLetter);
getContentPane().add(centerPanel, "Center");
JPanel southPanel = new JPanel();
southPanel.add(timer);
getContentPane().add(southPanel, "South");
setSize(350, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private Boolean Magic(char letter)
{
Boolean hit = false;
String temp = "";
for (int i = 0; i < inputString.length(); i++)
{
if (textfieldString.charAt(i) == inputMask.charAt(i))
{
if (letter == inputString.charAt(i))
{
temp += inputString.charAt(i);
hit = true;
}
else
{
temp += inputMask.charAt(i);
}
}
else
{
temp += textfieldString.charAt(i);
}
}
textfieldString = temp;
return hit;
}
public void actionPerformed(ActionEvent e)
{
if ("OK".equals(e.getActionCommand()))
{
textfieldString = tfInput.getText();
Magic();
}
}
public static void main(String[] args)
{
new timebombtest();
}
}//end TimeBomb class
im using Eclipse !