我做了一個Swing應用程序,它的功能相當簡單。但是,我認爲它所組成的代碼變得相當龐大和非常混亂。所有的擺動組件和動作都在一個文件中。因此,舉例來說,如果我想用更多功能創建更大的應用程序,代碼將很難通過。有關Java Swing的一般問題
所以我的問題是如何使一個良好的代碼結構。或者如果有一個很好的網頁,我可以閱讀它,如果可能的話還有一些代碼示例。我已經檢查了Sun關於Swing的教程,但是他們展示了一些相當簡單的例子。
更新:我琢磨了一會兒,並檢查了一些例子。我不知道我是否得到了正確的MVC模式。無論如何,我的想法是將每個JFrame分離到他們自己的類文件中。之後,我有一個MainFrame,它是應用程序的主窗口。從該JFrame中,我創建了每個JFrame的一個實例。並用Actions來調用MainFrame中的那些幀。我不知道這是不是一個好主意。但是,它使代碼顯着易於閱讀。
這裏是我如何意味着
class Main implements ActionListener {
private JFrame frame = new JFrame();
private JButton button1 = new JButton();
private JPanel panel = new JPanel();
private FirstFrame frame1 = new FirstFrame();
private SecondFrame frame2 = new SecondFrame();
private ThirdFrame frame3 = new ThirdFrame();
public Main() {
button1.addActionListener(this);
}
public createGUI() {
frame.setTitle("Main");
frame.setSize(400,300);
panel.add(button);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public static void main(String args[]) {
new Main().createGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1)
{
frame1.enable();
}
}
}
偉大的問題!我一直在思考這個問題。 – 2009-12-26 11:21:13