📄 sampledesktop13.java
字号:
JComponent editor = spinner.getEditor();
if (editor instanceof JSpinner.DefaultEditor) {
return ((JSpinner.DefaultEditor)editor).getTextField();
} else {
System.err.println("Unexpected editor type: "
+ spinner.getEditor().getClass()
+ " isn't a descendant of DefaultEditor");
return null;
}
}
//downloded code ends heres
protected JComponent createEntryFields(String[] labelStrings) {
JPanel panel = new JPanel(new SpringLayout());
JLabel[] labels = new JLabel[labelStrings.length];
JComponent[] fields = new JComponent[labelStrings.length];
int fieldNum = 0;
nameField = new JTextField[labelStrings.length];
for(int p=0;p<labelStrings.length;p++){
nameField[p] = new JTextField(20);
nameField[p].setColumns(20);
fields[fieldNum++] = nameField[p];
nameField[p].setMaximumSize(nameField[p].getPreferredSize());}
//Associate label/field pairs, add everything,
//and lay it out.
for (int i = 0; i < labelStrings.length; i++) {
labels[i] = new JLabel(labelStrings[i],
JLabel.TRAILING);
labels[i].setLabelFor(fields[i]);
labels[i] .setMaximumSize(labels[i] .getPreferredSize());
panel.add(labels[i]);
panel.add(fields[i]);
//Add listeners to each field.
JTextField tf = null;
tf = (JTextField)fields[i];
tf.addActionListener(this);
//nameField1[i]=nameField[i];
}
makeCompactGrid(panel,
labelStrings.length, 2,
6, 6, //init x,y
7, 7);//xpad, ypad
return panel;
}
protected void fileopen(String s){
FileInputStream fin = null;
ht.clear();
try{
fin = new FileInputStream(s);
}catch(FileNotFoundException e){
//ignoring missing file
}
try {
if(fin != null){
ht.load(fin);
fin.close();
}
}catch(IOException e) {
System.out.println("Error reading file");
}
}
protected void fileopen1(String s){
FileInputStream fin = null;
try{
fin = new FileInputStream(s);
}catch(FileNotFoundException e){
//ignoring missing file
}
try {
if(fin != null){
ht1.load(fin);
fin.close();
}
}catch(IOException e) {
System.out.println("Error reading file");
}
}
protected void writetofile(String s){
try{
File f=new File(s);
f.delete();
FileOutputStream fout = new FileOutputStream(s,true) ;
ht.store(fout,"Prosun's creation");
fout.close();
}catch(IOException e){}
}
private void save(){
fileopen("srecord.txt");
name=nameField[0].getText();
fname=nameField[1].getText();
number=nameField[2].getText();
add=nameField[3].getText();
qua=nameField[4].getText();
email=nameField[5].getText();
tele=nameField[6].getText();
insertroll(number,"roll.txt");
sums=name+"#"+fname+"#"+add+"#"+qua+"#"+email+"#"+tele;
ht.put(number,sums);
writetofile("srecord.txt");
}
//save upto here
private void Update(){
fileopen("srecord.txt");
name=nameField[0].getText();
fname=nameField[1].getText();
//number=nameField2[2].getText();
number=newSelection;
add=nameField[2].getText();
qua=nameField[3].getText();
email=nameField[4].getText();
tele=nameField[5].getText();
//insertroll(number);
sums=name+"#"+fname+"#"+add+"#"+qua+"#"+email+"#"+tele;
ht.put(number,sums);
writetofile("srecord.txt");
}
public void insertroll(String number,String fn)
{
try
{
DataOutputStream writeFile = new DataOutputStream(new FileOutputStream(fn,true));
String rec = number+"#";
writeFile.writeBytes(rec);
writeFile.close();
}
catch(Exception e)
{
//e.printStackTrace();
}
}
//trying to delete
private void Delete(){
fileopen("srecord.txt");
File f=new File("d:\\prosun\\SWING\\srecord.txt");
f.delete();
try{
FileOutputStream fout = new FileOutputStream("srecord.txt",true);
ht.remove(newSelection);
ht.store(fout,"Prosun's creation");
fout.close();
}catch(IOException e){}
deletefromsuppotedfile("roll.txt",newSelection);
}
//detete upto here
//file copy
private void filecopy(String a,String b){
int i;
FileInputStream fin;
FileOutputStream fout;
try{
try{
fin =new FileInputStream(a);
}catch(FileNotFoundException e){
System.out.println("FILE NOT FOUND");
return;}
try{
File f1=new File("d:\\prosun\\SWING\\roll.txt");
f1.delete();
fout =new FileOutputStream(b);
}catch(FileNotFoundException e){
System.out.println("OUTPUT FILE NOT FOUND");
return;}
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("usage");
return;}
// read characters until EOF
try{
do{
i=fin.read();
if(i !=-1)
fout.write(i);
//System.out.print((char) i);
}
while(i!=-1);}
catch(IOException e){
System.out.println("file Error");
}
try{
fin.close();
fout.close();
File f2=new File("d:\\prosun\\SWING\\temp1.txt");
f2.delete();
}catch(IOException e){}
}
//copy file upto here
protected JComponent createButtons(String s,String t) {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
JButton button = new JButton(s);
button.addActionListener(this);
button.setActionCommand(s);
panel.add(button);
button = new JButton(t);
button.addActionListener(this);
button.setActionCommand(t);
panel.add(button);
//Match the SpringLayout's gap, subtracting 5 to make
//up for the default gap FlowLayout provides.
panel.setBorder(BorderFactory.createEmptyBorder(0, 0,
10, 10));
return panel;
}
/*//adding actionlistener to the buttons
public void actionPerformed(ActionEvent e) {
if ("clear".equals(e.getActionCommand())) {
System.out.exit(0);
}}*/
//upto here for buttons
protected JComponent createcombo(final int t) {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
JLabel rol=new JLabel(" Roll No :");
JComboBox petList = new JComboBox();
DataInputStream readFile;
try
{
if(t==1){
readFile = new DataInputStream(new FileInputStream("irollno.txt"));}
else{
readFile = new DataInputStream(new FileInputStream("roll.txt"));}
String rec = readFile.readLine();
while(rec != null)
{
StringTokenizer st = new StringTokenizer(rec,"#");
while(st.hasMoreTokens())
{
//System.out.println(st.nextToken()+"\t");
petList.addItem(st.nextToken());
rec = readFile.readLine(); //Random Access File
}
readFile.close();
}
}
catch(Exception e)
{
//e.printStackTrace();
}
//petList.setSelectedIndex(0);
petList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource() ;
newSelection = (String)cb.getSelectedItem();
if(t==1){
//count1=0;
reformat1(newSelection); }
if(t==0){
reformat(newSelection);
wcoboxchange=1;
}
}
});
panel.add("EAST",rol);
panel.add(petList);
//Match the SpringLayout's gap, subtracting 5 to make
//up for the default gap FlowLayout provides.
panel.setBorder(BorderFactory.createEmptyBorder(0, 0,
1, 1));
return panel;
}
private void reformat1(String s){
stb.setVisible(true);
pix22.setVisible(true);
int n=0;
for( int i=0; i<count; i++ ) {
listModel.removeElementAt(0);
}
count=0;
fileopen("issuedbook.txt");
String sums1 = (String) ht.get(s);
String bdetails1="";
ht.clear();
fileopen("srecord.txt");
String sums2 = (String) ht.get(s);
ht.clear();
StringTokenizer st3 = new StringTokenizer(sums2,"#");
nameField[0].setText(st3.nextToken());
StringTokenizer st = new StringTokenizer(sums1,"#");
while(st.hasMoreTokens())
{
n++;
String em= st.nextToken();
//changing inside
if(n==1||n==4||n==7){
fileopen1("books.txt");
String rec1= (String) ht1.get(em);
//nameField[0].setText(em);
StringTokenizer st1 = new StringTokenizer(rec1,"#");
while(st1.hasMoreTokens()){
bdetails1=bdetails1+st1.nextToken()+" ";
}
listModel.addElement(bdetails1);
bdetails1="";
count++;
}else{
count1=count1;
st.nextToken();}
//changing innside upto here
}
}
//change
private void reformat(String s){
FileInputStream fin = null;
try{
fin = new FileInputStream("srecord.txt");
}catch(FileNotFoundException e){
//ignoring missing file
}
try {
if(fin != null){
ht.load(fin);
fin.close();
}
}catch(IOException e) {
System.out.println("Error reading file");
}
String sums = (String) ht.get(s);
int i=0;
StringTokenizer st = new StringTokenizer(sums,"#");
while(st.hasMoreTokens())
{
nameField[i].setText(st.nextToken());
i++;
}
//fin.close();
}
//change for compactgrid
public static void makeCompactGrid(Container parent,
int rows, int cols,
int initialX, int initialY,
int xPad, int yPad) {
SpringLayout layout;
try {
layout = (SpringLayout)parent.getLayout();
} catch (ClassCastException exc) {
System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
return;
}
//Align all cells in each column and make them the same width.
Spring x = Spring.constant(initialX);
for (int c = 0; c < cols; c++) {
Spring width = Spring.constant(0);
for (int r = 0; r < rows; r++) {
width = Spring.max(width,
getConstraintsForCell(r, c, parent, cols).
getWidth());
}
for (int r = 0; r < rows; r++) {
SpringLayout.Constraints constraints =
getConstraintsForCell(r, c, parent, cols);
constraints.setX(x);
constraints.setWidth(width);
}
x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
}
//Align all cells in each row and make them the same height.
Spring y = Spring.constant(initialY);
for (int r = 0; r < rows; r++) {
Spring height = Spring.constant(0);
for (int c = 0; c < cols; c++) {
height = Spring.max(height,
getConstraintsForCell(r, c, parent, cols).
getHeight());
}
for (int c = 0; c < cols; c++) {
SpringLayout.Constraints constraints =
getConstraintsForCell(r, c, parent, cols);
constraints.setY(y);
constraints.setHeight(height);
}
y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
}
//Set the parent's size.
SpringLayout.Constraints pCons = layout.getConstraints(parent);
pCons.setConstraint(SpringLayout.SOUTH, y);
pCons.setConstraint(SpringLayout.EAST, x);
}
//upto here compactgrid
//change cons
private static SpringLayout.Constraints getConstraintsForCell(
int row, int col,
Container parent,
int cols) {
SpringLayout layout = (SpringLayout) parent.getLayout();
Component c = parent.getComponent(row * cols + col);
return layout.getConstraints(c);
}
//traints
public static void main(String args[]){
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
SampleDesktop13 td = new SampleDesktop13("Digital Libary System");
td.setSize(width,height-30);
td.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -