📄 optionspanel.java
字号:
package za.co.halo.SecureCommunications.gui.server;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.*;
import za.co.halo.SecureCommunications.ServerAdmin;
import za.co.halo.SecureCommunications.gui.server.ServerPanel2.CancelAction;
import za.co.halo.SecureCommunications.gui.server.ServerPanel2.SaveAction;
import za.co.halo.SecureCommunications.gui.server.ServerPanel2.ServerCellRenderer;
import za.co.halo.SecureCommunications.gui.server.ServerPanel2.StartAction;
import za.co.halo.SecureCommunications.gui.server.ServerPanel2.StopAction;
/**
* This code was generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* *************************************
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED
* for this machine, so Jigloo or this code cannot be used legally
* for any corporate or commercial purpose.
* *************************************
*/
public class OptionsPanel extends javax.swing.JPanel
{
private JPanel buttonPanel;
private JPanel leftPanel;
private JPanel rightPanel;
private JPanel editServerPanel;
private JLabel serverNameLabel;
private JLabel passwordLabel;
private JPanel editButtonPanel;
private JSplitPane splitPane;
private JList serverList;
private JScrollPane scroll;
private JButton addButton;
private JButton removeButton;
private JButton saveButton;
private JButton cancelButton;
private JTextField serverNameText;
private JPasswordField passwordText;
private Vector<String> servers;
private Vector<String> passwords;
private String password;
private String serverName;
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.getContentPane().add(new OptionsPanel());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public OptionsPanel()
{
super();
initGUI();
}
private void initGUI()
{
try
{
this.setLayout(null);
//this.setBackground(Color.green);
JPanel imagePanel = new ImagePanel("images/options.png","accounts");
imagePanel.setLayout(null);
imagePanel.setBounds(0,0,625,83);
this.add(imagePanel);
leftPanel = new JPanel();
leftPanel.setMinimumSize(new Dimension(300,350));
//leftPanel.setBackground(Color.BLACK);
leftPanel.setLayout(null);
rightPanel = new JPanel();
//rightPanel.setBackground(Color.BLACK);
rightPanel.setLayout(null);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
//splitPane.setBackground(Color.GREEN);
//splitPane.setLayout(null);
splitPane.setBounds(7,100,605,350);
splitPane.setDividerLocation(300);
this.add(splitPane);
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setBounds(7,465,327,30);
this.add(buttonPanel);
addButton = new JButton(new AddAction());
addButton.setText("Add Server");
addButton.setBounds(3,3,110,25);
buttonPanel.add(addButton);
removeButton = new JButton(new RemoveAction());
removeButton.setText("Remove Server");
removeButton.setBounds(120,3,150,25);
removeButton.setEnabled(false);
buttonPanel.add(removeButton);
servers = new Vector<String>();
passwords = new Vector<String>();
readServer();
//servers.addElement(readServer());
serverList = new JList(servers);
serverList.setBackground(new Color(21,118,146));
serverList.setLayout(null);
serverList.setBounds(0,0,290,350);
serverList.setCellRenderer(new ServerCellRenderer());
MouseListener mouseListener = new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
}
if (e.getClickCount() == 1)
{
removeButton.setEnabled(true);
editServerPanel = new JPanel();
//editServerPanel.setBackground(Color.GREEN);
editServerPanel.setLayout(null);
//editServerPanel.setBounds(0, 0, 350, 315);
serverNameLabel = new JLabel("Server Name");
serverNameLabel.setBounds(12,12,80,20);
editServerPanel.add(serverNameLabel);
serverNameText = new JTextField();
//Object element = serverNameList.getSelectedValue();
int i = serverList.getSelectedIndex();
serverNameText.setText(servers.elementAt(i));
serverNameText.setBounds(100,9,130,30);
editServerPanel.add(serverNameText);
//portText.setEditable(true);
passwordLabel = new JLabel("Password");
passwordLabel.setBounds(12,50,80,20);
editServerPanel.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setText(passwords.elementAt(i));
passwordText.setBounds(100,46,130,30);
editServerPanel.add(passwordText);
editButtonPanel = new JPanel();
editButtonPanel.setLayout(null);
//editButtonPanel.setBackground(Color.GREEN);
editButtonPanel.setBounds(7,250,250,50);
editServerPanel.add(editButtonPanel);
saveButton = new JButton(new SaveAction());
saveButton.setText("Save & Close");
saveButton.setBounds(0,0,120,25);
editButtonPanel.add(saveButton);
cancelButton = new JButton(new CancelAction());
cancelButton.setText("Cancel");
cancelButton.setBounds(125,0,90,25);
editButtonPanel.add(cancelButton);
/*editPortPanel.add(portLabel);
editPortPanel.add(portText);
portButtonPanel = new JPanel();
portButtonPanel.setLayout(null);
portButtonPanel.setBounds(5,230,300,50);
editPortPanel.add(portButtonPanel);
saveButton = new JButton(new SaveAction());
saveButton.setText("Save & Close");
saveButton.setBounds(0,2,120,25);
portButtonPanel.add(saveButton);
cancelButton = new JButton(new CancelAction());
cancelButton.setText("Canel");
cancelButton.setBounds(125,2,100,25);
portButtonPanel.add(cancelButton);*/
splitPane.setRightComponent(editServerPanel);
}
}
};
serverList.addMouseListener(mouseListener);
scroll = new JScrollPane(serverList);
scroll.setBounds(0,0,300,350);
leftPanel.add(scroll);
/*t = new JTextArea();
t.setLayout(null);
t.setBounds(2,2,250,300);
t.setText(readServer());
leftPanel.add(t);*/
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void readServer()
{
File serverFile = new File("Cluster/servers.txt");
if (!serverFile.exists())
{
try
{
ServerAdmin.error.warning("The file servers.txt does not exist");
serverFile.createNewFile();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
BufferedReader reader = null;
StringTokenizer tokenizer;
String s = "";
try
{
reader = new BufferedReader(new FileReader(serverFile));
while (reader.ready())
{
s = reader.readLine();
tokenizer = new StringTokenizer(s,"\t");
serverName = tokenizer.nextToken();
password = tokenizer.nextToken();
servers.addElement(serverName);
passwords.addElement(password);
//logTextArea.setForeground(Color.white);
//s += "\n";
//System.out.println(s);
//logTextArea.setText(s);
}
reader.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
private void writeServer()
{
File file = new File("Cluster/servers.txt");
try
{
FileWriter fw = new FileWriter(file);
int i = 0;
while (i < servers.size())
{
fw.write(servers.elementAt(i)+'\t'+passwords.elementAt(i)+'\n');
fw.flush();
i++;
}
fw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
class ServerCellRenderer implements ListCellRenderer
{
public Component getListCellRendererComponent(JList list, Object object, int index, boolean isSelected, boolean arg4)
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(new Color(21,118,146));
if (object instanceof String)
{
//singleNamePanel = new JPanel();
//singleNamePanel.setLayout(new BorderLayout());
panel.add(new ViewSingleServer2NamesPanel(servers.elementAt(index), isSelected));
}
else
panel.add(new JLabel("Could not load information"));
return panel;
}
}
class AddAction extends AbstractAction
{
AddAction() {
super("add server");
}
/**
* actionPeformed method
* Use to:
* Handle cancel actions
* @param ActionEvent: the type of action
* @return void
*/
public void actionPerformed(ActionEvent arg0)
{
editServerPanel = new JPanel();
editServerPanel.setLayout(null);
serverNameLabel = new JLabel("Server Name");
serverNameLabel.setBounds(12,12,80,20);
editServerPanel.add(serverNameLabel);
serverNameText = new JTextField();
serverNameText.setBounds(100,9,130,30);
editServerPanel.add(serverNameText);
passwordLabel = new JLabel("Password");
passwordLabel.setBounds(12,50,80,20);
editServerPanel.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setBounds(100,46,130,30);
editServerPanel.add(passwordText);
editButtonPanel = new JPanel();
editButtonPanel.setLayout(null);
editButtonPanel.setBounds(7,250,250,50);
editServerPanel.add(editButtonPanel);
saveButton = new JButton(new NewSaveAction());
saveButton.setText("Save & Close");
saveButton.setBounds(0,0,120,25);
editButtonPanel.add(saveButton);
cancelButton = new JButton(new CancelAction());
cancelButton.setText("Cancel");
cancelButton.setBounds(125,0,90,25);
editButtonPanel.add(cancelButton);
splitPane.setRightComponent(editServerPanel);
}
}
class RemoveAction extends AbstractAction
{
RemoveAction()
{
super("remove server");
}
/**
* actionPeformed method
* Use to:
* Handle cancel actions
* @param ActionEvent: the type of action
* @return void
*/
public void actionPerformed(ActionEvent arg0)
{
int i = serverList.getSelectedIndex();
servers.removeElementAt(i);
passwords.removeElementAt(i);
serverList.repaint();
splitPane.setRightComponent(rightPanel);
writeServer();
removeButton.setEnabled(false);
}
}
class CancelAction extends AbstractAction
{
CancelAction()
{
super("cancel");
}
/**
* actionPeformed method
* Use to:
* Handle cancel actions
* @param ActionEvent: the type of action
* @return void
*/
public void actionPerformed(ActionEvent arg0)
{
splitPane.setRightComponent(rightPanel);
}
}
class SaveAction extends AbstractAction
{
SaveAction()
{
super("save");
}
/**
* actionPeformed method
* Use to:
* Handle save actions
* @param ActionEvent: the type of action
* @return void
*/
public void actionPerformed(ActionEvent arg0)
{
int i = serverList.getSelectedIndex();
servers.setElementAt(serverNameText.getText(),i);
//char[] p = passwordText.getPassword();
//String p2 = p.toString();
//passwords.setElementAt(p2,i);
passwords.setElementAt(new String(passwordText.getPassword()),i);
serverList.repaint();
writeServer();
splitPane.setRightComponent(rightPanel);
}
}
class NewSaveAction extends AbstractAction
{
NewSaveAction()
{
super("save");
}
/**
* actionPeformed method
* Use to:
* Handle save actions
* @param ActionEvent: the type of action
* @return void
*/
public void actionPerformed(ActionEvent arg0)
{
servers.addElement(serverNameText.getText());
//char[] p = passwordText.getPassword();
//String p2 = p.toString();
//passwords.addElement(p2);
passwords.addElement(new String(passwordText.getPassword()));
serverList.setListData(servers);
serverList.repaint();
writeServer();
splitPane.setRightComponent(rightPanel);
serverList.repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -