我正在尝试使用文本设置可见标签。但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();
}
}
});
}
}
控制器(动作主屏幕):
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);
}
});
}
}
控制器(管理员日志面板):
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);
}
答案 0 :(得分:0)
您将窗口的可见性设置为true,但是您没有将JLabel的可见性设置为true,您也可以尝试调用pack()。另一个有用的提示是确保你的组件使用布局,也许像CardLayout。
答案 1 :(得分:0)
我道歉,我无法测试你的代码。太大了。但是,您可以在更新代码之后添加以下代码段吗?
SwingUtilities.updateComponentTreeUI(component);
这里“组件”是框架下的顶层。