我正在學習Java和GUI。我有一些問題,首先是創建JFrame的子類和JFrame的實例之間有什麼主要區別。看起來像一個子類更強大?我也想知道是否有必要使用創建GUI時這個代碼:關於Java GUI並清除JFrame以顯示新內容?
Container contentPane = getContentPane();
contentPane.setLayot(new Flowlayout());
添加我的GUI類,這是一個簡單的測試,到目前爲止,到我手的任務。當用戶輸入。文本框中的一些文本,然後按按鈕繼續下一步,我該如何清除框架並顯示新內容,或者有沒有一種特殊的方法可以在Java中執行此操作?我想最好是使用相同的窗口而不是創建新的!?幫助ID preciated!由於
// Gui class
import java.awt.FlowLayout; // layout
import java.awt.event.ActionListener; // listener
import java.awt.event.ActionEvent; // event
import javax.swing.JFrame; // windows properties
import javax.swing.JLabel; // row of text
import javax.swing.JTextField; // enter text
import javax.swing.JOptionPane; // pop up dialog
import javax.swing.JButton; // buttons
// import.javax.swing.*;
public class Gui extends JFrame {
private JLabel text1;
private JTextField textInput1;
private JTextField textInput2;
private JButton nextButton;
// constructor creates the window and it's components
public Gui() {
super("Bank"); // title
setLayout(new FlowLayout()); // set default layout
text1 = new JLabel("New customer");
add(text1);
textInput1 = new JTextField(10);
add(textInput1);
nextButton = new JButton("Continue");
add(nextButton);
// create object to handle the components (action listener object)
frameHandler handler = new frameHandler();
textInput1.addActionListener(handler);
nextButton.addActionListener(handler);
}
// handle the events (class inside another class inherits contents from class outside)
private class frameHandler implements ActionListener {
public void actionPerformed(ActionEvent event){
String input1 = "";
// check if someone hits enter at first textfield
if(event.getSource() == textInput1){
input1 = String.format(event.getActionCommand());
JOptionPane.showMessageDialog(null, input1);
}
else if(event.getSource() == nextButton){
// ??
}
}
}
}
不錯!有多少面板可以有限制?所以我只是給面板添加不同的內容! – 2012-01-05 22:27:19
不,實際上沒有限制。但是更好的學習java.awt包提供的不同佈局。這可以以某種方式幫助你。由於每個組件都有默認的佈局設置,比如JFrame有BorderLayout和JPanel有FlowLayout。 Regards – 2012-01-05 23:04:51
我不知道他們何時離開ContentPane contentPane = frame.getContentPane();但上面的答案修復了在第二次加載時出現在我的Jframe上的重複數據 – 2017-09-07 19:43:27