В общем у меня есть уже код этой программы..Спасибо) Но мне туда нужно добавить строку меню (типа Файл, правка и т.п.) , а я не знаю куда....
Текст самой программы:
1.
Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
| package javaapplication10;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;import javax.swing.border.Border;
import javax.swing.event.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerException;
import org.w3c.dom.*;
import java.awt.event.ItemListener;
public class Form extends JFrame
{ private JList List;
private JButton add,del,search,search1;
private static DefaultListModel listPark;
private static ListData left;
private JTextField fio,numb,day,minute,year,plac;
private JTextField Number, Number1;
private JButton poisk,poisk1;
JTextArea ta;
JPanel panel5,panel4;
public Form()
{
super("Учет книг в библиотеке");
createGui();
}
public void createGui()
{
ta=new JTextArea();
ta.setPreferredSize(new Dimension(100,100));
ta.setEditable(false);
left = new ListData();
listPark = new DefaultListModel();
List = new JList(listPark);
List.setPreferredSize(new Dimension(200,100));
List.setAlignmentX(LEFT_ALIGNMENT);
List.setAutoscrolls(true);
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(150,150));
panel1.setBorder(BorderFactory.createTitledBorder("Книги: "));
panel1.setLayout(new BoxLayout(panel1,BoxLayout.PAGE_AXIS));
panel1.setBackground(Color.WHITE);
panel1.add(List);
JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(175,135));
panel2.setBorder(BorderFactory.createTitledBorder("Поиск: "));
panel2.setLayout(new BoxLayout(panel2,BoxLayout.PAGE_AXIS));
JPanel panel3 = new JPanel();
panel3.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
panel3.setPreferredSize(new Dimension(150,150));
panel3.setBorder(BorderFactory.createTitledBorder("Информация: "));
panel3.setLayout(new BoxLayout(panel3,BoxLayout.PAGE_AXIS));
panel3.add(ta);
panel4 = new JPanel();
panel4.setBorder(BorderFactory.createTitledBorder("Меню: "));
panel4.setLayout(new BoxLayout(panel4,BoxLayout.PAGE_AXIS));
panel4.setPreferredSize(new Dimension(180,250));
JButton add = new JButton(" Добавить книгу ");
JLabel label=new JLabel("ФИО");
fio = new JTextField("");
JLabel label1=new JLabel("Год издания");
numb = new JTextField("");
JLabel label2=new JLabel("Кол-во экземпляров:");
year= new JTextField("");
year.setPreferredSize(new Dimension(30,20));
JLabel label5=new JLabel("Название книги");
plac = new JTextField("");
panel4.setLayout(new BoxLayout(panel4, BoxLayout.PAGE_AXIS));
panel4.add(label);
panel4.add(fio);
panel4.add(label1);
panel4.add(numb);
panel4.add(label2);
panel5 = new JPanel();
panel5.setLayout(new FlowLayout(FlowLayout.LEFT));
panel4.add(year);
panel4.add(label5);
panel4.add(plac);
add.addActionListener(new Add());
panel4.add(add);
del = new JButton(" Удалить книгу ");
del.addActionListener(new Del());
del.setAlignmentX(LEFT_ALIGNMENT);
panel4.add(del);
Number = new JTextField("");
poisk = new JButton("Поиск по году издания");
panel2.add(Number);
panel2.add(poisk);
poisk.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ showInfo(ListData.Search(left,Integer.parseInt (Number.getText())));
Number.setText("");
} });
Number1 = new JTextField("");
poisk1 = new JButton(" Поиск по фамилии автора");
panel2.add(Number1);
panel2.add(poisk1);
poisk1.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ showInfo1(ListData.Search1(left, Number1.getText()));
Number1.setText("");
} });
JPanel panel6 = new JPanel();
panel6.setPreferredSize(new Dimension(175,135));
panel6.setLayout(new BoxLayout(panel6,BoxLayout.PAGE_AXIS));
getContentPane().add(panel1,BorderLayout.WEST);
getContentPane().add(panel2,BorderLayout.EAST);
getContentPane().add(panel3,BorderLayout.SOUTH);
getContentPane().add(panel4,BorderLayout.CENTER);
List.addMouseListener(new ShowInfo());
}
public void showInfo(DefaultListModel model)
{
JDialog d = new JDialog();
JList info = new JList(model);
d.getContentPane().add(info);
d.pack();
d.setVisible(true);
d.setResizable(false);
}
public void showInfo1(DefaultListModel model)
{
JDialog d = new JDialog();
JList info = new JList(model);
d.getContentPane().add(info);
d.pack();
d.setVisible(true);
d.setResizable(false);
}
class ShowInfo implements MouseListener {
@Override
public void mouseClicked(MouseEvent arg0)
{
if(arg0.getClickCount()==1){ta.setText("");}
if(arg0.getClickCount()==2)
{
ta.setText("");
int index1;
index1=List.getSelectedIndex();
if(index1!=-1)
{
Constr a = new Constr();
a= (Constr)left.elementAt(index1);
ta.setText("ФИО: "+a.Name+"\n"+"Год издания: "+Integer.toString(a.Number)+"\n"
+"Название книги: "+a.Place+"\n"+"Количество экз: "+Integer.toString(a.year));
}
}
}
@Override
public void mouseEntered(MouseEvent arg0){}
@Override
public void mouseExited(MouseEvent arg0){}
@Override
public void mousePressed(MouseEvent arg0){}
@Override
public void mouseReleased(MouseEvent arg0){}
}
class Add implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
String Nam,place;
int Namb,y;
Nam = fio.getText();
Namb = Integer.parseInt(numb.getText());
y=Integer.parseInt(year.getText());
place = plac.getText();
Constr dat = new Constr(Nam,Namb,place,y);
listPark.addElement(dat.Name+" "+dat.Number+"");
left.addElement(dat);
fio.setText("");
numb.setText("");
year.setText("");
plac.setText("");
}
}
class Del implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
int index1;
index1=List.getSelectedIndex();
if (index1!=-1)
{
listPark.remove(index1);
left.remove(index1);
}
ta.setText("");
}
}
public static void main(String[] args)
{
Form f = new Form();
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setResizable(false);
f.addWindowListener(new WindowListener()
{
@Override
public void windowOpened(WindowEvent arg0)
{
try
{
XML xml = new XML();
Document doc = xml.getDocument();
xml.readXML(doc, left);
for(int i=0;i< left.size();i++)
{
listPark.addElement(((Constr)left.elementAt(i)).Name+" "+((Constr)left.elementAt(i)).Number);
}
}
catch(Exception ex){}
}
@Override
public void windowIconified(WindowEvent arg0){}
@Override
public void windowDeiconified(WindowEvent arg0){}
@Override
public void windowDeactivated(WindowEvent arg0) {}
@Override
public void windowClosing(WindowEvent arg0)
{
XML xml = new XML();
try {xml.writeXML(left);}
catch (TransformerException e) {}
catch (IOException e) {}
}
@Override
public void windowClosed(WindowEvent arg0) {}
@Override
public void windowActivated(WindowEvent arg0) {}});
f.pack();
f.setVisible(true);
}
} |
|
2.
Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
| package javaapplication10;
import java.util.*;
import com.sun.org.apache.xpath.internal.operations.Bool;
import javax.swing.DefaultListModel;
public class ListData extends Vector
{
static DefaultListModel rez;
public ListData()
{
}
public static DefaultListModel Search(Vector vektor,int elem)
{
rez= new DefaultListModel();
for(int i=0;i<vektor.size();i++)
{
if(elem==((Constr)vektor.elementAt(i)).Number)
{
Constr a = new Constr();
a=((Constr)vektor.elementAt(i));
rez.addElement("ФИО: " + a.Name +" "+"Кол-во экземпляров: "+a.year);
}
}
return rez;
}
public static DefaultListModel Search1(Vector vektor1,String elem)
{
DefaultListModel rez = new DefaultListModel();
for(int i=0;i<vektor1.size();i++)
{
String buf;
buf = (((Constr)vektor1.elementAt(i)).Name);
for(int j=0;j<buf.length();j++)
{
if (buf.equals(elem))
{
Constr a = new Constr();
a=((Constr)vektor1.elementAt(i));
rez.addElement("ФИО: " + a.Name +" Год издания: "+a.Number);
break;
}
}
}
return rez;
}
} |
|
3.
Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
| package javaapplication10;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import javax.xml.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
/**
*
* @author Maria
*/
public class XML {
private DocumentBuilder builder;
public Document getDocument() throws Exception
{
try
{
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setValidating(false);
DocumentBuilder builder = f.newDocumentBuilder();
return builder.parse(new File("read.xml"));
}
catch(Exception exception)
{
String message = "XML parsing error!";
throw new Exception(message);
}
}
public void readXML(Document doc, Vector vektor)
{
doc.getDocumentElement().normalize();
NodeList children = doc.getElementsByTagName("employee");
for(int i=0;i<children.getLength();i++)
{
Node childNode = children.item(i);
if(childNode.getNodeType() == Node.ELEMENT_NODE)
{
Element elem = (Element) childNode;
NodeList fioBufList = elem.getElementsByTagName("fio");
Element fioBufElmn = (Element) fioBufList.item(0);
NodeList fioNm = fioBufElmn.getChildNodes();
String fio = ((Node)fioNm.item(0)).getNodeValue();
NodeList NambBufList = elem.getElementsByTagName("Namb");
Element NambBufElmn = (Element) NambBufList.item(0);
NodeList NambNm = NambBufElmn.getChildNodes();
String Namb= ((Node)NambNm.item(0)).getNodeValue();
NodeList yBufList = elem.getElementsByTagName("y");
Element yBufElmn = (Element) yBufList.item(0);
NodeList yNm = yBufElmn.getChildNodes();
String y = ((Node)yNm.item(0)).getNodeValue();
NodeList placeBufList = elem.getElementsByTagName("place");
Element placeBufElmn = (Element) placeBufList.item(0);
NodeList placeNm = placeBufElmn.getChildNodes();
String place = ((Node)placeNm.item(0)).getNodeValue();
Constr el = new Constr(fio,Integer.parseInt(Namb),place,Integer.parseInt(y));
vektor.addElement(el);
}
}
}
public void writeXML(Vector vektor) throws TransformerException,IOException
{
String fio;
String place;
int Namb;
int d;
int m;
int y;
Element fe,ge,re,te,me,ye;
Element root1;
Node f,g,r,ty,my,yy;
Document xmldoc = new DocumentImpl();
Element root = xmldoc.createElement("applicatioin");
for(int i=0;i<vektor.size();i++)
{
fio = ((Constr)vektor.elementAt(i)).Name;
Namb= ((Constr)vektor.elementAt(i)).Number;
y= ((Constr)vektor.elementAt(i)).year;
place = ((Constr)vektor.elementAt(i)).Place;
root1 = xmldoc.createElement("employee");
fe=xmldoc.createElementNS(null,"fio");
f = xmldoc.createTextNode(fio);
fe.appendChild(f);
re = xmldoc.createElementNS(null,"Namb");
r = xmldoc.createTextNode(Integer.toString(Namb));
re.appendChild(r);
ye = xmldoc.createElementNS(null,"y");
yy = xmldoc.createTextNode(Integer.toString(y));
ye.appendChild(yy);
ge = xmldoc.createElementNS(null,"place");
g = xmldoc.createTextNode((place));
ge.appendChild(g);
root1.appendChild(fe);
root1.appendChild(re);
root1.appendChild(ye);
root1.appendChild(ge);
root.appendChild(root1);
}
xmldoc.appendChild(root);
FileOutputStream fos = new FileOutputStream("read.xml");
OutputFormat of = new OutputFormat("XML","Utf-8",true);
of.setIndent(1);
of.setIndenting(true);
XMLSerializer serializer = new XMLSerializer(fos,of);
serializer.asDOMSerializer();
serializer.serialize(xmldoc.getDocumentElement());
fos.close();
}
} |
|
4.
Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| package javaapplication10;
public class Constr
{
String Name,Place;
int Number,year;
public Constr()
{
}
public Constr(String Nam,int Namb,String plac,int y)
{
Name=Nam;
Number=Namb;
Place=plac;
year=y;
}
} |
|
там было 4 файла... Если Вы в этом разбираетесь, то не могли бы объяснить мне тогда какие функции тут за что отвечают..))) Буду Вам очень благодарна))
|