N
NochKeinGuterJavaStudent
Gast
Ich soll ein Interface programmieren das ein array annimmt, auf Knopfdruck sortiert und in einem anderen Feld anzeigt. Meine Frage, wie gebe ich den SortButton die functionaliteat?
Ich stelle mir das Interface so vor (just run the code, don't go in details):
Dazu moechte ich BubbleSort verwenden:
Ich stelle mir das Interface so vor (just run the code, don't go in details):
Code:
/*
* GUI.java
*
* Created on April 24, 2008, 6:43 PM
*/
package learn;
import java.awt.Graphics;
import java.util.Random;
/**
*
* @author alexander
*/
public class GUI extends javax.swing.JFrame {
/** Creates new form GUI */
public GUI() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
firstLabel = new javax.swing.JLabel();
secondLabel = new javax.swing.JLabel();
numberField = new javax.swing.JTextField();
numberText = new javax.swing.JLabel();
sortButton = new javax.swing.JButton();
exitButton = new javax.swing.JButton();
clearButton = new javax.swing.JButton();
sortBox = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setBackground(new java.awt.Color(200, 200, 100));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
firstLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18));
firstLabel.setText("Java Programming");
secondLabel.setText("Assignment 3");
numberField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
numberFieldActionPerformed(evt);
numberFieldActionPerformed1(evt);
}
});
numberText.setText("Enter random number:");
sortButton.setText("Sort");
sortButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sortButtonActionPerformed(evt);
}
});
sortButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
sortButtonMouseClicked(evt);
}
});
exitButton.setText("Exit");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
clearButton.setText("Clear");
clearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearButtonActionPerformed(evt);
}
});
sortBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sortBoxActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(146, 146, 146)
.add(secondLabel))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(numberText))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(sortButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
.add(numberField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)))
.add(layout.createSequentialGroup()
.add(111, 111, 111)
.add(firstLabel))
.add(layout.createSequentialGroup()
.addContainerGap(164, Short.MAX_VALUE)
.add(clearButton)
.add(66, 66, 66)
.add(exitButton))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(sortBox, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(20, 20, 20)
.add(firstLabel)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(secondLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(46, 46, 46)
.add(numberText)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(numberField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(sortButton)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(sortBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(12, 12, 12)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(exitButton)
.add(clearButton))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void numberFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void sortButtonActionPerformed(java.awt.event.ActionEvent evt) {
numberField.getInputContext();
//instead of "print", "setText"
}
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit (0);
}
private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
numberField.setText("");
sortBox.setText("");
// TODO add your handling code here:
}
private void numberFieldActionPerformed1(java.awt.event.ActionEvent evt) {
}
private void sortBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void sortButtonMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton clearButton;
private javax.swing.JButton exitButton;
private javax.swing.JLabel firstLabel;
private javax.swing.JTextField numberField;
private javax.swing.JLabel numberText;
private javax.swing.JLabel secondLabel;
private javax.swing.JTextField sortBox;
private javax.swing.JButton sortButton;
// End of variables declaration
}
Dazu moechte ich BubbleSort verwenden:
Code:
public void sort(int b[]) {
int temp;
for (int i = 0; i < b.length - 1; i++) {
for (int j = i + 1; j < b.length; j++) {
if (b[i] > b[j]) {
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
}
}