gpt4 book ai didi

java - 编译后不显示GUI

转载 作者:行者123 更新时间:2023-12-01 10:36:05 25 4
gpt4 key购买 nike

我目前正在开发一个地址簿程序,并且之前已经让它工作了......但由于某种原因,我无法在 DrJava 上打开 GUI 窗口

我尝试编译它,没有错误。我确实注释掉了异常,因为它给我带来了问题,所以我将修复该错误,但无论如何这是针对 FileIO 的

奇怪的是我无法弹出 GUI,我真的不知道为什么它不起作用......

    //imports
import javax.swing.*;
import java.io.PrintWriter;
import java.io.File;
import java.io.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*; //Adding the event since we will now be using an action listener
import java.io.FileNotFoundException;


public class AddressbookFinal extends JFrame implements ActionListener//actionlistener enables us to set actions to buttons
{
// Create some Panels
JPanel pan1 = new JPanel ();//panel 1
JPanel pan2 = new JPanel ();//panel 2
File writeFile = new java.io.File ("Address book.txt"); //create the file to write to
File readFile = new java.io.File ("Address book.txt");//read the file
//global variables
static final int MAX = 100;//lenght of the arrays
String lastname[] = new String [MAX];//holds last names
String names[] = new String [MAX];//holds first names
String e_mail[] = new String [MAX];// holds emails
String address[] = new String [MAX];//holds addresses
String phone[] = new String [MAX];// holds phone numbers
int total;//the counter that keeps track of our position

// Create some GUI components
JLabel lastnameLabel = new JLabel ("Last name", JLabel.LEFT);
JTextField lastnameField = new JTextField (40);
JLabel nameLabel = new JLabel ("First Name ", JLabel.LEFT);
JTextField nameField = new JTextField (40);
JLabel AddressLabel = new JLabel ("Address", JLabel.LEFT);
JTextField AddressField = new JTextField (40);
JLabel PhoneLabel = new JLabel ("Phone", JLabel.LEFT);
JTextField PhoneField = new JTextField (40);
JLabel EmailLabel = new JLabel ("Email", JLabel.LEFT);
JTextField EmailField = new JTextField (40);
JButton deleteButton = new JButton ("Delete");
JButton saveButton = new JButton ("Save");
JButton modifyButton = new JButton ("Update");
JButton previousButton = new JButton ("<<");
JButton searchButton = new JButton ("Search");
JButton nextButton = new JButton (">>");
JButton readButton = new JButton ("Read");
JButton writeButton = new JButton ("Write");
JButton clearButton = new JButton ("Clear");
JLabel instructionsLabel = new JLabel ("Enter your name", JLabel.LEFT);
JButton exitButton = new JButton ("Exit");


// CONSTRUCTOR - Setup your GUI here
public void HamzaTest () throws IOException
{
total = 0;
//all fields to empty
for (int i = total ; i < names.length ; i++)
{
lastname [i] = "";
names [i] = "";
e_mail [i] = "";
address [i] = "";
phone [i] = "";
}

setTitle ("Hello World!"); // Create a window with a title
setSize (640, 480); // set the window size

// Create some Layouts
GridLayout layout1 = new GridLayout (4, 1);
GridLayout layout2 = new GridLayout (5, 1);

// Set the frame and both panel layouts
setLayout (layout1); // Setting layout for the whole frame
pan1.setLayout (layout2); // Layout for Pan1
pan2.setLayout (layout1); // Layout for Pan2

saveButton.addActionListener (this); // Add an action listener to the buttons
deleteButton.addActionListener (this);
modifyButton.addActionListener (this);
previousButton.addActionListener (this);
searchButton.addActionListener (this);
nextButton.addActionListener (this);
clearButton.addActionListener (this);
exitButton.addActionListener (this);
readButton.addActionListener (this);
writeButton.addActionListener (this);


// this allows the program to know if
// the button was pressed

// Add all the components to the panels
pan1.add (lastnameLabel);
pan1.add (lastnameField);
pan1.add (nameLabel);
pan1.add (nameField);
pan1.add (AddressLabel);
pan1.add (AddressField);
pan1.add (PhoneLabel);
pan1.add (PhoneField);
pan1.add (EmailLabel);
pan1.add (EmailField);
//panel 2 is for buttons
pan2.add (saveButton);
pan2.add (deleteButton);
pan2.add (modifyButton);
pan2.add (previousButton);
pan2.add (searchButton);
pan2.add (nextButton);
pan2.add (clearButton);
pan2.add (readButton);
pan2.add (writeButton);
pan2.add (exitButton);
pan2.add (instructionsLabel);

// add the panels to the frame and display the window
add (pan1);
add (pan2);
setVisible (true);//set the GUI window to visible
}


// ACTION LISTENER - This method runs when an event occurs
// Code in here only runs when a user interacts with a component
// that has an action listener attached to it
public void actionPerformed (ActionEvent event) /* throws IOException */
{

String command = event.getActionCommand (); // find out the name of the
// component
// that was used

if (command.equals ("Save"))
{ // if the save button was pressed
while (lastname [total] != "")//find the current position and save in the nearest
total++;
System.out.println ("save button pressed"); // display message inconsole(for testing)
System.out.println ("name:" + nameField.getText ()); // get the info located in the field component
instructionsLabel.setText ("Thank you " + nameField.getText ()); // change the label message to 'thank you'
//get the String values from the fields and set them to our arrays
lastname [total] = lastnameField.getText ();
names [total] = nameField.getText ();
e_mail [total] = EmailField.getText ();
address [total] = AddressField.getText ();
phone [total] = PhoneField.getText ();
//move to the next position
total++;
//sort the lists
Sort (lastname, names, e_mail, address, phone, total);
}
else if (command.equals ("Delete"))//if delete is pressed
{
System.out.println ("delete button pressed");
System.out.println (nameField.getText () + "was deleted");
//set the current position to empty
lastname [total] = ("");
names [total] = ("");
e_mail [total] = ("");
address [total] = ("");
phone [total] = ("");
instructionsLabel.setText ("Deleted");
}
else if (command.equals ("Update"))//modify button was pressed
{
//change a field without changing the current position meaning that it will overwrite what you already have
System.out.println (lastnameField.getText () + "was modified");
lastname [total] = lastnameField.getText ();
names [total] = nameField.getText ();
e_mail [total] = EmailField.getText ();
address [total] = AddressField.getText ();
phone [total] = PhoneField.getText ();
instructionsLabel.setText (lastnameField.getText () + " was modified");

}
else if (command.equals ("<<"))//previous button is pressed
{
if (total > 0)//go back if it's not zero
total--;
System.out.println ("name: " + names [total]);
//set the text field on the GUI to the current data
lastnameField.setText (lastname [total]);
nameField.setText (names [total]);
EmailField.setText (e_mail [total]);
AddressField.setText (address [total]);
PhoneField.setText (phone [total]);
instructionsLabel.setText (names [total]);
}
else if (command.equals ("Search"))//search for a specific name(last name)
{
int location = search (lastname, lastnameField.getText (), 0, 99);
if (location >= 0)//if the name is found the mehtod will return a number larger than zero which is the location
{
lastnameField.setText (lastname [location]);
nameField.setText (names [location]);
EmailField.setText (e_mail [location]);
PhoneField.setText (phone [location]);
AddressField.setText (address [location]);
instructionsLabel.setText ("found");
System.out.println (lastname [location]);
total = location;
}
else//the name wasn't found
instructionsLabel.setText ("not found");
}
else if (command.equals (">>"))//next button
{
if (total < 100)//move up if the position is smaller than a 100
total++;
System.out.println ("name" + names [total]);
//set the text fields to the current data
lastnameField.setText (lastname [total]);
nameField.setText (names [total]);
EmailField.setText (e_mail [total]);
AddressField.setText (address [total]);
PhoneField.setText (phone [total]);
instructionsLabel.setText (names [total]);
}
else if (command.equals ("Clear"))//clear the fields button
{
instructionsLabel.setText ("cleared");
lastnameField.setText ("");
nameField.setText ("");
EmailField.setText ("");
AddressField.setText ("");
PhoneField.setText ("");
}
else if (command.equals ("Read"))//read file that already exists
{
System.out.println ("At read");//reading
Scanner input = null;
try
{
input = new Scanner (readFile);//the file name was declared previously
while (input.hasNext ())//if there are more lines to go
{
lastname [total] = input.nextLine ();
names [total] = input.nextLine ();
e_mail [total] = input.nextLine ();
address [total] = input.nextLine ();
phone [total] = input.nextLine ();
total++;
System.out.println (total);
}
input.close ();//close the file stream
}
catch (FileNotFoundException e)//exception
{
System.out.println ("File not found");
}
//catch (IOException e)//exception
{
System.out.println ("Error writing to file");
}
//finally//makes sure the input stream is being closed
{
if (input != null)
input.close ();
}
}
else if (command.equals ("Write"))//writes to text files to save the data
{
System.out.println ("At write");
PrintWriter output = null;
try
{
output = new PrintWriter (writeFile);//the file name was declared previously
for (int i = 0 ; i < total ; ++i)//writes data to the file based on the current position
{
output.println (lastname [i]);
output.println (names [i]);
output.println (e_mail [i]);
output.println (address [i]);
output.println (phone [i]);
output.println (""); //make a space in between people
}
}


catch (IOException e)//exception
{
instructionsLabel.setText ("File cannot be read");
}


finally//makes sure the files is being closed so it would save
{
if (output != null)
output.close ();
}


instructionsLabel.setText ("Info has been saved to a file");
}

else if (command.equals ("Exit"))//exit the GUI
{
setVisible (false);//hide the GUI

}

}


public static int Search (String lastnames[], String searchString, int min, int max)//search method
{
if (max < min)
return -1;
else
{
int mid = (max + min) / 2;
if (searchString.compareToIgnoreCase (lastnames [mid]) < 0) //is below the midpoint
return Search (lastnames, searchString, min, mid - 1);
else if (searchString.compareToIgnoreCase (lastnames [mid]) > 0) //is above the midpoint
return Search (lastnames, searchString, mid + 1, max);
else
return mid; //of the searched item has been found
}
}


public static int search (String[] names, String name, int min, int max)//temporary search method
{

for (int i = 0 ; i < MAX ; i++)
{
if (names [i].compareTo (name) == 0)
return i;
}
return -1;
}


public static void Sort (String lastname[], String names[], String e_mail[], String address[], String phone[], int total)//insertion sort
{
String tempLS; //temporary variable for last names
String tempN; //temporary variable for first names
String tempA; //temporary variable for addresses
String tempP; //temporary variable for phones
String tempE; //temporary variable for emails
int j;
for (int i = 1 ; i < total ; i++) // going through list --> start i at 1
// to make one less comparison
{
tempLS = lastname [i]; // setting temp at start of loop
tempN = names [i];
tempA = address [i];
tempP = phone [i];
tempE = e_mail [i];

j = i - 1; // to check names before
while (j >= 0 && tempLS.compareToIgnoreCase (lastname [j]) < 0) // checks to see if
// previous names
// are greater ->
// then makes swap
{
lastname [j + 1] = lastname [j]; // if previous name is greater, moves
// greater value up the array by 1
names [j + 1] = names [j];
e_mail [j + 1] = e_mail [j];
address [j + 1] = address [j];
phone [j + 1] = phone [j];
j--; // compares to previous numbers
}
lastname [j + 1] = tempLS; // swap //+1 to make up for j=-1
names [j + 1] = tempN;
e_mail [j + 1] = tempE;
address [j + 1] = tempA;
phone [j + 1] = tempP;
}
}


// Main method
public static void main (String[] args) throws IOException
{
AddressbookFinal frame1 = new AddressbookFinal (); // Start the GUI

}
}

最佳答案

你有两个选择,要么改变

// CONSTRUCTOR - Setup your GUI here
public void HamzaTest() throws IOException {

成为类的正确构造函数,

public AddressbookFinal() throws IOException {

或者您从 main 方法调用 HamzaTest 方法

// Main method
public static void main(String[] args) throws IOException {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

AddressbookFinal frame1 = new AddressbookFinal(); // Start the GUI
frame1.HamzaTest();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}

您应该看看Providing Constructors for Your Classes了解更多详情。

对于 EventQueue.invokeLater 的原因,您应该查看 Concurrency in Swing

关于java - 编译后不显示GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34714487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com