最近作自動數據平臺,沒有界面。週末因而想用java實現一個可視化的界面。java
package cn.wuwenfu.swing; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class HelloWorld extends JFrame{ private JPanel infoJP; private JPanel textJP; private JPanel buttJP; public HelloWorld(){ super(); this.setSize(500, 600); this.getContentPane().setLayout(new GridLayout(3,1)); this.add(getInfoJP()); this.add(getTextJP()); this.add(getButtJP()); this.setTitle("ryzur"); } /*瑞德醫療官網,工做日誌*/ private JPanel getInfoJP() { if(infoJP==null){ infoJP = new JPanel(); //infoJP.setSize(500, 200); infoJP.setLayout(new FlowLayout()); infoJP.add(new JButton("瑞德醫療官網")); infoJP.add(new JButton("查看本地日誌")); } return infoJP; } /*軟件介紹*/ private JPanel getTextJP() { if(textJP==null){ textJP = new JPanel(); //textJP.setSize(500, 200); textJP.setLayout(new FlowLayout()); textJP.add(new JLabel("瑞德醫療自動數據平臺......................")); } return textJP; } /*開啓,關閉*/ private JPanel getButtJP() { if(buttJP==null){ buttJP = new JPanel(); //buttJP.setSize(500, 200); buttJP.setLayout(new FlowLayout()); buttJP.add(new JButton("開啓")); buttJP.add(new JButton("關閉")); } return buttJP; } public static void main(String[] args) { HelloWorld w = new HelloWorld(); w.setVisible(true); } }
實現的效果是函數
界面大概就這樣實現了,後面要作的是 實現按鈕的效果。佈局
收穫:JFrame是窗體,JPanel是容器,放置多個組件時,若是須要不一樣佈局,應該是在窗體中放置多個容器,每一個容器採用不一樣佈局。this
一開始我錯誤的覺得JFrame 內放入JFrame 。日誌
收穫二:上述代碼中的結果比較清晰,構造函數調用get方法,完成初始化。變量和get方法設置爲private ,這樣的代碼須要借鑑。blog