通过另一个类访问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;
}
# 1 楼答案
首先,这实际上是一个getter,因为它是一个值
这将是一个二传手
但是,你似乎没有这些
如果你有这种方法,那么你可以这样做
现在,这并不完美,也不能保证显示一个按钮,但“设置”一个值的想法才是重要的
或者,当类已经扩展了一个
static
方法时,您可能不想使用该方法或创建新的JFrame
。。。试试这样的然后您可以尝试
UserInterface gui = new UserInterface();
并获取按钮,并在其上设置文本# 2 楼答案
您的代码中有多个错误:
您有两个名为
processItem
的变量,一个全局变量:和一个局部变量:
第二个
processItem
变量仅存在于bookingUI()
方法中,您在getter上返回的是全局变量,该变量未初始化如果要初始化全局变量
processItem
,请从此处删除此单词JButton
:这就是返回未初始化变量的原因
您正在创建一个
JFrame
对象并扩展JFrame
。如果没有任何原因,请删除类中的extends JFrame
部分为什么您的setter方法返回值?作为惯例,它们应该是
void
(并设置一个值),而getter应该是返回内容的getter您没有正确缩进代码
您在
JFrame
中绘制所有内容之前将其可见,这将在以后给您带来麻烦您没有放置程序inside the EDT