【发布时间】: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