JAVA硬件。在程序完成之前无法与创建的JFrame GUI交互
我有一些为我的程序创建的JFrame GUI;一个带有菜单并显示output.txt文件,另一个用于输入信息 在程序完全运行完成之前,我无法与它们交互或“单击” 我认为是JOption窗格(是/否)优先于JFrames,但是,我注释掉了我程序中的所有JOption窗格来测试这一点,您仍然必须等到程序完全完成后才能访问JFramesJAVA硬件。在程序完成之前无法与创建的JFrame GUI交互,java,user-interface,jframe,Java,User Interface,Jframe,我有一些为我的程序创建的JFrame GUI;一个带有菜单并显示output.txt文件,另一个用于输入信息 在程序完全运行完成之前,我无法与它们交互或“单击” 我认为是JOption窗格(是/否)优先于JFrames,但是,我注释掉了我程序中的所有JOption窗格来测试这一点,您仍然必须等到程序完全完成后才能访问JFrames public class TheList extends JPanel { public TheList(){ } public static void
public class TheList extends JPanel {
public TheList(){
}
public static void List(){
ArrayList<Food> didntBuy = new ArrayList<Food>();
ArrayList<Food> arrayList = new ArrayList<Food>();// creating an ArrayList for Food objects.
ArrayList<Food> sortLow = new ArrayList<Food>();// creating ArrayLists for high, medium, and low priorities.
ArrayList<Food> sortMed = new ArrayList<Food>();
ArrayList<Food> sortHigh = new ArrayList<Food>();
final Scanner keyboard = new Scanner(System.in);
//System.out.println("\nEnter your name");
String yourName=JOptionPane.showInputDialog("\nEnter your name for the list");
JOptionPane.showMessageDialog(null, "\nYou entered: "+ yourName);
boolean checkLetterFirstName;
checkLetterFirstName = CheckInput.CheckForNumber(yourName);// Used this class from another student.
if(checkLetterFirstName == true)
{ JOptionPane.showMessageDialog(null, "I'm sorry but no numbers or spaces can be in your first name.", yourName, JOptionPane.ERROR_MESSAGE);
//System.out.println("Enter you name");
// yourName=keyboard.nextLine();
yourName=JOptionPane.showInputDialog("\nEnter your name");
checkLetterFirstName = CheckInput.CheckForNumber(yourName);}
try{
//System.out.println("Enter funds");
//Food.currentCash=keyboard.nextDouble();
String fn=JOptionPane.showInputDialog("Enter funds for the list");
Food.currentCash= Double.parseDouble(fn);// converting String to a Double.
if(Food.getCurrentCash()<0)
throw new Exception("Exception: no negative numbers");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, (e.getMessage()), null, JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "Enter postive numbers only");
String fn=JOptionPane.showInputDialog("Enter funds for the list");
Food.currentCash= Double.parseDouble(fn);
}
Food.currentCash1=Food.currentCash;
NumberFormat formatter=NumberFormat.getCurrencyInstance();
System.out.println("Current Cash");
System.out.println(formatter.format(Food.currentCash));// used a currency formatter to covert to dollars.
final Scanner keyboard6 = new Scanner(System.in);
for (int i = 0; i < 50; i++) {
//An abstract class cannot be instantiated
int answer=
JOptionPane.showConfirmDialog(null, "Do you want to add a new apple(s) object to list?", "Click Yes or No:", JOptionPane.YES_NO_OPTION);
if (answer== JOptionPane.YES_OPTION)
{
JFrame frame = new JFrame(); // creating a new JFrame object called "frame".
Container pane = null;
// SpringLayout layout = new SpringLayout();
BoxLayout layout= new BoxLayout(pane, BoxLayout.Y_AXIS);
JTextArea text0 =new JTextArea("Do you want to add an apple(s) to list?"); //creating a new TextArea object called "text".
frame.setSize(600, 600); //setting width and hight of window frame.
JButton yes = new JButton("Yes");
JButton no = new JButton("No");
JButton enter= new JButton("Enter");
JButton enter2= new JButton("Enter");
JButton enter3= new JButton("Enter");
JButton enter4= new JButton("Enter");
yes.setAlignmentX(Component.CENTER_ALIGNMENT);
no.setAlignmentX(Component.CENTER_ALIGNMENT);
final JPanel panel = new JPanel(); // creating 4 new panels
final JPanel panel2 = new JPanel();
final JPanel panel3 = new JPanel();
final JPanel panel4 = new JPanel();
JLabel label = new JLabel("What kind of apple?"); // creating 4 labels per panel.
JLabel label2 = new JLabel("Enter priority or -1 to exit program.");
JLabel label3 = new JLabel ("Enter price you want to pay.");
JLabel label4 = new JLabel ("Enter quanity.");
JTextField text = new JTextField(10);
JTextField text2 = new JTextField(10);
JTextField text3 = new JTextField(10);
JTextField text4 = new JTextField(10);
panel.add(label);// adding labels to the panel
panel2.add(label2);
panel3.add(label3);
panel4.add(label4);
panel.add(text); // adding text fields to each panel to enter information.
panel2.add(text2);
panel3.add(text3);
panel4.add(text4);
panel.add(enter);
panel2.add(enter2);
panel3.add(enter3);
panel4.add(enter4);
panel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel2.setAlignmentX(Component.CENTER_ALIGNMENT);
panel3.setAlignmentX(Component.CENTER_ALIGNMENT);
panel4.setAlignmentX(Component.CENTER_ALIGNMENT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// ends program if closed.
frame.setAlwaysOnTop(true); // on top of other windows.
frame.setTitle("Apple");
//Image img = ImageIO.read(new File("C:\\Users\\joey zasa\\workspace\\homework4\\src\\homework4\\apple-full2.jpg"));
frame.add(text0);
frame.add(yes);
frame.add(no);
frame.add(panel);
frame.getContentPane().add(panel2);
frame.getContentPane().add(panel3);
frame.getContentPane().add(panel4);
addComponentsToPane(frame.getContentPane());
final Fruit appleObject =new apple();
yes.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
panel.setVisible(true);
panel2.setVisible(true);
panel3.setVisible(true);
panel4.setVisible(true);
}
});
no.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
panel.setVisible(false);
panel2.setVisible(false);
panel3.setVisible(false);
panel4.setVisible(false);
}
});
enter.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
appleObject.setName(keyboard6.nextLine());
}
});
enter2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
appleObject.setPriority(keyboard.nextInt());
}
});
enter3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
appleObject.setPrice(keyboard.nextDouble());
}
});
enter3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try{
System.out.println("Enter quanity");
appleObject.setQuanity(keyboard.nextInt());
if(appleObject.getQuanity()<0)
throw new negativeException();
}
catch (negativeException x) {
System.out.println(x.getMessage());
System.out.println("Enter quanity again");
appleObject.setQuanity(keyboard.nextInt());
}
}
});
frame.pack();
frame.setVisible(true);// setting the visibility of the frame to true.
for (int z=0; z<appleObject.getQuanity(); z++){
arrayList.add (appleObject);
i++;
}}
if( appleObject.getPriority()==1||appleObject.getPriority()==2){
sortHigh.add(appleObject);}// adding only one instance of an Object to one of 3 Arraylists
if( appleObject.getPriority()==3||appleObject.getPriority()==4){
sortMed.add(appleObject);}
if( appleObject.getPriority()==5){
sortLow.add(appleObject);}
}
System.out.println("\n"+yourName+"'s list:");
int listSize=arrayList.size();
for(int position=0; position <listSize; position++){
System.out.println(arrayList.get(position));
}
int y = arrayList.size();
System.out.println("\nSize of list: "+y+"\n" );
// Sorting with low, medium,100 high, needs. Priorities 1-2=high, 3-4=medium, 5= low.
System.out.println("\nHigh priority items :\n");
int listHigh=sortHigh.size();
for(int position=0; position <listHigh; position++){
System.out.println(sortHigh.get(position));
}
System.out.println("\nMedium priority items :\n");
int listMed=sortMed.size();
for(int position=0; position <listMed; position++){
System.out.println(sortMed.get(position));
}
System.out.println("\nLow priority items :\n");
int listLow=sortLow.size();
for(int position=0; position <listLow; position++){
System.out.println(sortLow.get(position));
}
System.out.println("\n\nCash left for virtual list: ");
System.out.println(formatter.format(Food.currentCash));
System.out.println("\n\nCash left after real world prices: ");
System.out.println(formatter.format(Food.currentCash1));
String nameofFile="out.txt";
PrintWriter outputStream=null;
try{
outputStream=new PrintWriter(nameofFile);
}
catch(FileNotFoundException e)
{
System.out.println("Error opening file");
System.exit(0);
}
{
outputStream.println("\n"+yourName+"'s list of purchased Items:");
int listSize4=arrayList.size();
for(int position=0; position <listSize4; position++){
outputStream.println(arrayList.get(position));
}
outputStream.println("\nHigh priority items :\n");
int listHigh1=sortHigh.size();
for(int position=0; position <listHigh; position++){
outputStream.println(sortHigh.get(position));
}
outputStream.println("\nMedium priority items :\n");
int listMed1=sortMed.size();
for(int position=0; position <listMed1; position++){
outputStream.println(sortMed.get(position));
}
outputStream.println("\nLow priority items :\n");
int listLow1=sortLow.size();
for(int position=0; position <listLow; position++){
outputStream.println(sortLow.get(position));
}
outputStream.println("\nItems you did not buy, but you may need: \n");
int listDidntBuy=didntBuy.size();
for(int position=0; position <listDidntBuy; position++){
outputStream.println(didntBuy.get(position));
}
outputStream.close();
}
}
}
private static void addComponentsToPane(Container pane) {
{
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
}
}
}
公共类列表扩展了JPanel{
公共列表(){
}
公共静态无效列表(){
ArrayList didntBuy=新ArrayList();
ArrayList ArrayList=新建ArrayList();//为食物对象创建ArrayList。
ArrayList sortLow=新建ArrayList();//为高、中、低优先级创建ArrayList。
ArrayList sortMed=新建ArrayList();
ArrayList sorthough=新的ArrayList();
最终扫描仪键盘=新扫描仪(System.in);
//System.out.println(“\n输入您的姓名”);
String yourName=JOptionPane.showInputDialog(“\n在列表中输入您的姓名”);
JOptionPane.showMessageDialog(空,“\n您输入:“+yourName”);
布尔checkLetterFirstName;
checkLetterFirstName=CheckInput.CheckForNumber(yourName);//从另一个学生那里使用了这个类。
if(checkLetterFirstName==true)
{JOptionPane.showMessageDialog(null,“对不起,您的名字中不能有数字或空格。”,yourName,JOptionPane.ERROR_MESSAGE);
//System.out.println(“输入您的姓名”);
//yourName=keyboard.nextLine();
yourName=JOptionPane.showInputDialog(“\n输入您的姓名”);
checkLetterFirstName=CheckInput.CheckForNumber(您的姓名);}
试一试{
//System.out.println(“输入资金”);
//Food.currentCash=keyboard.nextDouble();
字符串fn=JOptionPane.showInputDialog(“为列表输入资金”);
Food.currentCash=Double.parseDouble(fn);//将字符串转换为Double。
如果(Food.getCurrentCash()您似乎在用某种长时间运行的进程或无限循环阻塞事件调度线程
查看更多详细信息。“如果需要,我会发布代码。”这可能是明智的……更好的方法是演示您的问题。谢谢,我最终重新设计了我的程序。我从菜单GUI开始,并从那里运行所有内容。