我正在尝试用文本设置可见标签。但是GUI不会更新它。你能看一下代码并给我一些建议吗?我有几个控制器类,下面我介绍其中的两个。动作主屏幕负责检测点击和管理日志面板应该检查数据是否正确和更新视图。
主:public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Model model = new Model();
MainScreen window = new MainScreen(model);
ActionMainScreen actionMainScreen = new ActionMainScreen(
model, window);
OpenClose openClose = new OpenClose();
actionMainScreen.connectToServer();
window.getFrame().setVisible(true); // GUI
actionMainScreen.contol(); // GUI controllers
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
控制器(Action Main Screen):
public class ActionMainScreen {
private MainScreen viewMain;
private OpenClose openClose = new OpenClose();
private ConnectionNetwork connectionNetwork = new ConnectionNetwork();
private UpdateData updateData = new UpdateData();
private Model model = new Model();
private AdminLogPanel admPanel = new AdminLogPanel();
public ActionMainScreen(Model model, MainScreen window) {
this.viewMain = window;
}
public void connectToServer() {
try {
connectionNetwork.connectToServer();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (java.net.ConnectException e1) {
e1.printStackTrace();
System.out.println("ConnectException");
} catch (IOException e1) {
e1.printStackTrace();
}
}
public void contol() {
openClose.checkByTheFirstTime();
updateData.changeText();
viewMain.getBtnLogIn().addActionListener(new ActionListener() {
String login;
String password;
@Override
public void actionPerformed(ActionEvent e) {
login = viewMain.getTextFieldLogin().getText();
password = new String(viewMain.getPasswordField().getPassword());
admPanel.checkIfAdmin(login, password);
}
});
}
}
控制器(Admin Log Panel):
public class AdminLogPanel {
Model model = new Model();
MainScreen mainScreen = new MainScreen(model);
public void checkIfAdmin(String login, String password) {
if (ModelAdmin.getLogin().equals(login)
&& ModelAdmin.getPassword().equals(password)) {
System.out.println("HURA!");
mainScreen.getLblLoginOrPassword().setVisible(true);
JOptionPane.showMessageDialog(null, "OK");
} else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println(SwingUtilities.isEventDispatchThread()); // true
mainScreen.getLblLoginOrPassword().setText("abc"); // correct text in console
System.out.println(mainScreen.getLblLoginOrPassword().getText());
mainScreen.getLblLoginOrPassword().setVisible(true); // nothing happened
mainScreen.getLblLoginOrPassword().repaint();
mainScreen.getPanel_1().repaint();
}
});
}
}
}
视图:
public class MainScreen {
public MainScreen(Model model) {
initialize();
}
private JButton btnLogIn;
private JLabel lblLoginOrPassword;
public JLabel getLblLoginOrPassword() {
return lblLoginOrPassword;
}
public void setLblLoginOrPassword(JLabel lblLoginOrPassword) {
this.lblLoginOrPassword = lblLoginOrPassword;
}
public JButton getBtnLogIn() {
return btnLogIn;
}
(....)
lblLoginOrPassword = new JLabel("Login or Password is incorrect!");
lblLoginOrPassword.setFont(new Font("Tahoma", Font.BOLD, 17));
lblLoginOrPassword.setVisible(false);
}
您将窗口的可见性设置为true,但您没有将JLabel的可见性设置为true,您也可以尝试调用pack()。另一个有用的技巧是确保你的组件使用布局,也许像CardLayout。
对不起,我无法测试你的代码。它太大了。但是,您可以将以下代码片段放在更新代码之后并查看吗?
SwingUtilities.updateComponentTreeUI(component);
这里的"component"是框架下的顶层