我为我的程序创建了一些JFrame GUI;一个带有菜单并显示output.txt文件,另一个用于输入信息。
在程序完全运行之前,我无法进行交互或“点击”它们。
我认为是JOption窗格(是/否)优先于JFrames,但是,我在程序中注释掉所有JOption窗格来测试它,你仍然需要等到程序完全完成到访问JFrame。
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));
}
}
}
我的程序有太多的课程为你提供了一个小程序,抱歉。这是我正在创建其中一个GUI的类。它的小GUI叫做“apple”。图片中苹果背后的GUI来自另一个类,并在Main中打开。我还编辑了剩余的“杂货清单对象”,仅包括“苹果”。