有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

通过另一个类访问JavaSwing组件?

我试图从另一个类访问JavaSwing组件。例如,我需要在特定操作完成后更改按钮上的文本。我正在尝试使用“getter”和“setter”来执行操作(在代码部分的底部)现在我只想“设置”文本

我有另一个类调用“set”方法并尝试设置所选按钮的文本。这是主课。代码行是“gui.setProcessItemBtn().setText(“某些文本”);”抛出:

Exception in thread "main" java.lang.NullPointerException

我相信这意味着没有任何东西可以设置文本。我是否缺少将setter方法链接到实际gui组件的内容

主类

package book;
import book.UserInterface;

/**
 *
 * @author KJ4CC
 */
public class Book   {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       UserInterface gui = new UserInterface();
       gui.startUI();
       gui.setProcessItemBtn().setText("Some Text");
    }

}

用户界面类

    public class UserInterface extends JFrame {
        private JButton processItem;
        private JButton confirmItem;
        private JButton viewOrder;
        private JButton finishOrder;
        private JButton newOrder;
        private JButton exit;

        public void startUI(){

            UserInterface gui = new UserInterface();
            gui.bookingUI();
        }

        public static void  bookingUI(){
            //sets windows, and pane in the UI 
            JFrame frame = new JFrame("Ye old Book stoppe");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel toppane = new JPanel(new GridBagLayout());
            JPanel bottomPane = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            frame.setSize(800, 300);
            frame.setVisible(true);
            //adds labels  to the window
        //----------------------------------------------------------BUTTOM PANE-------------------------
        //setting up buttons to be placed onto the bottompanel 
        JButton processItem = new JButton("Process Item");
        JButton confirmItem = new JButton("Confirm Item");
        JButton viewOrder = new JButton("View Order");
        JButton finishOrder = new JButton("Finish Order ");
        JButton newOrder = new JButton("New Order");
        JButton exit = new JButton("Exit");
        //adding the buttons to the pane.---------------------------------------------------------------
        GridBagConstraints b = new GridBagConstraints();
        b.insets = new Insets(5,5,5,5);
        b.ipadx = 10;
        b.ipady = 10;
        b.gridx = 1;
        b.gridy = 0;
        bottomPane.add(processItem, b);
        b.gridx = 2;
        b.gridy = 0;
        bottomPane.add(confirmItem,b);
        confirmItem.setEnabled(false);
        b.gridx = 3;
        b.gridy = 0;
        bottomPane.add(viewOrder, b);
        viewOrder.setEnabled(false);
        b.gridx = 4;
        b.gridy = 0;
        bottomPane.add(finishOrder,b);
        finishOrder.setEnabled(false);
        b.gridx = 5;
        b.gridy = 0;
        bottomPane.add(newOrder,b);
        b.gridx = 6;
        b.gridy = 0;
        bottomPane.add(exit, b);
        bottomPane.setBackground(Color.BLUE);
        frame.add(bottomPane,BorderLayout.SOUTH);
        frame.setSize(810, 310);


        }


    //Creating getters and setters to change the text for the buttons and labels. 
    public JButton setProcessItemBtn(){
         return processItem;
     }
    public JButton setConfirmItemBtn(){
         return confirmItem;
     } 
    public JButton setViewOrderbtn(){
        return viewOrder;
     }
    public JButton setFinishOrderBtn(){
         return finishOrder;
     }
    public JButton setNewOrderBtn(){
         return newOrder;
     }
    public JButton setsetExitBtn(){
         return exit;
     }

共 (2) 个答案

  1. # 1 楼答案

    首先,这实际上是一个getter,因为它是一个值

    public JButton setProcessItemBtn(){
         return processItem;
     }
    

    这将是一个二传手

    public void setProcessItemBtn(JButton btn){
         processItem = btn;
     }
    

    但是,你似乎没有这些

    如果你有这种方法,那么你可以这样做

    public static void main(String[] args) {
       UserInterface gui = new UserInterface();
       gui.setProcessItemBtn(new JButton("Some Text"));
       gui.startUI();     
    }
    

    现在,这并不完美,也不能保证显示一个按钮,但“设置”一个值的想法才是重要的


    或者,当类已经扩展了一个static方法时,您可能不想使用该方法或创建新的JFrame。。。试试这样的

    public class UserInterface extends JFrame {
    
        // Button variables
    
        public UserInterface(){
            bookingUI();
        }
    
        public void bookingUI(){
            //sets windows, and pane in the UI 
            setTitle("Ye old Book stoppe");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel toppane = new JPanel(new GridBagLayout());
            JPanel bottomPane = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            setSize(800, 300);
            setVisible(true);
    
            //setting up buttons to be placed onto the bottompanel 
            processItem = new JButton("Process Item");
            confirmItem = new JButton("Confirm Item");
            viewOrder = new JButton("View Order");
            finishOrder = new JButton("Finish Order ");
            newOrder = new JButton("New Order");
            exit = new JButton("Exit");
    
            // etc...
        }
    

    然后您可以尝试UserInterface gui = new UserInterface();并获取按钮,并在其上设置文本

  2. # 2 楼答案

    您的代码中有多个错误:

    1. NPE发生的原因如下:

    您有两个名为processItem的变量,一个全局变量:

    private JButton processItem;
    

    和一个局部变量:

    JButton processItem = new JButton("Process Item");
    

    第二个processItem变量仅存在于bookingUI()方法中,您在getter上返回的是全局变量,该变量未初始化

    如果要初始化全局变量processItem,请从此处删除此单词JButton

    JButton processItem = new JButton("Process Item");
    

    这就是返回未初始化变量的原因

    1. 您正在创建一个JFrame对象并扩展JFrame。如果没有任何原因,请删除类中的extends JFrame部分

    2. 为什么您的setter方法返回值?作为惯例,它们应该是void(并设置一个值),而getter应该是返回内容的getter

    3. 您没有正确缩进代码

    4. 您在JFrame中绘制所有内容之前将其可见,这将在以后给您带来麻烦

    5. 您没有放置程序inside the EDT