使用按钮改善UI(java)

使用按钮改善UI(java),java,swing,jbutton,Java,Swing,Jbutton,我是一名新的Java程序员,我有一些代码试图通过添加按钮来改进UI。基本上,现在,代码接受一个数组列表,并允许用户使用它做很多事情。用户可以向数组列表中添加项,从数组列表中删除项,在数组列表中的某个索引处设置项,打印数组列表中的项,并在数组中的某个索引处获取项。它允许用户使用基本数组列表函数(.add、.remove等)以一些基本方式与数组列表交互 现在,用户必须输入一个数字才能执行其中一项操作(即,输入1以打印数组中的元素),但我正在尝试这样做,以便函数可以与按钮一起工作。在过去的一个多小时里

我是一名新的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.");
            }  */

        }  
    }
import java.io.*;
导入java.util.ArrayList;
导入java.util.Scanner;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
课堂测试
{
公共静态void main(字符串arg[])引发IOException{
//*主变量声明(带有任何初始化)
//
ArrayList=新建ArrayList();
布尔continueThisApp=true;
int userInput_MenuChoice;
扫描仪=新的扫描仪(System.in);
//*主代码
//        
while(continueThisApp)
{
//*选择菜单
//
JFrame=新JFrame();
JButton b1=新JButton();
JButton b2=新JButton();
JButton b3=新JButton();
JButton b4=新JButton();
JButton b5=新JButton();
JButton b6=新JButton();
b1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
打印(项目列表);
}
});
b2.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
添加(表中的项目);
}
});
b3.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
移除(禁止的项目);
}
});
b4.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
获取(项目列表);
}
});
b5.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
成套设备(设备/设备);
}
});
b6.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
System.out.println(“感谢您尝试我们的应用程序:”);
}
});
框架。设置尺寸(10001000);
//b1
b1.设置可见(真);
b1.setText(“打印元素”);
frame.setLayout(新的FlowLayout());
框架。添加(b1);
//b2
b2.setVisible(真);
b2.setText(“添加元素”);
框架。添加(b2);
//b3
b3.setVisible(真);
b3.setText(“删除元素”);
框架。添加(b3);
//b4
b4.设置可见(真);
b4.setText(“获取元素”);
框架。添加(b4);
//b5
b5.设置可见(真);
b5.setText(“集合元素”);
框架。添加(b5);
//b6
b6.setVisible(真);
b6.setText(“退出”);
帧。添加(b6);
//请注意,这一部分(我们以前的做法)已被注释掉
/*if(userInput\u MenuChoice==1){
打印项目列表(项目列表);
}
else if(userInput\u MenuChoice==2){
扫描仪输入至地址(项目列表);
}
else if(userInput\u MenuChoice==3){
扫描器输入至拆卸位置(观察项目);
}
else if(userInput\u MenuChoice==4){
扫描仪输入至第个月(观察项目);
}            
else if(userInput\u MenuChoice==5){
扫描仪输入至设置时间(项目观察);
}
else if(userInput\u MenuChoice==9){
continueThisApp_Bool=false;
System.out.println(“感谢您尝试我们的应用程序:”);
}
否则{
System.out.println(“***菜单选项无效。请重试”);
}  */
}  
}

动作侦听器中的函数正常工作,但按钮本身没有为我显示。我想问是否有人知道按钮没有显示的原因。我确信我的代码中存在其他问题,但如果我知道按钮没有显示的原因,我可能可以修复这些问题。如果这有帮助,我目前正在使用BlueJ

tl;dr-我在代码中实现了按钮,但当我执行