public class HängaGubbe2 extends javax.swing.JPanel {
private String valtOrd;
public HängaGubbe2() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jProgressBar1 = new javax.swing.JProgressBar();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("Gissa på en bokstav i ordet");
jButton1.setText("Hämta nytt ord");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(104, 104, 104)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(95, 95, 95)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
.addGap(7, 7, 7))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jButton1)
{
jProgressBar1.setValue(jProgressBar1.getMinimum());
try {
Scanner sc = new Scanner(new FileReader("ord.txt"));
java.util.List<String> ord = new ArrayList<String>();
while (sc.hasNext())
{
ord.add(sc.next());
}
Random rand = new Random();
int slump = rand.nextInt(ord.size());
valtOrd = ord.get(slump);
char[] array = new char[valtOrd.length()];
Arrays.fill(array, '?');
String hemligtOrd = new String(array);
jLabel3.setText(hemligtOrd);
}
catch (FileNotFoundException fe){
System.out.println("Filen finns inte");
}
}
else if (e.getSource() == jTextField1)
{
String txt = jTextField1.getText();
if(valtOrd.indexOf(txt)!=-1)
{
JOptionPane.showMessageDialog(null, "Bokstaven hittades i ordet!");
if(valtOrd.indexOf(txt)==valtOrd.lastIndexOf(txt))
{
char [] hit = new char[jLabel3.getText().length()];
Arrays.fill(hit, '?');
char c = txt.charAt(0);
hit[valtOrd.indexOf(txt)] = c;
String hemligtOrd = new String(hit);
jLabel3.setText(hemligtOrd);
}
}
else
{
JOptionPane.showMessageDialog(null, "Bokstaven finns inte!");
jProgressBar1.setValue(jProgressBar1.getValue()+1);
}
}
}
public static void main(String [] arg)
{
HängaGubbe2 h = new HängaGubbe2();
}
}