首页 文章

使用按钮使UI更好(java)

提问于
浏览
0

我是一名新的Java程序员,我有一些代码,我试图通过添加按钮来改进UI . 基本上,现在,代码采用数组列表,并允许用户使用它做很多事情 . 用户可以将项添加到数组列表,从数组列表中删除项,在数组列表中的某个索引处设置项,在数组列表中打印项,并在数组的某个索引处获取项 . 它几乎允许用户使用基本数组列表函数(.add,.remove等)以一些基本方式与数组列表进行交互 .

现在,用户必须输入一个数字才能执行其中一项操作(即输入1以打印数组中的元素),但我正在尝试使用它来使函数与按钮一起使用 . 我一直在查看按钮和动作监听器在过去一小时左右的工作情况,我认为我对它有很好的把握,但我遇到了一个问题 . 我运行我的代码,我的按钮不会出现,所以我很困惑为什么会发生这种情况 .

这是我的代码中最重要的部分:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test                                                                                                                                                                                                                                                                                                                                     
{
    public static void main (String arg[]) throws IOException {

        // * Main Variable Declaration (with any Initialization)
        //
        ArrayList<String> List = new ArrayList<String>();        
        boolean            continueThisApp = true;       
        int                userInput_MenuChoice;                    
        Scanner            scanner = new Scanner(System.in);                 

        // * Main Code
        //        
        while( continueThisApp )
        {
            // * Menu of Choices
            //
            JFrame frame = new JFrame();
            JButton b1 = new JButton();
            JButton b2 = new JButton();
            JButton b3 = new JButton();
            JButton b4 = new JButton();
            JButton b5 = new JButton();
            JButton b6 = new JButton();           
            b1.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    print(items_ObsInArrLst);             
                }
            });
            b2.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    add(items_ObsInArrLst);             
                }
            });
            b3.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    remove(items_ObsInArrLst);             
                }
            });
            b4.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    get(items_ObsInArrLst);             
                }
            });
            b5.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    set(items_ObsInArrLst);             
                }
            });
            b6.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    System.out.println("*** Thanks for Trying our App. :)");
                }
            });
            frame.setSize(1000, 1000);
            //b1
            b1.setVisible(true);
            b1.setText("Print elements");
            frame.setLayout(new FlowLayout());
            frame.add(b1);
            //b2
            b2.setVisible(true);
            b2.setText("Add elements");
            frame.add(b2);
            //b3
            b3.setVisible(true);
            b3.setText("Remove elements");
            frame.add(b3);
            //b4
            b4.setVisible(true);
            b4.setText("Get elements");
            frame.add(b4);
            //b5
            b5.setVisible(true);
            b5.setText("Set elements");
            frame.add(b5);
            //b6
            b6.setVisible(true);
            b6.setText("Quit");
            frame.add(b6);

            // NOTE THAT THIS PART (THE OLD WAY WE DID IT) IS COMMENTED OUT
            /*if( userInput_MenuChoice == 1 ){
                print_Items_ObsInArrLst_Mth(items_ObsInArrLst);             
            }
            else if( userInput_MenuChoice == 2 ){
                scannerInputTo_AddLst_Mth (items_ObsInArrLst);             
            }
            else if( userInput_MenuChoice == 3 ){
                scannerInputTo_RemoveLst_Mth (items_ObsInArrLst);             
            }
            else if( userInput_MenuChoice == 4 ){
                scannerInputTo_GetLst_Mth (items_ObsInArrLst);             
            }            
            else if( userInput_MenuChoice == 5 ){
                scannerInputTo_SetLst_Mth (items_ObsInArrLst);             
            }
            else if( userInput_MenuChoice == 9 ){
                continueThisApp_Bool = false;
                System.out.println("*** Thanks for Trying our App. :)");
            }
            else{
                System.out.println("*** Invalid Menu Choice. Retry.");
            }  */

        }  
    }

动作侦听器内部的功能可以正常工作,但按钮本身并不适合我 . 我问是否有人知道按钮没有出现的原因 . 我确定我的代码还有其他问题,但如果我知道为什么按钮没有显示,我可以解决这些问题 . 如果这有帮助,我目前正在使用BlueJ .

tl; dr - 我在我的代码中实现了按钮,但是当我运行代码时它们没有出现 .

1 回答

  • 1

    现在,用户必须输入一个数字才能执行其中一项操作(即输入1以打印数组中的元素),但我正在尝试使用它来使函数与按钮一起使用 .

    GUI应用程序与基于文本的应用程序不同 . 首先,您不使用 while loop 来监听用户输入 .

    基本上,现在,代码采用数组列表

    在GUI应用程序中,您也无法直接使用ArrayLists来显示数据 . 相反,您使用Swing组件,该组件将使用 Model 来保存数据 . 所以"model"取代了ArrayList . 这就是对模型进行的所有添加/删除操作 .

    因此,我建议您从一开始就重新设计应用程序的结构,使用GUI,而不是尝试将现有代码放入GUI结构中 .

    首先阅读How to Use Lists上Swing教程中的部分 . ListDemo 示例显示了创建GUI所需的一切,该GUI使用按钮在 JListListModel 中添加/删除项目 .

相关问题