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

通过另一个类访问JavaSwing组件?,java,swing,nullpointerexception,Java,Swing,Nullpointerexception,我试图从另一个类访问JavaSwing组件。例如,我需要在特定操作完成后更改按钮上的文本。我试图使用getter和setter在代码部分的底部执行操作。现在我只想设置文本 我有另一个类调用set方法并尝试设置所选按钮的文本。这是主课。代码行,即gui.setProcessItemBtn.setTextSome文本;抛出: 线程主java.lang.NullPointerException中出现异常 我相信这意味着没有任何东西可以设置文本。我是否缺少将setter方法链接到实际gui组件的内容 主

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

我有另一个类调用set方法并尝试设置所选按钮的文本。这是主课。代码行,即gui.setProcessItemBtn.setTextSome文本;抛出:

线程主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;
     }

您的代码中有多个错误:

NPE发生的原因如下: 您有两个名为processItem的变量,一个全局变量:

private JButton processItem;
JButton processItem = new JButton("Process Item");
和一个局部变量:

private JButton processItem;
JButton processItem = new JButton("Process Item");
第二个processItem变量只存在于bookingUI方法中,您在getter上返回的是全局变量,它没有初始化

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

JButton processItem = new JButton("Process Item");
这就是返回未初始化变量的原因

如果要创建JFrame对象并毫无理由地扩展JFrame,请删除类中的ExtendesJFrame部分

为什么setter方法返回值?作为惯例,它们应该是空的,并设置一个值,而getter应该是返回某些内容的getter

您没有正确缩进代码

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

你没有把你的程序


首先,这实际上是一个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();     
}
现在,这并不完美,也不能保证显示一个按钮,但设置一个值的想法才是重要的

或者,当类已经扩展了一个JFrame时,您可能不想使用静态方法或创建新的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=newuserinterface;获取按钮,并设置按钮上的文本

private JButton processItem的值;从未分配过。。。提示:jbuttonprocessitem=新建jbuttonprocessitem;可能没有做你认为它做的事情是的,我认为这就是导致异常的原因。JButton processItem=新的JButton流程项;创建JButton的实例是否正确?然后引号中的文本设置文本。是否有方法将按钮分配给私有JButton processItem?