Hallo,
ich möchte gerne, dass mein Programm auch mit der Entertaste ausgeführt wird, nachdem man etwas in des Textfeld eingegeben hat. Wie kann ich das am einfachsten machen? Die GUI wurde mit SWING in NetBeans erstellt, also falls das wichtig sein sollte.
[CODE lang="java" title="GUI.java"]import java.io.File;
import javax.swing.JOptionPane;
public class GUI extends javax.swing.JFrame {
public GUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
textFieldName = new javax.swing.JTextField();
createButton = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
textFieldName.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
textFieldName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textFieldNameActionPerformed(evt);
}
});
createButton.setText("Erstellen");
createButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
createButtonMouseClicked(evt);
}
});
createButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createButtonActionPerformed(evt);
}
});
jLabel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
jLabel1.setText("Name des Ordners:");
jLabel2.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
jLabel2.setText("Version 1.0");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(createButton)
.addComponent(textFieldName, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
.addContainerGap(52, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel2))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(createButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addComponent(jLabel2))
);
pack();
}// </editor-fold>
private void textFieldNameActionPerformed(java.awt.event.ActionEvent evt) {
}
private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
private void createButtonMouseClicked(java.awt.event.MouseEvent evt) {
String name = this.textFieldName.getText();
// Wo soll der Ordner erstellt werden?
String path1 = System.getProperty("user.home") + "/Desktop/";
// Abfrage des Ordnernamens vom User
// System.out.println("Gib den Namen für den Ordner ein: ");
path1 = path1 + name;
// Initialisierung der File Klasse
File f0 = new File(path1);
// Ordner erstellen
boolean bool0 = f0.mkdir();
// Die 3 Ordner erstellen
String path2;
path2 = path1 + "/PNGs";
File f1 = new File(path2);
boolean bool1 = f1.mkdir();
String path3;
path3 = path1 + "/JPEGs";
File f2 = new File(path3);
boolean bool2 = f2.mkdir();
String path4;
path4 = path1 + "/RAWs";
File f3 = new File(path4);
boolean bool3 = f3.mkdir();
// Überprüfung, ob alles funktioniert hat
if(bool0 && bool1 && bool2 && bool3 == true){
System.out.println("Folders are created successfully");
JOptionPane.showMessageDialog(this, "Aufgabe wurde erfolgreich ausgeführt!", "Success", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Error: Aufgabe konnte nicht ausgeführt werden!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton createButton;
private javax.swing.JFrame jFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField textFieldName;
// End of variables declaration
}
[/CODE]
Und so sieht das Programm aus:
Vielen Dank schon einmal im Voraus!
ich möchte gerne, dass mein Programm auch mit der Entertaste ausgeführt wird, nachdem man etwas in des Textfeld eingegeben hat. Wie kann ich das am einfachsten machen? Die GUI wurde mit SWING in NetBeans erstellt, also falls das wichtig sein sollte.
[CODE lang="java" title="GUI.java"]import java.io.File;
import javax.swing.JOptionPane;
public class GUI extends javax.swing.JFrame {
public GUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
textFieldName = new javax.swing.JTextField();
createButton = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
textFieldName.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
textFieldName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textFieldNameActionPerformed(evt);
}
});
createButton.setText("Erstellen");
createButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
createButtonMouseClicked(evt);
}
});
createButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createButtonActionPerformed(evt);
}
});
jLabel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
jLabel1.setText("Name des Ordners:");
jLabel2.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
jLabel2.setText("Version 1.0");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(createButton)
.addComponent(textFieldName, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
.addContainerGap(52, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel2))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(createButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addComponent(jLabel2))
);
pack();
}// </editor-fold>
private void textFieldNameActionPerformed(java.awt.event.ActionEvent evt) {
}
private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
private void createButtonMouseClicked(java.awt.event.MouseEvent evt) {
String name = this.textFieldName.getText();
// Wo soll der Ordner erstellt werden?
String path1 = System.getProperty("user.home") + "/Desktop/";
// Abfrage des Ordnernamens vom User
// System.out.println("Gib den Namen für den Ordner ein: ");
path1 = path1 + name;
// Initialisierung der File Klasse
File f0 = new File(path1);
// Ordner erstellen
boolean bool0 = f0.mkdir();
// Die 3 Ordner erstellen
String path2;
path2 = path1 + "/PNGs";
File f1 = new File(path2);
boolean bool1 = f1.mkdir();
String path3;
path3 = path1 + "/JPEGs";
File f2 = new File(path3);
boolean bool2 = f2.mkdir();
String path4;
path4 = path1 + "/RAWs";
File f3 = new File(path4);
boolean bool3 = f3.mkdir();
// Überprüfung, ob alles funktioniert hat
if(bool0 && bool1 && bool2 && bool3 == true){
System.out.println("Folders are created successfully");
JOptionPane.showMessageDialog(this, "Aufgabe wurde erfolgreich ausgeführt!", "Success", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Error: Aufgabe konnte nicht ausgeführt werden!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton createButton;
private javax.swing.JFrame jFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField textFieldName;
// End of variables declaration
}
[/CODE]
Und so sieht das Programm aus:
Vielen Dank schon einmal im Voraus!