Jeg har samlet et par java tips som det kunne være nogen kunne bruge.
________________________________________________________________
TIP1:
At kalde en eksekverbar fil via Runtime.exec().
import java.lang.*;
import java.io.*;
public class eksternKommando
{
public static void main(String[] args)
{
Runtime runtime = Runtime.getRunTime();
try
{
Process process = runtime.exec("dir.exe") ;
}
catch(IOException e)
{
System.out.println("Der opstod en fejl : " + e);
e.printStackTrace();
}
}
}
________________________________________________________________
TIP2:
Udskrivning af alle filnavne i et angivet dir.
import java.io.*;
public class Dir
{
public static void main(String[] args)
{
dir("c:\\test");
}//main
public static void dir(String str)
{
File dir = new File(str);
String[] dirlist = dir.list();
for (int i = 0; i < dirlist.length; i++)
{
String fname = dir + System.getProperty("file.separator") + dirlist[i];
System.out.println(fname);
if ((new File(fname)).isDirectory())
{
dir(fname);
}//if
}//for
}//dir
}//Dir
________________________________________________________________
TIP3:
Ultra kort eksempel på tabel design i java.
public class tableEx
{
final String[] columnNames = {"Vare Nr",
"Vare Navn",
"Vare Beskrivelse",
"Dato",
"Antal",
"Lokation",
"Pris"};
Object[] data = {"1234567890001","TandPasta","Colgate Natural Whitening","25-04-2002","100","10-00-10","19.50"};
private JTable table = new JTable();
DefaultTableModel tableModel = (DefaultTableModel)table.getModel();
//Create the scroll pane and add the table to it.
private JScrollPane scrollPane = new JScrollPane(table);
public static void main(String[] args)
{
tableModel.setColumnIdentifiers(columnNames);
tableModel.addRow(data);
}
public fill()
{
for(int i =0 ; i < 10; i++)
tableModel.addRow(data);
}
public remove()
{
tableModel.setNumRows(0);
}
public updateTable()
{
table.updateUI();
}
________________________________________________________________
TIP4:
Metode til at centrere et Container objekt, Frame osv.
public static void CenterWindow(Container obj)
{
Dimension screen;
Dimension comp;
Toolkit tk;
int c_x,c_y;
int s_x,s_y;
int x,y;
tk = obj.getToolkit();
screen = tk.getScreenSize();
comp = obj.size();
c_y = comp.height / 2;
c_x = comp.width / 2;
s_y = screen.height / 2;
s_x = screen.width / 2;
x = s_x - c_x;
y = s_y - c_y;
obj.move(x,y);
}
________________________________________________________________
TIP5:
Metode til at erstatte første eller alle forekomster af af en given streng i en String.
public class StringReplacer
{
public static String replace(String orig, String strReplace, String strWith, boolean all)
{
if (orig == null || strReplace == null || strReplace.length() == 0 || strWith == null)
throw new IllegalArgumentException("Null or zero-length arguments.");
StringBuffer buffOrig = new StringBuffer(orig);
int i = 0;
while (i + strReplace.length() <= buffOrig.length())
{
if (buffOrig.substring(i, i + strReplace.length()).equals(strReplace))
{
buffOrig.replace(i, i + strReplace.length(), strWith);
if (!all)
break;
else
i += strWith.length();
}
else
i++;
}
return buffOrig.toString();
}
}
________________________________________________________________
TIP6:
Hvordan man for adgang til en cookie på klient siden.
Cookie klassen er placeret : javax.servlet.http.Cookie
Cookie[] cookies = request.getCookies();
for(int i = 0; i<cookies.length;i++)
{
Cookie thisCookie = cookies[i];
if(thisCookie.getName().equals("Cookiename"))
{
//Kode går her!!
}
else
{
//Hvis cookien ikke eksistere
}
}
________________________________________________________________
TIP7:
At lukke et vindue via X'et i øverste højre hjørne.
public static void main(String args[])
{
Converter f = new Converter();
f.setSize(400,300);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowsClosing(WindowEvent e)
{
System.exit(0);
}
});
} // main()
________________________________________________________________
TIP8:
Konverter et binært tal til integer:
radix = 2;
Integer.parseInt(String s,int radix);
________________________________________________________________
TIP9:
Få pc speakeren til at udsende et bip.
Toolkit.getDefaultToolkit().beep();
________________________________________________________________
TIP10:
At eksekvere en kommando som indeholder et space.
String[] kommando = {"cat","file.axx"};
Runtime.getRuntime().Exec(kommando);
________________________________________________________________
/Xor
|