如何關閉當前幀(Frame1)並打開已創建的新幀(Frame2),並通過單擊按鈕將數據從frame1傳遞到frame2?按鈕提交時的擺動方式
0
A
回答
3
2
達到這一目的的最佳方式,是通過@Andrew湯普森十分告訴你。 另一種完成方式是代碼中描述的問題的動機。在你創建新的JFrame
的對象時,你必須將其他類中需要的東西作爲參數傳遞給另一個類,或者你可以簡單地傳遞這個對象(通過這個,你可以一次性傳遞所有對象)類)
的一點幫助的樣本代碼:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TwoFramesExample
{
public JFrame frame;
private JPanel panel;
private JButton button;
private JTextField tfield;
private SecondFrame secondFrame;
public TwoFramesExample()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new BorderLayout());
tfield = new JTextField(10);
tfield.setBackground(Color.BLACK);
tfield.setForeground(Color.WHITE);
button = new JButton("NEXT");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
// Here we are passing the contents of the JTextField to another class
// so that it can be shown on the label of the other JFrame.
secondFrame = new SecondFrame(tfield.getText());
frame.dispose();
}
});
frame.setContentPane(panel);
panel.add(tfield, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TwoFramesExample();
}
});
}
}
class SecondFrame
{
private JFrame frame;
private JPanel panel;
private JLabel label;
private JButton button;
private TwoFramesExample firstFrame;
public SecondFrame(String text)
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new BorderLayout());
label = new JLabel(text);
button = new JButton("BACK");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
firstFrame = new TwoFramesExample();
frame.dispose();
}
});
frame.setContentPane(panel);
panel.add(label, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
}
希望這是一些幫助。
Regards
+0
+1。 'frame.setLocationRelativeTo(null);'另請參見['setLocationByPlatform(true)'](http://stackoverflow.com/a/7143398/418556)。適合這種情況。 ;) – 2012-01-27 11:29:58
+0
@AndrewThompson:謝謝,正是我在找的東西,因爲我剛剛從你的答案的某個地方找到了關於這個東西的地方:-)問候 – 2012-01-27 11:33:25
相關問題
- 1. 用提交按鈕的形式提交表單與幾個提交按鈕
- 2. 在點擊提交按鈕時提交
- 3. 表單提交按鈕不會提交按鈕時的名稱是「提交」
- 4. 提交按鈕動畫
- 5. Java提交按鈕動作
- 6. 提交按鈕動作
- 7. 自動提交形式提交按鈕參數
- 8. 當提交按鈕的值時提醒
- 9. 提交不提交按鈕
- 10. 提交按鈕不提交
- 11. 提交按鈕不提交
- 12. 提交按鈕不提交
- 13. 按動按鈕提交按鈕的動畫背景?
- 14. 當頁面加載時,自動調用按鈕提交的方式
- 15. 自動提交Javascript的按鈕動作
- 16. 提交按鈕
- 17. 的提交按鈕
- 18. 按鈕Href按按鈕提交按鈕
- 19. 表單未按提交按鈕提交
- 20. 最簡潔的方式爲formtastic提交按鈕設置樣式
- 21. 形式輸入按鍵提交或點擊提交按鈕
- 22. 如何擺脫IE和Opera中的提交按鈕內邊框?
- 23. 按下按鈕時提交表單值
- 24. Activate-Deactivate提交按鈕的形式
- 25. 2形式的一個提交按鈕
- 26. 未提交的單選按鈕形式
- 27. 從一個提交的兩種形式的值提交按鈕
- 28. 提交表單的提交按鈕
- 29. 擺動切換按鈕
- 30. 停止按鈕在擺動
您能否詳細說明您的問題。什麼情況? – MrWaqasAhmed 2012-01-27 09:53:50
@MWWaqasAhmed優秀的問題。 +1 – 2012-01-27 12:37:35