2011-02-25 77 views
2

我已經使用Java一段時間了,但是我從來沒有創建過GUI - 始終是CLI。如何在Java中創建一個GUI?你能建議一個很好的教程/參考嗎?如何在Java中創建GUI

我正在創建一個簡單的GUI,它有兩個長文本區域和一些按鈕。

+2

http://www.google.com/search?hl=zh-CN&source=hp&biw=1440&bih=715&q=creating+a+ui+in+java&aq=f&aqi=g1&aql=&oq= – Flynn1179 2011-02-25 15:00:08

+0

http://download.oracle。 com/javase/tutorial/uiswing/ – 2011-02-25 15:01:00

+1

@ Flynn1179谷歌認爲用於GUI的流行網站不會給我我在SO尋找的專業意見。 – 2011-02-25 15:01:01

回答

5

閱讀Oracle教程頁面上的Swing

+2

Plz no。這是2017年沒有理由使用Swing over Javafx /其他 – 2017-01-27 23:58:15

1

你有不同的可能性在這裏,但我推薦使用Swing的一個IDE如Netbeans的,這提供了一個非常好的所見即所得的編輯器(稱爲Matisse)。

Netbeans也有項目模板,您可以使用它來快速入門。

最後,正如其他人指出的那樣,請務必做好功課並閱讀一些初學Swing教程。

4

這裏有一個簡單的例子

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 

public class Foo{ 

    public static void main(String[] args) { 

    JFrame f = new JFrame("A JFrame"); 
    f.setSize(250, 250); 
    f.setLocation(300,200); 
    final JTextArea textArea = new JTextArea(10, 40); 
    f.getContentPane().add(BorderLayout.CENTER, textArea); 
    final JButton button = new JButton("Click Me"); 
    f.getContentPane().add(BorderLayout.SOUTH, button); 
    button.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      textArea.append("Button was clicked\n"); 

     } 
    }); 

    f.setVisible(true); 

    } 

} 
+0

事件可以替換爲lambda表達式:button.addActionListener((ActionEvent e) - > { textArea.append(「Button was clicked \ n」); } ); - 這個例子勝過千言萬語... – pollaris 2017-08-28 14:28:24

0

創建一個JFrame爲n盡人皆知,你需要的只是一個定義類和一個GUI類。這是最簡單的事情之一Java中

定義類:

public class GetMyJavaWindow { 
public static void main (String args[]) { 
new JavaWindow(); 
} 
} 

JFrame類:

import javax.swing.JFrame; 

public class JavaWindow { // Class name must match what it says in the def. class 
public static final long serialVersionUID = 1L; // Needed for the JFrame to work. 

public JavaWindow() { // Must match class name 

this.setVisible(true); // Required 
this.setDefaultCloseOperation(EXIT_ON_CLOSE); // Required 
this.setSize(800,600); // this.setSize (x,y); 

} 
} 

如果您需要更多的GUI幫助,來找我。

1

那麼你要做的是創建你的文本/代碼薄(記事本++或記事本),並記住名稱。

然後打開它的類型

import java.awt.*; 
import javax.swing.*; 

這主要是告訴Java來從用java來了,當你下載了它的各種代碼庫得到java.awtjavax.swing它(理解的,因爲這是由以幫助開發語言)。

然後,你需要使你的功能,從大小,它裏面的文本,顏色等方面的一切。 REMEBER,我們沒有在這裏編碼gui,因爲我們進口了進口java.awt.*;javax.swing.*;已經完成了。

當我把public class work,在我的文件的名稱工作(如果它被稱爲代碼它將是public class code

public class work { 

private static void createWindow() { 

//Create and set up the window 
JFrame frame = new JFrame ("simple GUI"); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
JLabel textLabel = new JLabel("Im cool" ,SwingConstants.CENTER); 
textLabel.setPreferredSize(new Dimension(300, 100)); 
frame.getContentPane().add(textLabel, BorderLayout.CENTER); 
//Display the window 
frame.setLocationRelativeTo(null); 
frame.pack(); 
frame.setVisible(true); 

請記住,您並沒有在任何地方調用函數,它基本上在那裏等待直到它被調用。

public static void main(String[] args) { 

這告訴計算機,接下來會發生什麼,當我運行該程序時你會這樣做。

所以裏面,你需要把

createWindow(); 

,因爲這是你叫你上面的功能,它被調用函數,你不需要爲它做它,當你運行調用這個函數程序。