G
Geoff Cox
Hello
The code below allows me to display an image and a question and when I
click on the "Next" button I can save the slider value (the answer to
the question) to a file.
I cannot at the moment see how I can display a new image and question
after the "Next" button has been clicked ...
Help!
Cheers
Geoff
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.io.*;
class Changes3 extends JFrame implements ChangeListener,
ActionListener
{
JSlider scale = new JSlider(-20,+20,0);
JLabel position = new JLabel("Set Position");
JButton nb = new JButton("Next");
public Changes3()
{
super("Slider");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container contentArea = getContentPane();
GridLayout lay = new GridLayout(3,1);
contentArea.setLayout(lay);
scale.setMajorTickSpacing(10);
scale.setMinorTickSpacing(5);
scale.setPaintTicks(true);
scale.setPaintLabels(true);
scale.addChangeListener(this);
JLabel text = new JLabel("Look at the picture and indicate
on the slider how you feel.");
ImageIcon picicon = new ImageIcon("pic1.jpg");
JButton buttonpic = new JButton(picicon);
nb.addActionListener(this);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
panel1.add(buttonpic);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.add(text);
JPanel panel3 = new JPanel();
panel3.setBackground(Color.white);
JLabel slidertext = new JLabel("-20 = Very Bad, +20 = Very
Good");
panel3.add(slidertext);
panel3.add(scale);
panel3.add(nb);
contentArea.add(panel1);
contentArea.add(panel2);
contentArea.add(panel3);
setContentPane(contentArea);
}
public void stateChanged(ChangeEvent event)
{
JSlider src = (JSlider) event.getSource();
if(!src.getValueIsAdjusting())
position.setText("Position is "+scale.getValue());
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == nb ) {
tofile();
}
}
public void tofile() {
try
{
FileWriter outfile = new FileWriter ("data.txt");
PrintWriter pw = new PrintWriter(outfile);
pw.println(scale.getValue());
pw.close();
}
catch (IOException e) { System.out.println(e); }
}
public static void main(String[] args)
{
Changes3 eg = new Changes3();
}
}
The code below allows me to display an image and a question and when I
click on the "Next" button I can save the slider value (the answer to
the question) to a file.
I cannot at the moment see how I can display a new image and question
after the "Next" button has been clicked ...
Help!
Cheers
Geoff
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.io.*;
class Changes3 extends JFrame implements ChangeListener,
ActionListener
{
JSlider scale = new JSlider(-20,+20,0);
JLabel position = new JLabel("Set Position");
JButton nb = new JButton("Next");
public Changes3()
{
super("Slider");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container contentArea = getContentPane();
GridLayout lay = new GridLayout(3,1);
contentArea.setLayout(lay);
scale.setMajorTickSpacing(10);
scale.setMinorTickSpacing(5);
scale.setPaintTicks(true);
scale.setPaintLabels(true);
scale.addChangeListener(this);
JLabel text = new JLabel("Look at the picture and indicate
on the slider how you feel.");
ImageIcon picicon = new ImageIcon("pic1.jpg");
JButton buttonpic = new JButton(picicon);
nb.addActionListener(this);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
panel1.add(buttonpic);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.add(text);
JPanel panel3 = new JPanel();
panel3.setBackground(Color.white);
JLabel slidertext = new JLabel("-20 = Very Bad, +20 = Very
Good");
panel3.add(slidertext);
panel3.add(scale);
panel3.add(nb);
contentArea.add(panel1);
contentArea.add(panel2);
contentArea.add(panel3);
setContentPane(contentArea);
}
public void stateChanged(ChangeEvent event)
{
JSlider src = (JSlider) event.getSource();
if(!src.getValueIsAdjusting())
position.setText("Position is "+scale.getValue());
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == nb ) {
tofile();
}
}
public void tofile() {
try
{
FileWriter outfile = new FileWriter ("data.txt");
PrintWriter pw = new PrintWriter(outfile);
pw.println(scale.getValue());
pw.close();
}
catch (IOException e) { System.out.println(e); }
}
public static void main(String[] args)
{
Changes3 eg = new Changes3();
}
}