我有一个包含JButton的GUI类:
public class GUI {
static JFrame mainFrame;
static JLabel headerLabel;
static JLabel statusLabel;
static JPanel controlPanel;
static JButton sub;
public GUI(){
prepareGUI();
}
public static void prepareGUI(){
mainFrame = new JFrame("Service 2 - Evaluate Sensor Values");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
sub = new JButton("Send Subscribe to Service 1");
showButtonDemo();
mainFrame.setVisible(true);
}
public static void showButtonDemo(){
headerLabel.setText("Configuration Phase");
headerLabel.setFont(new Font("Serif", Font.PLAIN, 14));
sub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Monitor.createWaterSubscriptionResources();
Monitor.createTemperatureSubscriptionResources();
}
});
controlPanel.add(sub);
mainFrame.setVisible(true);
}
}
在其他课程中,我将处理一些事情,当完成该过程时,我想禁用GUI类中的按钮,例如: GUI.sub.setEnabled(false)。我怎么能这样做?
先谢谢你了!
答案 0 :(得分:1)
在您的其他类事件中,您调用Frame.getFrames()
来返回所有帧的数组。然后,您将获得GUI框架并调用GUI.sub.setEnabled(false)
答案 1 :(得分:0)
你应该在Gui类中为你的JButton设置一个getter方法,如下所示:
public static JButton getSubButton(){
return this.sub;
}
并从另一个类中禁用Button:
Gui.getsubButton().setEnabled(false);