📄 protocoldemo.java
字号:
/*This program provide a main interface containing all the 6 protocol'
*Demo cheng. It quan use DesktopMenu to choose the function,and
*swith among the 6 protocol's Panel. Every Panel of the protocol
*was packed at first,and all the packages can be imported by the
*main interface program.
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import chq.Pro1Panel;
import chq.Pro2Panel;
public class ProtocolDemo extends JFrame implements ActionListener{
private Container c;
private JMenuBar bar;
private JMenu fileMenu,choosePMenu,optionMenu,ackMenu,helpMenu;
private JMenuItem aboutItem,exitItem,runItem,ackItem;
private JTabbedPane tabbedPane;
private JRadioButtonMenuItem protocolItems[];
private ButtonGroup protocolGroup;
private Pro1Panel pro1panel;
private Pro2Panel pro2panel;
private Pro2Panel pro3panel;
private JPanel panel4,panel5,panel6;
private proReceiving threads;
private int sign[]={0,0,0,0,0,0};
// set up GUI
public ProtocolDemo()
{
super( "ProtocolDemo" );
c=getContentPane();
bar = new JMenuBar();
setJMenuBar( bar );
// set up File menu and its menu items
fileMenu = new JMenu( "File" );
fileMenu.setMnemonic('F');
//set up exitItem menu item
exitItem = new JMenuItem( "Exit" );
exitItem.setMnemonic( 'x' );
fileMenu.add( exitItem );
exitItem.addActionListener(this);
bar.add( fileMenu );
// create Choose Protocol menu, its submenus and menu items
choosePMenu = new JMenu( "Choose" );
choosePMenu.setMnemonic('C');
// create Protocol submenu
String protocols[]={"Protocol 1","Protocol 2","Protocol 3",
"Protocol 4","Protocol 5","Protocol 6"};
protocolItems = new JRadioButtonMenuItem[ protocols.length ];
protocolGroup = new ButtonGroup();
// create color radio button menu items
for ( int count = 0; count < protocols.length; count++ ) {
protocolItems[ count ] =
new JRadioButtonMenuItem( protocols[ count ] );
choosePMenu.add( protocolItems[ count ] );
protocolGroup.add( protocolItems[ count ] );
protocolItems[ count ].addActionListener( this );
}
bar.add( choosePMenu );
//Create optionMenu and its Menu item
optionMenu = new JMenu( "Option" );
optionMenu.setMnemonic('O');
ackMenu = new JMenu( "getAck" );
ackMenu.setMnemonic('A');
ackItem = new JMenuItem( "No ack" );
ackItem.setMnemonic( 'N' );
ackItem.addActionListener(this);
ackMenu.add( ackItem );
optionMenu.add(ackMenu);
bar.add(optionMenu);
//Create helpMenu and its Menu item
JMenu helpMenu = new JMenu( "Help" );
helpMenu.setMnemonic('H');
aboutItem = new JMenuItem( "About" );
aboutItem.setMnemonic('A');
helpMenu.add( aboutItem );
aboutItem.addActionListener(this);
bar.add(helpMenu);
//End of menu
// create JTabbedPane
tabbedPane = new JTabbedPane();
// set up prospanel and add them to JTabbedPane
pro1panel=new Pro1Panel();
tabbedPane.addTab( "Protocol 1",null,pro1panel,"A simple perfect channel");
pro2panel=new Pro2Panel();
tabbedPane.addTab( "Protocol 2", null, pro2panel,"Protocol 2");
pro3panel=new Pro2Panel();
tabbedPane.addTab( "Protocol 3", null, pro3panel, "Protocol 3" );
panel4=new JPanel();
tabbedPane.addTab( "Protocol 4", null, panel4, "Protocol 4" );
panel5=new JPanel();
tabbedPane.addTab( "Protocol 5", null, panel5, "Protocol 5" );
panel6=new JPanel();
tabbedPane.addTab( "Protocol 6", null, panel6, "Protocol 6" );
// add JTabbedPane to container
c.add( tabbedPane );
setSize(700,500);
setVisible( true );
} // end constructor
public class proReceiving extends Thread{
int i=0;
public proReceiving(int count)
{
i=count;
}
public void run()
{
if(i==0) pro1panel.receiving();
else if(i==1) pro2panel.waitForEvent();
else if(i==2) pro3panel.waitForEvent();
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==aboutItem){
JOptionPane.showMessageDialog(ProtocolDemo.this,
"Protocol Simulator:Protocol1-Protocol6\n"+
"\tEdited by ChengQuan\n\tCopyRight Reserved\n\tVision 1.1",
"About", JOptionPane.PLAIN_MESSAGE );
}
if(e.getSource()==exitItem){System.exit( 0 );}
for (int count=0;count<protocolItems.length;count++)
{
if(protocolItems[count].isSelected()){
tabbedPane.setSelectedIndex(count);
if(sign[count]==0) {
threads=new proReceiving(count);
threads.start();
sign[count]=1;
}
}//end of if Selected
}//end of for
if(e.getSource()==ackItem){threads.suspend();}
}//End of actionPerformed
public static void main( String args[] )
{
ProtocolDemo proDemo = new ProtocolDemo();
proDemo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class JTabbedPaneDemo
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -