通过另一个类访问Java Swing组件?

时间:2016-08-29 21:48:03

标签: java swing nullpointerexception

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

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

  

线程“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 个答案:

答案 0 :(得分:4)

首先,这实际上是一个吸气剂,因为它return是一个值。

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();并获取按钮,并在其上设置文字

答案 1 :(得分:3)

您的代码中存在多个错误:

  1. NPE因此而发生:
  2. 您有两个名为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(并设置一个值), getters 应该是返回的东西。

    3. 您没有正确缩进代码

    4. 您正在使JFrame显示之前您已在其中绘制了所有内容,这会让您以后遇到麻烦

    5. 您没有放置您的计划inside the EDT