【问题标题】:Accessing Java Swing Components Through Another Class? [duplicate]通过另一个类访问 Java Swing 组件? [复制]
【发布时间】:2016-08-29 21:48:03
【问题描述】:

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

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

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

【问题讨论】:

  • private JButton processItem; 的值从未被赋值...
  • 是的,我认为这就是导致异常的原因。 JButton processItem = new JButton("Process Item");创建 JButton 的实例正确吗?然后引用中的文本设置文本。有没有办法将按钮分配给私有 JButton processItem?

标签: java swing nullpointerexception


【解决方案1】:

首先,这实际上是一个吸气剂,因为它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(); 获取按钮,然后在上面设置文字

【讨论】:

    【解决方案2】:

    您的代码中有多个错误:

    1. NPE 由于以下原因而发生:

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

    private JButton processItem;
    

    和一个局部变量:

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

    第二个processItem 变量只存在于bookingUI() 方法中,而您在getter 上返回的变量是全局变量,它没有被初始化!

    如果你想初始化 global 变量 processItem 从这里删除这个词 JButton

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

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

    1. 您正在创建一个JFrame 对象并无缘无故地扩展JFrame,请删除您班级中的extends JFrame 部分。

    2. 为什么你的 setter 方法会返回一个值?按照惯例,它们应该是 void(并设置一个值),而 getters 应该是返回的东西。

    3. 您的代码缩进不正确

    4. 你让你的JFrame可见之前你已经画了它里面的所有东西,这会给你以后带来麻烦

    5. 你没有放置你的程序inside the EDT

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 2014-04-06
      • 1970-01-01
      相关资源
      最近更新 更多