Java Programming tutorial, GUI, Graphical User Interface. this tutorial is teaching. How to add the BoxLayout manager to the panel, to create a questionnaire; or exam display with the use of check boxes and radio buttons, separated with labels.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class UserFrame extends JFrame implements ActionListener{
public static String name = "charles";
public static JMenuItem print;
public static JButton jb11;
public static ImageIcon icon1;
public static ImageIcon icon2;
public static ImageIcon icon3;
public static ImageIcon icon4;
public static ImageIcon icon5;
public static ImageIcon icon6;
public static ImageIcon icon7;
public static JCheckBox tbox;
public static JRadioButton rbut;
public static JCheckBox check1;
public static JCheckBox check2;
public static JCheckBox check3;
public static JRadioButton radio1;
public static JRadioButton radio2;
public static JRadioButton radio3;
public static JLabel lb1;
public static JLabel lb2;
public static JLabel lb3;
public static JLabel lb4;
public static JLabel lb5;
public static JLabel lb6;
public static JLabel lb7;
public static JLabel lb8;
public static JLabel lb9;
public static JLabel lb10;
UserFrame(){
setTitle("UsersFrame");
setSize(600,550);
}
public static void main(String[] args){
UserFrame f = new UserFrame();
JMenuBar bar = new JMenuBar();
bar.setToolTipText("this is the manu bar please use this carefully");
JToolBar tb = new JToolBar();
tb.setToolTipText("This is a too bar");
icon1 = new ImageIcon("src\\Paint-icon.png");
icon2 = new ImageIcon("src\\stop-icon.png");
icon3 = new ImageIcon("src\\shield-icon.png");
icon4 = new ImageIcon("src\\repair-icon.png");
icon5 = new ImageIcon("src\\sunset-icon.png");
icon6 = new ImageIcon("src\\strawberry-icon.png");
icon7 = new ImageIcon("src\\girl-icon.png");
tbox = new JCheckBox("What");
rbut = new JRadioButton("Why");
check1 = new JCheckBox("YouTube");
check2 = new JCheckBox("Scotland");
check3 = new JCheckBox("Me");
radio1 = new JRadioButton("is Scotland Cool ?");
radio2 = new JRadioButton("Scotland Not Cool ?");
radio3 = new JRadioButton("Could Scotland be Cool ?");
ButtonGroup bg = new ButtonGroup();
bg.add(radio1);
bg.add(radio2);
bg.add(radio3);
jb11 = new JButton(icon1);
jb11.addActionListener(f);
jb11.setToolTipText("this button has a tool tip, please read!");
JButton jb12 = new JButton(icon2);
JButton jb13 = new JButton(icon3);
JButton jb14 = new JButton(icon4);
JButton jb15 = new JButton(icon5);
JButton jb16 = new JButton(icon6);
JButton jb17 = new JButton(icon7);
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
print = new JMenuItem("print name");
JMenuItem print2 = new JMenuItem("print nothen");
print.addActionListener(f);
fileMenu.add(print);
fileMenu.add(print2);
bar.add(fileMenu);
bar.add(editMenu);
f.setJMenuBar(bar);
tb.add(jb11);
tb.add(jb12);
tb.add(jb13);
tb.add(jb14);
tb.add(jb15);
tb.add(jb16);
tb.add(jb17);
tb.add(tbox);
tb.add(rbut);
lb1 = new JLabel(" ");
lb2 = new JLabel(" ");
lb3 = new JLabel("Answer the Question Below : Where Who Or What ? ");
lb4 = new JLabel(" ");
lb5 = new JLabel(" ");
lb6 = new JLabel(" ");
lb7 = new JLabel(" ");
lb8 = new JLabel("Answer The Question Below : About Coolability ");
lb9 = new JLabel(" ");
lb10 = new JLabel(" ");
JPanel pan = new JPanel();
JPanel pan2 = new JPanel();
pan2.setLayout(new BoxLayout(pan2,BoxLayout.Y_AXIS));
pan2.add(lb1);
pan2.add(lb2);
pan2.add(lb3);
pan2.add(lb4);
pan2.add(lb5);
pan2.setBackground(Color.WHITE);
pan.setLayout(new BorderLayout());
pan2.add(check1);
pan2.add(check2);
pan2.add(check3);
pan2.add(lb6);
pan2.add(lb7);
pan2.add(lb8);
pan2.add(lb9);
pan2.add(lb10);
pan2.add(radio1);
pan2.add(radio2);
pan2.add(radio3);
pan.add(tb,BorderLayout.NORTH);
pan.add(pan2,BorderLayout.CENTER);
f.add(pan);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object c = (Object) e.getSource();
if(c==jb11){
System.out.println("Charles");
}
if(c==print){
System.out.println("Menu Item Fire Event");
}
}
}
На этой странице сайта вы можете посмотреть видео онлайн Java programming part 73 GUI : BoxLayout Manager длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Redemptie 26 Май 2012, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 2,406 раз и оно понравилось 8 зрителям. Приятного просмотра!