我编程不长,并且遇到未知错误的问题。 我已经建立了一个用于测试排序算法的gui,但是一直没有出错,我无法看到问题出在哪里
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SorterTool2 extends JFrame{
// Instance Variables
private JButton jbtBubble, jbtSelection, jbtInsertion;
private JLabel jlblSelect, jlblBubbleTime1, jlblBubbleTime2,
jlblSelectionTime1, jlblSelectionTime2, jlblInsertion1,
jlblInsertion2;
private JTextField jtfBubbleField1, jtfBubbleField2, jtfSelectField1,
jtfSelectField2, jtfInsertionField1, jtfInsertionField2;
private JPanel content, topPanel, midPanel1, midPanel2, bottomPanel;
private JComboBox jCombo;
Random randNum = new Random();
//private int max = 5000;
private int[] myArray;
long timerStart;
long timerStop;
ListenerClass changes = new ListenerClass();
public SorterTool2() {
//Panels
content = new JPanel(new GridLayout(4, 1, 0, 0));
topPanel = new JPanel(new FlowLayout());
midPanel1 = new JPanel();
midPanel1.setLayout(new GridLayout(2, 3, 15, 5));
midPanel2 = new JPanel();
midPanel2.setLayout(new GridLayout(2, 3, 15, 5));
bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 3, 30, 5));
add(content);
content.add(topPanel);
content.add(midPanel1);
content.add(midPanel2);
content.add(bottomPanel);
midPanel1.setBorder(BorderFactory
.createTitledBorder("Sorted Array Analysis"));
midPanel2.setBorder(BorderFactory
.createTitledBorder("Unsorted Array Analysis"));
// Parts for the panels
jlblSelect = new JLabel(
"Please select the number of elements for the array");
jlblBubbleTime1 = new JLabel("Bubble sort in milliseconds");
jlblBubbleTime2 = new JLabel("Bubble sort in milliseconds");
jlblSelectionTime1 = new JLabel("Selection sort in milliseconds");
jlblSelectionTime2 = new JLabel("Selection sort in milliseconds");
jlblInsertion1 = new JLabel("Insertion sort in milliseconds");
jlblInsertion2 = new JLabel("Insertion sort in milliseconds");
// Textfields
jtfBubbleField1 = new JTextField(4);
jtfBubbleField2 = new JTextField(4);
jtfSelectField1 = new JTextField(4);
jtfSelectField2 = new JTextField(4);
jtfInsertionField1 = new JTextField(4);
jtfInsertionField2 = new JTextField(4);
String[] numbers = {"100", "1000", "10000"};
jCombo = new JComboBox(numbers);
int test = (Integer)jCombo.getSelectedItem();
//create the list
for (int i = 0; i < test; i++) {
//random numbers from 1 to max number:
myArray[i] = randNum.nextInt(30) + 1;
}
// Add elements
topPanel.add(jCombo);
topPanel.add(jlblSelect);
midPanel1.add(jlblBubbleTime1);
midPanel1.add(jlblSelectionTime1);
midPanel1.add(jlblInsertion1);
midPanel1.add(jtfBubbleField1);
midPanel1.add(jtfSelectField1);
midPanel1.add(jtfInsertionField1);
midPanel2.add(jlblBubbleTime2);
midPanel2.add(jlblSelectionTime2);
midPanel2.add(jlblInsertion2);
midPanel2.add(jtfBubbleField2);
midPanel2.add(jtfSelectField2);
midPanel2.add(jtfInsertionField2);
// Buttons
jbtBubble = new JButton("Bubble");
jbtSelection = new JButton("Selection");
jbtInsertion = new JButton("Insertion");
bottomPanel.add(jbtBubble);
bottomPanel.add(jbtSelection);
bottomPanel.add(jbtInsertion);
// The Bubble sort button
jbtBubble.addActionListener(changes);
}
public static void main(String[] args) {
SorterTool2 frame = new SorterTool2();
frame.setTitle("Sorter Tool");
frame.pack();
frame.setSize(550, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class ListenerClass implements ActionListener{
public void actionPerformed(ActionEvent e) {
// The iteration through the array
// and the check value loop below it
for (int i = 0; i < myArray.length - 1; i++) {
for (int j = 0; j < myArray.length - i - 1; j++) {
if (myArray[j] > myArray[j + 1]) {
// The swap method if the index
// value is smaller
int temp = myArray[j + 1];
myArray[j + 1] = myArray[j];
myArray[j] = temp;
}
}
}
jtfBubbleField2.setText(String.valueOf(1000));
}
}
}
有人可以尝试一下这个,看看我哪里出错了吗? 我刚刚只有bubblesort算法,如果我可以使用,我可以添加其他2个分拣机。我认为它可能与从JComboBox调用的数字有关。
我非常感激,因为我花了好几个小时。我在几个小时内完成了GUI。
非常感谢你。
答案 0 :(得分:2)
我清理了所有语法错误。我没有尝试让任何GUI按钮正常工作。
必须始终通过调用SwingUtilities invokeLater方法启动Swing应用程序。这将在Event Dispatch thread (EDT)上创建和更新Swing组件。
您的JFrame方法按特定顺序调用必须。我重新安排了JFrame方法调用的顺序。
如果Integer表示为String,则必须将String转换为Integer。我使用Integer valueOf静态方法进行转换。
创建数组时,必须告诉Java编译器该数组包含多少元素。
这是清理过的代码。
package com.ggl.testing;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SorterTool2 implements Runnable {
// Instance Variables
private JButton jbtBubble, jbtSelection, jbtInsertion;
private JLabel jlblSelect, jlblBubbleTime1, jlblBubbleTime2,
jlblSelectionTime1, jlblSelectionTime2, jlblInsertion1,
jlblInsertion2;
private JTextField jtfBubbleField1, jtfBubbleField2, jtfSelectField1,
jtfSelectField2, jtfInsertionField1, jtfInsertionField2;
private JPanel content, topPanel, midPanel1, midPanel2, bottomPanel;
private JComboBox<String> jCombo;
private Random randNum = new Random();
// private int max = 5000;
private int[] myArray;
// private long timerStart;
// private long timerStop;
private ListenerClass changes = new ListenerClass();
@Override
public void run() {
JFrame frame = new JFrame();
frame.setTitle("Sorter Tool");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Panels
content = new JPanel(new GridLayout(4, 1, 0, 0));
topPanel = new JPanel(new FlowLayout());
midPanel1 = new JPanel();
midPanel1.setLayout(new GridLayout(2, 3, 15, 5));
midPanel2 = new JPanel();
midPanel2.setLayout(new GridLayout(2, 3, 15, 5));
bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 3, 30, 5));
frame.add(content);
content.add(topPanel);
content.add(midPanel1);
content.add(midPanel2);
content.add(bottomPanel);
midPanel1.setBorder(BorderFactory
.createTitledBorder("Sorted Array Analysis"));
midPanel2.setBorder(BorderFactory
.createTitledBorder("Unsorted Array Analysis"));
// Parts for the panels
jlblSelect = new JLabel(
"Please select the number of elements for the array");
jlblBubbleTime1 = new JLabel("Bubble sort in milliseconds");
jlblBubbleTime2 = new JLabel("Bubble sort in milliseconds");
jlblSelectionTime1 = new JLabel("Selection sort in milliseconds");
jlblSelectionTime2 = new JLabel("Selection sort in milliseconds");
jlblInsertion1 = new JLabel("Insertion sort in milliseconds");
jlblInsertion2 = new JLabel("Insertion sort in milliseconds");
// Textfields
jtfBubbleField1 = new JTextField(4);
jtfBubbleField2 = new JTextField(4);
jtfSelectField1 = new JTextField(4);
jtfSelectField2 = new JTextField(4);
jtfInsertionField1 = new JTextField(4);
jtfInsertionField2 = new JTextField(4);
String[] numbers = { "100", "1000", "10000" };
jCombo = new JComboBox<String>(numbers);
int test = Integer.valueOf(jCombo.getSelectedItem().toString());
// create the list
myArray = new int[test];
for (int i = 0; i < test; i++) {
// random numbers from 1 to max number:
myArray[i] = randNum.nextInt(30) + 1;
}
// Add elements
topPanel.add(jCombo);
topPanel.add(jlblSelect);
midPanel1.add(jlblBubbleTime1);
midPanel1.add(jlblSelectionTime1);
midPanel1.add(jlblInsertion1);
midPanel1.add(jtfBubbleField1);
midPanel1.add(jtfSelectField1);
midPanel1.add(jtfInsertionField1);
midPanel2.add(jlblBubbleTime2);
midPanel2.add(jlblSelectionTime2);
midPanel2.add(jlblInsertion2);
midPanel2.add(jtfBubbleField2);
midPanel2.add(jtfSelectField2);
midPanel2.add(jtfInsertionField2);
// Buttons
jbtBubble = new JButton("Bubble");
jbtSelection = new JButton("Selection");
jbtInsertion = new JButton("Insertion");
bottomPanel.add(jbtBubble);
bottomPanel.add(jbtSelection);
bottomPanel.add(jbtInsertion);
// The Bubble sort button
jbtBubble.addActionListener(changes);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new SorterTool2());
}
class ListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
// The iteration through the array
// and the check value loop below it
for (int i = 0; i < myArray.length - 1; i++) {
for (int j = 0; j < myArray.length - i - 1; j++) {
if (myArray[j] > myArray[j + 1]) {
// The swap method if the index
// value is smaller
int temp = myArray[j + 1];
myArray[j + 1] = myArray[j];
myArray[j] = temp;
}
}
}
jtfBubbleField2.setText(String.valueOf(1000));
}
}
}