Hi,
wie kann ich ein programm schließen und gleichzeitig wieder starten?
wie kann ich ein programm schließen und gleichzeitig wieder starten?
Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
package browser;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Gamebrowser
{
Vector<String> genres=new Vector<String>();
Vector<Gamepfad> games=new Vector<Gamepfad>();
//ArrayList<String> genres=new ArrayList<String>();;
PopupMenu popup;
int nr=0;
public Gamebrowser(){
final TrayIcon trayIcon;
if (SystemTray.isSupported()) {
popup = new PopupMenu();
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("file/tray.gif");
ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
ActionListener addgenre = new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Addgenre(genres);
}
};
ActionListener addgame = new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Addgame(genres,games);
}
};
trayIcon = new TrayIcon(image, "Tray Demo", popup);
trayIcon.setImageAutoSize(true);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
MenuItem exit=new MenuItem("Schließen");
exit.addActionListener(exitListener);
Menu add=new Menu("Hinzufügen");
MenuItem game=new MenuItem("Spiel");
game.addActionListener(addgame);
MenuItem genre=new MenuItem("Genre");
genre.addActionListener(addgenre);
add.add(game);
add.add(genre);
loadgenandgam();
popup.add(add);
popup.add(exit);
} else {
System.err.println("System tray is currently not supported.");
}
}
public void loadgenandgam(){
loadgenres();
loadgames();
Menu mg;
MenuItem mig=new MenuItem();
for(int i=0;i<genres.size();++i){
mg=new Menu(genres.get(i));
for(int j=0;j<games.size();++j){
if(games.get(j).togenre.equals(genres.get(i))){
mig=new MenuItem(games.get(j).name);
nr=j;
mig.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec(games.get(nr).exepfad);
} catch (Exception exc) {
}
}
});
mg.add(mig);
}
}
popup.add(mg);
}
}
public void loadgames(){
try {
String gam="";
BufferedReader filega = new BufferedReader(new FileReader("file/games.txt"));
String ga[]=new String[3];
while ((gam=filega.readLine()) != null){
ga=gam.split(";");
games.add(new Gamepfad(ga[0],ga[1],ga[2]));
}
} catch (Exception e) {
}
}
public void loadgenres(){
try {
String gen="";
BufferedReader filege = new BufferedReader(new FileReader("file/genres.txt"));
while ((gen=filege.readLine()) != null)genres.add(gen);
} catch (Exception e) {
}
}
public static void main(String[] args)
{
Gamebrowser start = new Gamebrowser();
}
}
class Addgame extends JFrame{
JTextField pfad=new JTextField();
JTextField name=new JTextField();
JButton exepfad;
Vector<String> genres;
Vector<Gamepfad> games;
JComboBox combogen;
public Addgame(Vector<String> genres,Vector<Gamepfad> games){
super("Spiel hinzufügen");
this.genres=genres;
this.games=games;
this.setSize(204,125);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setVisible(true);
JLabel text=new JLabel("Name");
text.setSize(190, 15);
text.setLocation(5, 5);
name.setSize(190, 25);
name.setLocation(5, 25);
pfad.setSize(140, 25);
pfad.setLocation(5, 50);
exepfad=new JButton("Pfad wählen");
exepfad.setSize(50, 25);
exepfad.setLocation(145,50);
exepfad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == exepfad) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.showOpenDialog(Addgame.this);
File fileSelected = fileChooser.getSelectedFile();
pfad.setText(fileSelected.getAbsolutePath());
}
}
});
combogen=new JComboBox(genres);
combogen.setSize(120,25);
combogen.setLocation(5, 75);
JButton add=new JButton("Hinzufügen");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
addgame();
}
});
add.setSize(75, 25);
add.setLocation(120, 75);
this.add(text);
this.add(name);
this.add(pfad);
this.add(exepfad);
this.add(add);
this.add(combogen);
}
public void addgame(){
try {
BufferedWriter savegam=new BufferedWriter(new FileWriter("file/games.txt"));
for(int j=0;j<games.size();++j){
savegam.write(games.get(j).name+";");
savegam.write(games.get(j).exepfad+";");
savegam.write(games.get(j).togenre);
savegam.newLine();
}
savegam.write(name.getText()+";");
savegam.write(pfad.getText()+";");
savegam.write(combogen.getSelectedItem().toString());
savegam.newLine();
games.add(new Gamepfad(name.getText(),pfad.getText(),combogen.getSelectedItem().toString()));
savegam.close();
} catch (Exception e) {
}
}
}
class Addgenre extends JFrame{
String games[];
JLabel fehler=new JLabel();
public JTextField genre = new JTextField();
Vector<String> genres;
public Addgenre(Vector<String> genres){
super("Genre hinzufügen");
this.genres=genres;
this.setSize(200,150);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setVisible(true);
JLabel text=new JLabel("Genre");
text.setSize(150, 25);
text.setLocation(25, 0);
genre.setSize(150, 25);
genre.setLocation(25, 30);
ActionListener checkandadd = new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkandadd();
}
};
JButton add=new JButton("Hinzufügen");
add.addActionListener(checkandadd);
add.setSize(150, 25);
add.setLocation(25,60);
fehler.setSize(150,25);
fehler.setLocation(25, 90);
this.add(text);
this.add(add);
this.add(fehler);
this.add(genre);
}
public void checkandadd(){
boolean set=true;
for(int i=0;i<genres.size();++i){
if(genre.getText().equals(genres.get(i))){
fehler.setText("Ist bereits Vorhanden!");
set=false;
break;
}
if(genre.getText().equals(""))set=false;
}
if(set){
fehler.setText("");
try {
BufferedWriter savegen=new BufferedWriter(new FileWriter("file/genres.txt"));
for(int j=0;j<genres.size();++j){
savegen.write(genres.get(j));
savegen.newLine();
}
savegen.write(genre.getText());
savegen.newLine();
genres.add(genre.getText());
savegen.close();
fehler.setText("Hinzugefügt");
} catch (Exception e) {
}
}
}
}
class Gamepfad{
public String name;
public String exepfad;
public String togenre;
public Gamepfad(String name,String exepfad,String togenre) {
this.name = name;
this.exepfad = exepfad;
this.togenre = togenre;
}
}