Thread: JFrame problem
View Single Post
  #7 (permalink)  
Old 01-11-2008, 06:12 PM
saytri saytri is offline
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
Ok, thanks. You ideas were very useful. I have written this code so far (code below), but my problem is do i have to create a JLabel for each and every question. I had already wrote the questions in a textfile, and so i was wondering if there is a way by which i could call this textfile, instead of having to write the questions all over again, because if so, i think than the textfile has become useless if i have to creat labels for each question.

Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch14TextFrame1 extends JFrame implements ActionListener{ private static final int FRAME_WIDTH = 1024; private static final int FRAME_HEIGHT= 768; private static final int FRAME_X_ORIGIN = 0; private static final int FRAME_Y_ORIGIN = 0; private static final int BUTTON_WIDTH = 80; private static final int BUTTON_HEIGHT = 30; private JLabel prompt; private JTextField inputLine; public static void main(String[] args){ Ch14TextFrame1 frame = new Ch14TextFrame1(); frame.setVisible(true); } public Ch14TextFrame1(){ Container contentPane; JButton button1, button2, button3, button4, button5; setSize (FRAME_WIDTH, FRAME_HEIGHT); setResizable (true); setTitle ("Program Ch14TextFrame3"); setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane = getContentPane(); contentPane.setLayout(null); button1 = new JButton("Finish"); button1.setBounds(650,600,200,50); button1.setFont(new Font("Arial", Font.BOLD, 30)); button1.setBorder(BorderFactory.createRaisedBevelBorder()); button1.setBackground (Color.pink); button1.setForeground (Color.blue); prompt = new JLabel( ); prompt.setText("Plate Tectonics Quiz"); prompt.setSize(900,50); prompt.setLocation(300,30); prompt.setFont(new Font("Times New Roman", Font.BOLD, 40)); prompt.setForeground(Color.red); contentPane.add(prompt); prompt = new JLabel( ); prompt.setText(" 1)When the continents were joined together they were called a) Hellenic b) Pangaea c) Mariana"); prompt.setSize(900,70); prompt.setLocation(10,90); prompt.setFont(new Font("Times New Roman", Font.BOLD, 14)); prompt.setForeground(Color.black); contentPane.add(prompt); inputLine = new JTextField(); inputLine.setSize(100,20); inputLine.setLocation(625,115); contentPane.add(inputLine); inputLine.addActionListener(this); prompt = new JLabel( ); prompt.setText(" 2) When continents move towards each other they are called a) Constructive b) Destructive c) Conservative"); prompt.setSize(900,70); prompt.setLocation(10,120); prompt.setFont(new Font("Times New Roman", Font.BOLD, 14)); prompt.setForeground(Color.black); contentPane.add(prompt); inputLine = new JTextField(); inputLine.setSize(100,20); inputLine.setLocation(700,145); contentPane.add(inputLine); inputLine.addActionListener(this); prompt = new JLabel( ); prompt.setText(" 3) At what depth does a subducted plate melt? a) 100km b) 300km c) 20km"); prompt.setSize(900,70); prompt.setLocation(10,150); prompt.setFont(new Font("Times New Roman", Font.BOLD, 14)); prompt.setForeground(Color.black); contentPane.add(prompt); inputLine = new JTextField(); inputLine.setSize(100,20); inputLine.setLocation(500,175); contentPane.add(inputLine); inputLine.addActionListener(this); contentPane.add(button1); button1.addActionListener(this); button1.setActionCommand("b1"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent event) { if(event.getSource() instanceof JButton) { JButton clickedButton = (JButton) event.getSource(); String buttonText = clickedButton.getText(); setTitle("You clicked" + buttonText); String ac = event.getActionCommand(); if (ac.equals("b1")) { } } } }
Thanks again.

Last edited by saytri : 01-12-2008 at 01:52 PM.
Reply With Quote