2013-12-18 140 views
0

我對編碼相當陌生,我只是想創建一個簡單的GUI。我的代碼的目標是設計一個應用程序看起來像這樣: !http://curriculum.kcdistancelearning.com/courses/PROG2s-HS-A08/s/unit7/resources/images/JV_7.5.11.JPG! [代碼目標示例] 我一直在使用窗口生成器,我喜歡它在構建器內部的外觀。我的問題是當我嘗試將我的代碼作爲Java應用程序運行時。一個錯誤,說「選擇不包含小程序」我想我可能錯過了一些東西。我不確定我錯過了什麼,或者它會是什麼。Java Applet,當我按下Java程序運行時沒有運行

這裏是我的代碼看起來像現在:

`package Create.GUI` 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.border.LineBorder; 
import java.awt.Color; 
import javax.swing.JRadioButton; 
import javax.swing.JTextField; 
import javax.swing.JLabel; 
import javax.swing.SpringLayout; 
import javax.swing.ImageIcon; 
import javax.swing.SwingConstants; 
import javax.swing.JScrollPane; 
import java.awt.TextArea; 
import java.awt.Button; 
import java.awt.Font; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.Canvas; 
import javax.swing.JProgressBar; 
import javax.swing.JToggleButton; 
import javax.swing.JTextArea; 
import javax.swing.JButton; 
import javax.swing.JScrollBar; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JPasswordField; 
import java.awt.BorderLayout; 
public class StringPannel1 extends JFrame 
{ 
    private StringPannel1 currentPannel; 
    public StringPannel1() { 
     getContentPane().setBackground(Color.WHITE); 
     SpringLayout springLayout = new SpringLayout(); 
     getContentPane().setLayout(springLayout); 

    JButton btnNewButton = new JButton("Button"); 
    springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, 23, SpringLayout.NORTH, getContentPane()); 
    springLayout.putConstraint(SpringLayout.WEST, btnNewButton, 23, SpringLayout.WEST, getContentPane()); 
    springLayout.putConstraint(SpringLayout.SOUTH, btnNewButton, 88, SpringLayout.NORTH, getContentPane()); 
    getContentPane().add(btnNewButton); 

    txtTextFeild = new JTextField(); 
    txtTextFeild.setBackground(new Color(248, 248, 255)); 
    springLayout.putConstraint(SpringLayout.NORTH, txtTextFeild, 0, SpringLayout.NORTH, btnNewButton); 
    springLayout.putConstraint(SpringLayout.WEST, txtTextFeild, 27, SpringLayout.EAST, btnNewButton); 
    springLayout.putConstraint(SpringLayout.SOUTH, txtTextFeild, 0, SpringLayout.SOUTH, btnNewButton); 
    springLayout.putConstraint(SpringLayout.EAST, txtTextFeild, -53, SpringLayout.EAST, getContentPane()); 
    txtTextFeild.setText("Text Feild"); 
    getContentPane().add(txtTextFeild); 
    txtTextFeild.setColumns(10); 

    JScrollBar scrollBar = new JScrollBar(); 
    springLayout.putConstraint(SpringLayout.NORTH, scrollBar, 17, SpringLayout.SOUTH, btnNewButton); 
    springLayout.putConstraint(SpringLayout.WEST, scrollBar, 23, SpringLayout.WEST, getContentPane()); 
    springLayout.putConstraint(SpringLayout.SOUTH, scrollBar, 130, SpringLayout.SOUTH, btnNewButton); 
    getContentPane().add(scrollBar); 

    JTextArea txtrTextArea = new JTextArea(); 
    txtrTextArea.setBackground(new Color(248, 248, 255)); 
    springLayout.putConstraint(SpringLayout.NORTH, txtrTextArea, 0, SpringLayout.NORTH, scrollBar); 
    springLayout.putConstraint(SpringLayout.WEST, txtrTextArea, 7, SpringLayout.EAST, scrollBar); 
    springLayout.putConstraint(SpringLayout.SOUTH, txtrTextArea, 0, SpringLayout.SOUTH, scrollBar); 
    springLayout.putConstraint(SpringLayout.EAST, txtrTextArea, 173, SpringLayout.EAST, scrollBar); 
    txtrTextArea.setText("Text Area"); 
    getContentPane().add(txtrTextArea); 

    JCheckBox chckbxCheckBox = new JCheckBox("Check Box"); 
    chckbxCheckBox.setBackground(new Color(255, 255, 255)); 
    springLayout.putConstraint(SpringLayout.NORTH, chckbxCheckBox, 58, SpringLayout.SOUTH, txtTextFeild); 
    springLayout.putConstraint(SpringLayout.WEST, chckbxCheckBox, 6, SpringLayout.EAST, txtrTextArea); 
    getContentPane().add(chckbxCheckBox); 

    JLabel lblLabel = new JLabel("Label"); 
    springLayout.putConstraint(SpringLayout.NORTH, lblLabel, 15, SpringLayout.SOUTH, txtrTextArea); 
    springLayout.putConstraint(SpringLayout.WEST, lblLabel, 104, SpringLayout.WEST, getContentPane()); 
    lblLabel.setFont(new Font("Tahoma", Font.PLAIN, 13)); 
    getContentPane().add(lblLabel); 
} 
private JTextField txtTextFelid; 
/** 
* @wbp.nonvisual location=-193,109 
*/ 
private final JButton button = new JButton("New button"); 
private JTextField txtTextFeild; 
public void StringPannel() 
{ 
    currentPannel = new StringPannel1(); 

    setupFrame(); 
} 

private void setupFrame() 
{ 
    this.setVisible(true); 
    this.setContentPane(currentPannel); 
    this.setSize(350, 350); 
    this.setDefultCloseOpperation(JFrame.EXIT_ON_CLOSE); 
} 
private void setDefultCloseOpperation(int exitOnClose) { 
    // TODO Auto-generated method stub 

} 
{ 

{ 
    setupPannel1() 
} 
private void setupPannel() 
{ 
    setBorder(new LineBorder(new Color(0, 0, 255))); 
    setBackground(new Color(255, 255, 255)); 
    SpringLayout springLayout = new SpringLayout(); 
    getContentPane().setLayout(springLayout); 




} 

private void setBorder(LineBorder lineBorder) { 
    // TODO Auto-generated method stub 

} 
} 

所有幫助表示讚賞!

+1

嗯,有一件事你沒有'main()'...除非你的意思是你應該寫一個* Applet * –

回答

1

基本上,您的應用程序從JFrame延伸而不是JApplet

還有就是兩者之間的(顯著的)差異...

我會是第一個告訴...

  1. 始終創建基於JPanel核心UI,然後添加此/這些到您想要使用的容器(JFrameJApplet),它使您的代碼更加可重用,並允許您按需要部署應用程序,而無需訴諸某種破解...
  2. 您應該避免使用小程序,直到您瞭解UI的工作方式。小程序帶來有自己的問題,它可以使你的生活很悽慘,直到你明白他們

如果你開發一個applet是一意孤行,看看Lesson: Java Applets更多細節