This is the frame:
import javax.swing.JFrame;
import java.awt.Dimension;
PHP Code:
public class SimpleUiFrame
{
public static void main ( String [ ] args )
{
//establish panel
SimpleUi_Panel panel = new SimpleUi_Panel ( );
//establish frame
JFrame frame = new JFrame ( );
frame.add ( panel );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.setPreferredSize ( new Dimension ( 800, 600 ) );
frame.pack ( );
frame.setVisible ( true );
}
}
This is the panel:
PHP Code:
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
public class SimpleUi_Panel extends JPanel
{
//attributes
//initilize stuff for panel 0
//radio buttons
private JTextField textField0 = null;
private JRadioButton radioButtons [ ] = null;
private int numRadioButtons = 2;
//radio button container
private JPanel panel0_subContainer0 = new JPanel ( ), panel0_subContainer1 = new JPanel ( ), panel0_container = new JPanel ( );
//a button to go to panel 1
private JButton proceed_button = new JButton ( "go to question info(OK)" );
private JButton cancel_button = new JButton ( "cancel" );
//initialise stuff for panel 1
//textboxxes
private JTextField textField1 = null;
private JButton exit_button = new JButton ( "exit question info" );
private JPanel panel1_subContainer = new JPanel ( ), panel1_container = new JPanel ( );
//constructor
public SimpleUi_Panel ( )
{
//setup panel 0 stuff
//initialise text field
textField0 = new JTextField ( "Enter num questions : " );
panel0_subContainer0.add ( textField0 );
//layout the contents of panel0_subContainer using box layout
panel0_subContainer0.setLayout ( new BoxLayout ( panel0_subContainer0, BoxLayout.X_AXIS ) );
//add action listener to button
proceed_button.addActionListener ( new actionListening ( ) );
cancel_button.addActionListener ( new actionListening ( ) );
//add radio button panel + proceed button to this panel
panel0_subContainer1.add ( proceed_button );
panel0_subContainer1.add ( cancel_button );
//layout the contents of panel0_subContainer using box layout
panel0_subContainer1.setLayout ( new BoxLayout ( panel0_subContainer1, BoxLayout.X_AXIS ) );
//add buttons to sub container 1
panel0_container.add ( panel0_subContainer0 );
panel0_container.add ( panel0_subContainer1 );
//layout the contents of panel0_container using box layout
panel0_container.setLayout ( new BoxLayout ( panel0_container, BoxLayout.Y_AXIS ) );
add ( panel0_container );
//setup panel 1 stuff
//initialise text field
textField1 = new JTextField ( "LostWind" );
panel1_container.add ( textField1 );
panel1_container.add ( exit_button );
panel1_container.setLayout ( new BoxLayout ( panel1_container, BoxLayout.Y_AXIS ) );
//add action listener to button
exit_button.addActionListener ( new actionListening ( ) );
add ( panel1_container );
panel1_container.setVisible ( false ); //initially disable the first panel
//enable focus on this panel
setFocusable ( true );
}
//action listener
private class actionListening implements ActionListener
{
public void actionPerformed ( ActionEvent aEvent )
{
Object event = aEvent.getSource ( );
if ( event == proceed_button )
{
textField1.setText ( "There are " + textField0.getText ( ) + " questions " );
panel0_container.setVisible ( false );
panel1_container.setVisible ( true );
}
if ( event == cancel_button )
{
System.exit ( 0 );
}
if ( event == exit_button )
{
panel1_container.setVisible ( false );
panel0_container.setVisible ( true );
}
repaint ( ); //repaint this panel
}
}
}
This was done in BLUEJ.