[英]Where to place gui item code
我正在開始一個新的GUI項目,我想知道放置項目代碼的最佳位置在哪里,例如按鈕,文本字段或其他什么? 我不認為代碼的最佳位置在主類中,因為對於一個文件來說似乎代碼太多而且難以管理。 這就是我通常的做法(一體化文件)。
package apollo;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Apollo{
protected JFrame frame = new JFrame("Apollo");
public Apollo(){
frame.setSize(800, 600);
frame.setLayout(new FlowLayout());
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
this.buildLayout();
frame.revalidate();
}
protected void buildLayout(){
JTextField txt = new JTextField(30);
frame.add(txt);
JButton btn = new JButton("Submit");
frame.add(btn);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args){
Apollo a = new Apollo();
}
}
你的主要課通常應該只有一個主要的方法。
該main方法應該創建一個處理初始化GUI的類。
對於其他UI組件,如果重用它們,或者它們的代碼很大,則組件需要它自己的類。
如果您永遠不會重用該組件,並且其代碼很小,則它不需要自己的類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.