📄 mainframe.java
字号:
package stock;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class MainFrame
extends JFrame
{
// ChessRoom Room;
Vector QueueWithMan = new Vector(10);
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout();
JButton jButton_con = new JButton();
JButton jButton_save = new JButton();
JTabbedPane jtpane;
LogPanel log;
// LogPanel users;
LogPanel recMsg, ackMsg; //接受消息、发送消息记录
BorderLayout borderLayout2 = new BorderLayout();
JTextField TextField_port = new JTextField();
JLabel jLabel2 = new JLabel();
JTextField TextField_ip = new JTextField();
JLabel jLabel3 = new JLabel();
FileOperate fo = new FileOperate("./url.txt");
JButton jButton1 = new JButton();
TitledBorder titledBorder1;
JTextField TextField_name = new JTextField();
JTextField TextField_corp = new JTextField();
JLabel jLabel_name = new JLabel();
JLabel jLabel4 = new JLabel();
//Construct the frame
public MainFrame()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit()
throws Exception
{
jtpane = new JTabbedPane();
log = new LogPanel();
recMsg = new LogPanel();
ackMsg = new LogPanel();
//users = new LogPanel();
contentPane = (JPanel)this.getContentPane();
titledBorder1 = new TitledBorder("");
contentPane.setLayout(borderLayout1);
this.setResizable(true);
this.setSize(new Dimension(800, 600));
this.setTitle("股票服务");
statusBar.setText(" ");
jMenuFile.setText("操作");
jMenuFile.setFont(new java.awt.Font("宋体", 0, 12));
jMenuFileExit.setText("退出");
jMenuFileExit.addActionListener(new MainFrame_jMenuFileExit_ActionAdapter(this));
jMenuHelp.setText("帮助");
jMenuHelp.setFont(new java.awt.Font("宋体", 0, 12));
jMenuHelpAbout.setText("关于");
jMenuHelpAbout.addActionListener(new MainFrame_jMenuHelpAbout_ActionAdapter(this));
jPanel1.setLayout(flowLayout1);
jPanel2.setLayout(flowLayout2);
jButton_con.setFont(new java.awt.Font("宋体", 0, 12));
jButton_con.setText("注册服务模块");
jButton_con.addActionListener(new MainFrame_jButton_con_actionAdapter(this));
jButton_save.setFont(new java.awt.Font("宋体", 0, 12));
jButton_save.setToolTipText("");
jButton_save.setActionCommand("保存服务器地址");
jButton_save.setText("保存服务器地址");
jButton_save.addActionListener(new MainFrame_jButton_save_actionAdapter(this));
jPanel3.setLayout(borderLayout2);
TextField_port.setPreferredSize(new Dimension(50, 21));
TextField_port.setToolTipText("");
TextField_port.setText("");
jLabel2.setFont(new java.awt.Font("宋体", 0, 12));
jLabel2.setText("端口");
TextField_ip.setFont(new java.awt.Font("宋体", 0, 12));
TextField_ip.setMaximumSize(new Dimension(2147483647, 2147483647));
TextField_ip.setPreferredSize(new Dimension(110, 20));
jLabel3.setFont(new java.awt.Font("宋体", 0, 12));
jLabel3.setText("远程服务器地址");
jButton1.setFont(new java.awt.Font("宋体", 0, 12));
jButton1.setBorder(titledBorder1);
jButton1.setPreferredSize(new Dimension(100, 40));
jButton1.setText("启动本服务模块");
jButton1.addActionListener(new MainFrame_jButton1_actionAdapter(this));
TextField_corp.setFont(new java.awt.Font("宋体", 0, 12));
TextField_corp.setPreferredSize(new Dimension(70, 20));
TextField_corp.setText("");
jLabel_name.setFont(new java.awt.Font("宋体", 0, 12));
jLabel_name.setText("模块名");
jLabel4.setFont(new java.awt.Font("宋体", 0, 12));
jLabel4.setText("服务商");
TextField_name.setFont(new java.awt.Font("宋体", 0, 12));
TextField_name.setPreferredSize(new Dimension(70, 20));
TextField_name.setText("");
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(statusBar, BorderLayout.WEST);
contentPane.add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jButton1, null);
contentPane.add(jPanel2, BorderLayout.SOUTH);
jPanel2.add(jLabel3, null);
jPanel2.add(TextField_ip, null);
jPanel2.add(jLabel_name, null);
jPanel2.add(TextField_name, null);
jPanel2.add(jLabel4, null);
jPanel2.add(TextField_corp, null);
jPanel2.add(jLabel2, null);
jPanel2.add(TextField_port, null);
jPanel2.add(jButton_con, null);
jPanel2.add(jButton_save, null);
contentPane.add(jPanel3, BorderLayout.CENTER);
jPanel3.add(jtpane, BorderLayout.CENTER);
//添加标签卡
jtpane.setFont(new java.awt.Font("宋体", 0, 12));
jtpane.addTab("服务计算监控", new JScrollPane(log));
//jtpane.addTab("注册用户列表", new JScrollPane(users));
jtpane.addTab("模块接收消息", new JScrollPane(recMsg));
jtpane.addTab("模块发送消息", new JScrollPane(ackMsg));
//从url文件读取服务器信息(如果有的话)
MessageClass mc = new MessageClass(fo.readFileALine() + "::", ':');
if (mc.first.length() > 0)
{
TextField_ip.setText(mc.first);
//TextField_port.setText(mc.second);
TextField_ip.setEditable(false);
//TextField_port.setEditable(false);
jButton_save.setText("修改服务器地址");
}
if (new File("RegisteSuccess.txt").exists())
{
FileOperate rf = new FileOperate("RegisteSuccess.txt");
MessageClass mc1 = new MessageClass(rf.readFileALine(), ':');
this.TextField_name.setText(mc1.first);
this.TextField_corp.setText(mc1.second);
this.TextField_port.setText(mc1.third);
this.jButton_con.setEnabled(false);
this.TextField_corp.setEditable(false);
this.TextField_name.setEditable(false);
this.TextField_port.setEditable(false);
}
this.log.AddLine("模块初始化成功...");
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e)
{
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerformed(ActionEvent e)
{
MainFrame_AboutBox dlg = new MainFrame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
jMenuFileExit_actionPerformed(null);
}
}
//保存 | 修改服务器地址按钮事件
void jButton_save_actionPerformed(ActionEvent e)
{
new File("url.txt").delete();
if (jButton_save.getText().equals("保存服务器地址"))
{
this.TextField_ip.setEditable(false);
//this.TextField_port.setEditable(false);
this.jButton_save.setText("修改服务器地址");
fo.write2File(TextField_ip.getText());
System.out.print(fo.readFileALine());
}
else if (jButton_save.getText().equals("修改服务器地址"))
{
this.TextField_ip.setEditable(true);
//this.TextField_port.setEditable(true);
this.jButton_save.setText("保存服务器地址");
}
}
//连接按钮事件
void jButton_con_actionPerformed(ActionEvent e)
{
int port = new Integer(this.TextField_port.getText()).intValue();
//System.out.print(port);
SocketConn sc = new SocketConn(this.TextField_ip.getText(), 6001);
if (sc.error)
{ //如果连接失败
this.jButton_con.setEnabled(true);
// JOptionPane.showConfirmDialog(this, "连接失败", "WARNING",
// JOptionPane.OK_OPTION); //对话框提示
this.log.AddLine("模块连接服务器" + TextField_ip.getText() + ":" +
TextField_port.getText() + "失败");
}
else
{ //如果成功
sc.send("Sregiste." + this.TextField_name.getText() + "." +
this.TextField_corp.getText() + "." + this.TextField_port.getText()); //Sregiste.五子棋.微软.8888
String ack = sc.getAck();
if (ack.equals("r_ok"))
{ //注册成功
this.log.AddLine("模块:" + this.TextField_name.getText() + "来自" +
this.TextField_corp.getText() + "注册成功,本地端口为:" +
this.TextField_port.getText());
new FileOperate("RegisteSuccess.txt").write2File(this.TextField_name.
getText() + ":" + this.TextField_corp.getText() + ":" +
this.TextField_port.getText());
this.jButton_con.setEnabled(false);
this.TextField_corp.setEditable(false);
this.TextField_name.setEditable(false);
this.TextField_port.setEditable(false);
}
else if (ack.equals("r_repeat"))
{ //重复
this.log.AddLine(this.TextField_name.getText() + "已存在,请另起一个名字");
}
//this.log.AddLine("模块成功启动于" + sc.s.getInetAddress().getLocalHost() + ":" + sc.s.getLocalPort());
}
// this.log.AddLine("模块连接服务器" + TextField_ip.getText() + ":" +
// TextField_port.getText() + "成功");
//new Thread(new WaitForClient(sc,this)).start();//启动监听线程
}
//启动按钮时间
void jButton1_actionPerformed(ActionEvent e)
{
// try {
int port = new Integer(this.TextField_port.getText()).intValue();
// ServerSocket ss = new ServerSocket(port); //建立服务器监听线程
// this.jButton1.setEnabled(false);
this.jButton1.setPreferredSize(new Dimension(300, 50));
// this.log.AddLine("模块成功启动于【" + ss.getInetAddress().getLocalHost() + ":" + ss.getLocalPort()+"】");
this.jButton1.setText("模块成功启动,正在监听" + this.TextField_port.getText() +
"端口……");
this.jButton1.setEnabled(false);
new Thread(new StockInfo(this)).start();
//}
// catch (IOException ex) {
// System.err.println("无法监听端口!!");
// System.exit(1);
// }
}
}
class MainFrame_jMenuFileExit_ActionAdapter
implements ActionListener
{
MainFrame adaptee;
MainFrame_jMenuFileExit_ActionAdapter(MainFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jMenuFileExit_actionPerformed(e);
}
}
class MainFrame_jMenuHelpAbout_ActionAdapter
implements ActionListener
{
MainFrame adaptee;
MainFrame_jMenuHelpAbout_ActionAdapter(MainFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jMenuHelpAbout_actionPerformed(e);
}
}
class MainFrame_jButton_save_actionAdapter
implements java.awt.event.ActionListener
{
MainFrame adaptee;
MainFrame_jButton_save_actionAdapter(MainFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton_save_actionPerformed(e);
}
}
class MainFrame_jButton_con_actionAdapter
implements java.awt.event.ActionListener
{
MainFrame adaptee;
MainFrame_jButton_con_actionAdapter(MainFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton_con_actionPerformed(e);
}
}
class MainFrame_jButton1_actionAdapter
implements java.awt.event.ActionListener
{
MainFrame adaptee;
MainFrame_jButton1_actionAdapter(MainFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -