- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个由许多不同的 JPanel 组成的 CardLayout,我可以随意显示它们。该GUI链接到后端,GUI的目的是允许用户操作存储在后端的数据。
问题是,当用户在后端更改某些数据时,我的 JLabels 没有反射(reflect)该数据的更改。我知道这是因为 JLabels 被初始化了一次,当我更新它们初始化的变量时,我必须再次调用 setText 才能让它们体验到更改。
示例:这是我的 JLabel 的初始化方式
CurrentBAName = new javax.swing.JLabel();
CurrentBAName.setText(GUI.currentBankAccount.getAccountName());
问题是,当我更改 GUI.currentBankAccount.getAccountName()
的值时,JLabel 上的文本不会更新。我到处都有这样的 JLabel,每次用户进行任何类型的更改时,都必须对所有这些 JLabel 进行手动调用 setText,这将是一件很痛苦的事情。有什么方法可以让 JLabel 在该变量的值发生变化时自动更新吗?
也许我可以实现某种观察者模式?我该如何使用 JLabels 来做到这一点?
或者也许我可以使用 focusGained 事件,以便每次切换到面板时, focusGained 事件都会触发,以便我可以更新内容。但我不确定如何做到这一点,而且我不确定焦点如何与 CardLayout 一起使用。也许只是每次我用 layout.show(..)
显示面板时调用的方法。我将如何实现这个?
有更好的办法吗?
更新:示例代码
GUI.java 是我的主要 GUI 类,将包含一个 JPanel,我的所有页面都将添加到其中并随意显示。
import javax.swing.*;
public class GUI extends javax.swing.JFrame
{
public static String item;
private javax.swing.JPanel MainPanel;
public GUI()
{
// creates a JPanel called MainPanel which will hold the cards
// in my CardLayout
initComponents();
}
private void initComponents()
{
MainPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
MainPanel.setBackground(new java.awt.Color(198, 118, 38));
MainPanel.setMaximumSize(new java.awt.Dimension(1000, 1000));
MainPanel.setName(""); // NOI18N
MainPanel.setPreferredSize(new java.awt.Dimension(1024, 768));
MainPanel.setLayout(new java.awt.CardLayout());
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
setLocationRelativeTo(null);
}
// Grab the panel that was created in initComponents() so that I can add
// other panels to it
public JPanel getMainPanel()
{
return MainPanel;
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
GUI mainGUI = new GUI();
// The JLabel in DisplayPanel.java will initially say root
item = "root";
JPanel cardHolder = mainGUI.getMainPanel();
//This panel contains a button which will update the name of
//the currentUserAccount
MainMenuPanel mainMenu = new MainMenuPanel(cardHolder);
cardHolder.add(mainMenu, "MainMenu");
//This panel holds a JLabel that I want to have updated automatically
//when the user presses the button on MainMenuPanel
DisplayPanel display = new DisplayPanel(cardHolder);
cardHolder.add(display, "Display");
mainGUI.pack();
mainGUI.setVisible(true);
}
});
}
}
DisplayPanel.java 将是一个带有我想要更新的 JLabel 的 JPanel
import java.awt.*;
import javax.swing.*;
public class DisplayPanel extends javax.swing.JPanel
{
private JPanel MainPanel;
public DisplayPanel(JPanel MainPanel)
{
this.MainPanel = MainPanel;
initComponents(); // Creates my panel with the JLabels/buttons etc
}
@SuppressWarnings("unchecked")
private void initComponents()
{
jLabel1 = new javax.swing.JLabel();
GoToMainMenuButton = new javax.swing.JButton();
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel1.setText(GUI.item); // sets the JLabel to the GUI.item variable
GoToMainMenuButton.setText("Go to MainMenuPanel");
GoToMainMenuButton.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent evt)
{
GoToMainMenuButtonMouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(269, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(GoToMainMenuButton)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(266, 266, 266))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(168, 168, 168)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(83, 83, 83)
.addComponent(GoToMainMenuButton)
.addContainerGap(116, Short.MAX_VALUE))
);
}
private void GoToMainMenuButtonMouseClicked(java.awt.event.MouseEvent evt)
{
// go back to the MainPanel
CardLayout layout = (CardLayout) (MainPanel.getLayout());
layout.show(MainPanel, "MainMenu");
}
private javax.swing.JButton GoToMainMenuButton;
private javax.swing.JLabel jLabel1;
}
MainMenuPanel.java 将是一个带有按钮的 JPanel,按下该按钮时,将更新 DisplayPanel 中 JLabel 文本设置的变量。
import java.awt.*;
import javax.swing.*;
public class MainMenuPanel extends javax.swing.JPanel
{
private JPanel MainPanel;
public MainMenuPanel(JPanel MainPanel)
{
this.MainPanel = MainPanel;
initComponents(); // Creates my panel with the JLabels/buttons etc
}
@SuppressWarnings("unchecked")
private void initComponents()
{
GoToDisplayPanelButton = new javax.swing.JButton();
GoToDisplayPanelButton.setText("Press Me");
GoToDisplayPanelButton.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent evt)
{
GoToDisplayPanelButtonMouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(252, 252, 252)
.addComponent(GoToDisplayPanelButton)
.addContainerGap(260, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(198, Short.MAX_VALUE)
.addComponent(GoToDisplayPanelButton)
.addGap(175, 175, 175))
);
}
private void GoToDisplayPanelButtonMouseClicked(java.awt.event.MouseEvent evt)
{
// Changes the value of item. I want this change to be reflected on my
// JLabel in DisplayPanel.java, but it is not because I do not explicitly
// call setText on the JLabel here. I am wondering if there is a way to
// have my JLabel automatically update when the variable in its setText
// is changed, without having to directly call setText. Or some other
// way to update it without calling setText every time I change something.
GUI.item = "new value";
// Switches to my DisplayPanel which should show the updated name in its JLabel
CardLayout layout = (CardLayout)(MainPanel.getLayout());
layout.show(MainPanel, "Display");
}
private javax.swing.JButton GoToDisplayPanelButton;
}
最佳答案
您需要使用通知系统,让模型(“后端”)在数据更改时通知 View (GUI)。然后 View 可以请求更新的信息并使用它来更新 JLabel 显示。其细节取决于代码的具体情况,但我经常为我的模型(或模型的包装器)提供一个 SwingPropertyChangeSupport 字段以及 addPropertyChangeListener(...)
和类似的删除监听器方法,然后在更改属性(绑定(bind)属性)的模型方法中,触发 SwingPropertyChangeSupport 的通知方法。有关实现的更多详细信息,您应该考虑显示代码的一些详细信息,最好是 minimal example program .
编辑
请注意,如果您想要的只是在调用 show 时发出通知,您始终可以扩展 CardLayout 并重写其 show(...)
方法。但如果您这样做,请不要忘记调用 super.show(...)
方法。
编辑
请注意对 //!!
import java.awt.CardLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class GUI extends javax.swing.JFrame {
public static final String ITEM = "item";
public String item; // !! this should not be static!
private javax.swing.JPanel MainPanel;
public GUI() {
// creates a JPanel called MainPanel which will hold the cards
// in my CardLayout
initComponents();
}
// !!
public void setItem(String item) {
String oldValue = this.item;
String newValue = item;
this.item = item;
firePropertyChange(ITEM, oldValue, newValue);
}
//!!
public String getItem() {
return item;
}
private void initComponents() {
MainPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
MainPanel.setBackground(new java.awt.Color(198, 118, 38));
MainPanel.setMaximumSize(new java.awt.Dimension(1000, 1000));
MainPanel.setName(""); // NOI18N
MainPanel.setPreferredSize(new java.awt.Dimension(1024, 768));
MainPanel.setLayout(new java.awt.CardLayout());
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(MainPanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(MainPanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap()));
pack();
setLocationRelativeTo(null);
}
// Grab the panel that was created in initComponents() so that I can add
// other panels to it
public JPanel getMainPanel() {
return MainPanel;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
final GUI mainGUI = new GUI();
// The JLabel in DisplayPanel.java will initially say root
// !! item = "root";
mainGUI.setItem("root"); // !!
JPanel cardHolder = mainGUI.getMainPanel();
// This panel contains a button which will update the name of
// the currentUserAccount
final MainMenuPanel mainMenu = new MainMenuPanel(cardHolder,
mainGUI); // !!
cardHolder.add(mainMenu, "MainMenu");
// This panel holds a JLabel that I want to have updated
// automatically
// when the user presses the button on MainMenuPanel
final DisplayPanel display = new DisplayPanel(cardHolder, mainGUI); // !!
cardHolder.add(display, "Display");
mainGUI.pack();
mainGUI.setVisible(true);
// !!
mainGUI.addPropertyChangeListener(ITEM,
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
String itemText = mainGUI.getItem();
display.setJLabel1Text(itemText);
}
});
}
});
}
}
@SuppressWarnings("serial")
class DisplayPanel extends javax.swing.JPanel {
private JPanel mainPanel;
private GUI mainGui; // !!
public DisplayPanel(JPanel mainPanel, GUI mainGui) { // !!
this.mainPanel = mainPanel;
this.mainGui = mainGui;// !!
initComponents(); // Creates my panel with the JLabels/buttons etc
}
// !! added
public void setJLabel1Text(String text) {
jLabel1.setText(text);
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
GoToMainMenuButton = new javax.swing.JButton();
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
// !! jLabel1.setText(GUI.item); // sets the JLabel to the GUI.item
// variable
jLabel1.setText(mainGui.getItem()); // !!
GoToMainMenuButton.setText("Go to MainMenuPanel");
GoToMainMenuButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
GoToMainMenuButtonMouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(269, Short.MAX_VALUE)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(GoToMainMenuButton)
.addComponent(
jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
145,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(266, 266, 266)));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup()
.addGap(168, 168, 168)
.addComponent(jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE, 63,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(83, 83, 83).addComponent(GoToMainMenuButton)
.addContainerGap(116, Short.MAX_VALUE)));
}
private void GoToMainMenuButtonMouseClicked(java.awt.event.MouseEvent evt) {
// go back to the MainPanel
CardLayout layout = (CardLayout) (mainPanel.getLayout());
layout.show(mainPanel, "MainMenu");
}
private javax.swing.JButton GoToMainMenuButton;
private javax.swing.JLabel jLabel1;
}
@SuppressWarnings("serial")
class MainMenuPanel extends javax.swing.JPanel {
private JPanel MainPanel;
private GUI mainGui; // !!
public MainMenuPanel(JPanel MainPanel, GUI mainGui) { // !!
this.MainPanel = MainPanel;
this.mainGui = mainGui; // !!
initComponents(); // Creates my panel with the JLabels/buttons etc
}
private void initComponents() {
GoToDisplayPanelButton = new javax.swing.JButton();
GoToDisplayPanelButton.setText("Press Me");
GoToDisplayPanelButton
.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
GoToDisplayPanelButtonMouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGap(252, 252, 252)
.addComponent(GoToDisplayPanelButton)
.addContainerGap(260, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(198, Short.MAX_VALUE)
.addComponent(GoToDisplayPanelButton).addGap(175, 175, 175)));
}
private void GoToDisplayPanelButtonMouseClicked(java.awt.event.MouseEvent evt) {
// Changes the value of item. I want this change to be reflected on my
// JLabel in DisplayPanel.java, but it is not because I do not explicitly
// call setText on the JLabel here. I am wondering if there is a way to
// have my JLabel automatically update when the variable in its setText
// is changed, without having to directly call setText. Or some other
// way to update it without calling setText every time I change something.
// !! GUI.item = "new value";
mainGui.setItem("new value"); // !!
// Switches to my DisplayPanel which should show the updated name in its
// JLabel
CardLayout layout = (CardLayout) (MainPanel.getLayout());
layout.show(MainPanel, "Display");
}
private javax.swing.JButton GoToDisplayPanelButton;
}
关于java - 显示下一个面板时更新 CardLayout 中的 JLable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26873302/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!