- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个地址簿程序,并且之前已经让它工作了......但由于某种原因,我无法在 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/
我有一个无 GUI 的服务器(没有任何桌面环境或 Ubuntu 服务器的新鲜 Debian,没有 X 服务器,先验)。 我考虑安装 docker 并拉取一个基于官方 Ubuntu 的容器,并在其上添加
我正在构建一个带有临时用户名系统的简单聊天服务器。当屏幕弹出时,首先会出现一个简单的屏幕,询问您的用户名。你可以放入任何你想要的东西,这纯粹是暂时的(我也在尝试)。代码告诉程序继续,将用户名保存到代码
我想将来自其他类的图像显示到 QLabel 中,但要通知 GUI 有一个新的框架可用。我需要从非 GUI 类和非 GUI 线程发出信号。 有什么办法吗? 最佳答案 signal 可以从任何继承QObj
我正在用 Java 编写一个图形用户界面,它有一些按钮,其中一个按钮是选项。我想要它,所以当您单击选项时,它会将 gui 更改为我的选项 gui,而不是在另一个窗口中打开它。 我该怎么做? 最佳答案
标题说明了一切...我和我的 friend 正在这样做,我们不知道为什么 Ball.java 实际上没有在 gamePanel 中制作球,然后制作 GUI。顺便说一句,这是 8 球台球。这是代码: 驱
我正在使用 GUI 构建器,我想知道是否有一种简单的方法可以通过当前主窗口打开寄存器窗口(引用下面的页面)。我正在尝试通过菜单栏来执行此操作。 我一整天都在尝试,因为 GUI Builder 生成了一
我有一个程序使用了许多隐藏的 GUI 组件。例如,所有菜单项和打印机对话框/组件(仅占用至少 50 毫秒)。总的来说,我猜整个程序启动的大约 300 毫秒(或 40%)要归功于所有隐藏的东西。 我想做
我对 GUI 构建比较陌生。 我想制作一个带有按钮(我已经有了)的 GUI,用户可以按下该按钮并选择一个图像,然后动态地将该图像加载到面板中的 GUI 中。我希望每次用户浏览图像时图像都动态变化。 到
我有两年使用 Java 和 Visual Studio 进行企业应用程序编程的经验,而且我是 Python 和 wxPython 的新手。所以我的问题是:wxPython 能否为我提供足够丰富的 GU
这是我启动 mkvtoolnix-gui 时遇到的错误: mkvtoolnix-gui: symbol lookup error: mkvtoolnix-gui: undefined symbol:
我在初始屏幕上有一些最近使用的存储库,我想删除它们,因为我不再使用它们了。如何删除它们? 操作系统 = Windows 7 我查看了注册表并搜索了 git 目录,但找不到最近使用列表的存储位置。 最佳
我正在尝试在 matlab、GUI 中用户输入点作为输入和它们之间的连接。 我有 5 个 matlab 文件 - screen1.m、screen2.m、screen3.m、screen4.m、glo
我用java制作了一个客户端/服务器程序,我已经按照我想要的方式使用cmd完美地工作了,现在我正在尝试将代码的客户端转换为GUI,但是我在打印时遇到问题客户端消息并从文本字段和服务器消息读取客户端输入
我正在制作一种 CRUD 应用程序(Java GUI,MYSQL)我应该: 将数据从数据库加载到List(例如),然后将List加载到GUI 将数据从数据库加载到对象(具有 SQL 表等属性)和对象到
我正在开发一个有 5 个图形用户界面窗口的 Java 应用程序,其中一个是问候窗口或主窗口,我已经完成了所有逻辑部分的工作,我已经完成了 99.99%,唯一剩下的就是我如何以这种方式编码,当我点击一个
我目前正在开发 GUI。 我选择将我的 GUI 基于 bluej 项目 - Scribble。 当您创建 ScribbleGUI 对象时,DrawDemo 类会创建一个同时自动打开的 Canvas 。
在这里阅读了很多关于多进程、管道等的内容后,我还没有找到答案,但如果它已经存在,我深表歉意。 我有一个外围硬件,我正在尝试为其创建一个 GUI。我想让 GUI 使用来自外围设备的数据不断更新,同时仍保
我想做的是将 GUI 从一个单独文件中的类链接到另一个类。我的第一个类是一个主菜单,它将显示一些链接到另一个窗口的按钮。第二个类显示不同的窗口,但我现在遇到的问题是我不知道如何链接第一个类中的按钮来调
我的 GUI 代码中有一个奇怪的行为。如果用户在短时间内产生大量事件,则可能会发生正在运行的事件处理程序方法被另一个事件处理程序方法中断。由于一切都在同一个线程(GUI 线程)中运行,所以一切都应该按
这是一个涉及风格的问题。我正在寻找可以帮助我解决常见 GUI 设计问题 的想法。该应用程序是在 Winforms 中完成的,宁愿使用 WPF,该应用程序已经完成,但我是一个完美主义者,在与其他人合作时
我是一名优秀的程序员,十分优秀!