java 排序 gui 的未知错误
Unknown errors with java sorting 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。
非常感谢。
我清理了你所有的语法错误。我没有尝试让任何 GUI 按钮正常工作。
您必须始终通过调用 SwingUtilities invokeLater 方法来启动 Swing 应用程序。这将在 Event Dispatch thread (EDT).
上创建和更新 Swing 组件
您的 JFrame 方法调用必须按特定顺序进行。我将 JFrame 方法调用重新排列为该顺序。
如果整数表示为字符串,则必须将字符串转换为整数。我使用 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));
}
}
}
我的编程时间不长,遇到未知错误的问题。 我已经构建了一个用于测试排序算法的图形用户界面,但不断出现错误,我看不出问题出在哪里
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。
非常感谢。
我清理了你所有的语法错误。我没有尝试让任何 GUI 按钮正常工作。
您必须始终通过调用 SwingUtilities invokeLater 方法来启动 Swing 应用程序。这将在 Event Dispatch thread (EDT).
上创建和更新 Swing 组件
您的 JFrame 方法调用必须按特定顺序进行。我将 JFrame 方法调用重新排列为该顺序。
如果整数表示为字符串,则必须将字符串转换为整数。我使用 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));
}
}
}