java - 将变量范围从一帧扩展到另一帧
问题描述
我有两个JFrame
基于窗口:SeatLayout
和BillSummary
. 我需要seatnumber
从SeatLayout
框架中获取并显示它,BillSummary
但变量范围仅限于第一帧。
我怎样才能做到这一点?
解决方案
使用多个 JFrame 是一种不好的做法,应该避免。原因是,它会在未来增加更多的问题,维护起来将是一场噩梦。
要回答您的问题,如何将变量从您的父级(JFrame)传递给一个子级(JDialog)。这可以通过使用 JDialog 来实现。
我将通过一个例子来运行。可以说,您的 BillSummary.java 是....
//BillSummary Class
public class billSummary {
JFrame frame;
billSummary(JFrame frame) {
this.frame = frame;
}
public void launchbillSummary(int seatNumber) {
// Create a dialog that suits your ui , you can use JPanel as your layout container
JDialog dialog = new JDialog(frame, "Bill Summary", true);
dialog.setLayout(new BorderLayout());
dialog.setSize(100, 100);
dialog.add(new JLabel(Integer.toString(seatNumber)), BorderLayout.CENTER);
dialog.setVisible(true);
}
}
你的座位布局.java
public class seatLayout {
seatLayout(){
//Lets say you have seleted seat number 10
int defaultSeatNumber = 10;
//Lets say you have a button and when it is clicked , you pass the data to billsummary page
JButton enter = new JButton("Enter");
//Your seatLayout GUI
JFrame frame = new JFrame("seat layout");
frame.setSize(300,300);
frame.add(enter);
enter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
//Do your passing of data/ price of calculation here
//You pass the data that to your custom dialog -> Bill summary
new billSummary(frame).launchbillSummary(defaultSeatNumber);
}
});
frame.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new seatLayout();
}
});
}
}
我希望这有助于并回答您的问题。祝你好运 :)
推荐阅读
- python - 如何在 Heroku 上上传 python 模块
- c# - 如何使用封闭的 xml 比较列值?
- ios - InsertRows to UITableView swift 4 有错误:我的最后一行从上到下滚动
- elasticsearch - 由于 .kibana 索引,集群处于黄色状态
- .net - SpecRun- 测试未显示在测试资源管理器中
- ios - RxSwift 子类化最佳实践
- python - IBM-Watson 情绪分析
- slickgrid - 如何添加左侧下拉菜单以选择运算符
- node.js - Heroku 无法使用我的 Keyv 和 Sqlite 数据库
- django - 更新多个模型实例的有效方法