Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class SimplePhoneBook extends JPanel implements ActionListener,
ListSelectionListener {
private JButton loadButton, saveButton, insertButton, clearButton;
private JTextField forenameField, surnameField, telnumberField;
private DefaultListModel model;
public SimplePhoneBook(JFrame frame) {
frame.setTitle("Phone Book");
this.setLayout(new BorderLayout());
this.add(this.createFormPanel(), "North");
this.add(this.createListPanel(), "Center");
this.add(this.createButtonPanel(), "South");
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setLocationByPlatform(true);
frame.add(new SimplePhoneBook(frame));
frame.pack();
frame.setVisible(true);
}
public JComponent createFormPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(new JLabel("Forename"));
panel.add(forenameField = new JTextField(10));
panel.add(new JLabel("Surname"));
panel.add(surnameField = new JTextField(10));
panel.add(new JLabel("Phone Number"));
panel.add(telnumberField = new JTextField(10));
return panel;
}
public JComponent createListPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JList list = new JList(model = new DefaultListModel());
list.addListSelectionListener(this);
JScrollPane scrollPane = new JScrollPane(list);
panel.add(scrollPane);
return panel;
}
public JComponent createButtonPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
loadButton = new JButton("Load");
saveButton = new JButton("Save");
insertButton = new JButton("Insert");
clearButton = new JButton("Clear");
loadButton.setActionCommand("laod");
saveButton.setActionCommand("save");
insertButton.setActionCommand("insert");
clearButton.setActionCommand("clear");
loadButton.addActionListener(this);
saveButton.addActionListener(this);
insertButton.addActionListener(this);
clearButton.addActionListener(this);
panel.add(loadButton);
panel.add(insertButton);
panel.add(clearButton);
panel.add(saveButton);
return panel;
}
public void insert() {
model.addElement(forenameField.getText() + " * "
+ surnameField.getText() + " * " + telnumberField.getText());
forenameField.setText("");
surnameField.setText("");
telnumberField.setText("");
}
public void clear() {
model.clear();
}
public void load() {
Scanner scanner = null;
try {
scanner = new Scanner(new File("telefonbuch.dat"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
model.clear();
while (scanner.hasNextLine())
model.addElement(scanner.nextLine());
}
public void save() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < model.size(); i++) {
sb.append(model.get(i) + "\n");
}
try {
FileWriter fw = new FileWriter(new File("telefonbuch.dat"));
fw.write(sb.toString());
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equalsIgnoreCase("laod")) {
this.load();
} else if (s.equalsIgnoreCase("save")) {
this.save();
} else if (s.equalsIgnoreCase("insert")) {
this.insert();
} else if (s.equalsIgnoreCase("clear")) {
this.clear();
}else
System.err.println("Else");
}
public void valueChanged(ListSelectionEvent e) {
JList list = (JList) e.getSource();
String selection = model.get(list.getSelectedIndex()).toString();
String data[] = selection.split("\\*");
forenameField.setText("" + data[0].trim());
surnameField.setText("" + data[1].trim());
telnumberField.setText("" + data[2].trim());
}
}
InputStream resourceAsStream = getClass().getClassLoader()
.getResourceAsStream("RunApp.properties");
InputStreamReader reader = new InputStreamReader(resourceAsStream);
BufferedReader bufferedReader = new BufferedReader(reader);
/**
* Initialising the output
*/
String newline = System.getProperty("line.separator");
try {
FileWriter fileWriter = new FileWriter(
"src//main//java//RunApp.properties");
try {
boolean done = false;
while (!done) {
String sTemp = bufferedReader.readLine();
if (sTemp == null)
done = true;
else {
if (sTemp.contains("PageFormat="))// updating
sTemp = "PageFormat=" + newFormat;
fileWriter.write(sTemp);
fileWriter.write("\n");// darum gehts
fileWriter.close();
System.out.println(sTemp);
}
}
ich bin am ende mit meinem Latein...
Hat keiner ne idee wie das funktioniern kann. Ich kriegs einfach nicht hin
Gruß Serpil
package de.cmk.util;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class CMKProperties extends java.util.Properties
{
public CMKProperties()
{
super();
}
public CMKProperties(java.util.Properties defaults)
{
super(defaults);
}
/**
* Arbeitet prinzipeille genauso wie die Elternklasse jedoch werden die Properites
* sortiert gemäß gegebenen Comparators ausgegeben.
* @param out
* @param header
* @param keyComp
* @throws IOException
*/
public void store(OutputStream out, String header, Comparator keyComp) throws IOException
{
BufferedWriter awriter;
awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1"));
if (header != null)
{
writeln(awriter, "#" + header);
}
writeln(awriter, "#" + new Date().toString());
synchronized (this)
{
SortedSet keys = new TreeSet(keyComp);
keys.addAll(keySet());
Iterator kIter = keys.iterator();
while(kIter.hasNext())
{
String key = (String)kIter.next();
String val = (String)get(key);
key = saveConvert(key, true);
/* No need to escape embedded and trailing spaces for value, hence
* pass false to flag.
*/
val = saveConvert(val, false);
writeln(awriter, key + "=" + val);
}
}
awriter.flush();
}
//----------------------------------------------------------------------------------------------------
// Leider sind die entscheidenden Routinen private. Hätten besser protected sein sollen,
// dann hätte ich sie nicht kopieren müssen :-(((((((
/**
*
* @param bw
* @param s
* @throws IOException
*/
private static void writeln(BufferedWriter bw, String s) throws IOException
{
bw.write(s);
bw.newLine();
}
/**
* @param theString
* @param escapeSpace
* @return
*/
private String saveConvert(String theString, boolean escapeSpace) {
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len*2);
for(int x=0; x<len; x++)
{
char aChar = theString.charAt(x);
switch(aChar)
{
case ' ':
if (x == 0 || escapeSpace)
{
outBuffer.append('\\');
}
outBuffer.append(' ');
break;
case '\\':
outBuffer.append('\\'); outBuffer.append('\\');
break;
case '\t':
outBuffer.append('\\'); outBuffer.append('t');
break;
case '\n':
outBuffer.append('\\'); outBuffer.append('n');
break;
case '\r':
outBuffer.append('\\'); outBuffer.append('r');
break;
case '\f':
outBuffer.append('\\'); outBuffer.append('f');
break;
default:
if ((aChar < 0x0020) || (aChar > 0x007e))
{
outBuffer.append('\\');
outBuffer.append('u');
outBuffer.append(toHex((aChar >> 12) & 0xF));
outBuffer.append(toHex((aChar >> 8) & 0xF));
outBuffer.append(toHex((aChar >> 4) & 0xF));
outBuffer.append(toHex( aChar & 0xF));
}
else
{
if (specialSaveChars.indexOf(aChar) != -1)
outBuffer.append('\\');
outBuffer.append(aChar);
}
}
}
return outBuffer.toString();
}
private static char toHex(int nibble)
{
return hexDigit[(nibble & 0xF)];
}
/** A table of hex digits */
private static final char[] hexDigit =
{
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
private static final String specialSaveChars = "=: \t\r\n\f#!";
}