作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个基于 JFrame
的窗口:SeatLayout
和 BillSummary
。我需要从 SeatLayout
框架获取 seatnumber
并将其显示在 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);
}
}
你的seatLayout.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();
}
});
}
}
我希望这对您有所帮助并回答您的问题。祝你好运:)
关于java - 将变量范围从一帧扩展到另一帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58364114/
有时,当我调用 ipdb 时,我知道我想要成为跟踪设置上方的框架。我认为这就是 API 公开 frame 参数的原因(如 the documentation 中所述)。 所以这是函数: import
我是一名优秀的程序员,十分优秀!