2014-09-28 27 views
0

我對java很陌生。我用一些jlabel,jtextfield創建了一個GUI窗口。但是最近對於一個項目,我需要用jlabel和jtextfield創建另一個類,並且需要將它們顯示給主類。但我不知道該怎麼做。我已經看過這個教程,但它只覆蓋了jmenu(而不是jtext字段和jlabel)。如何將差異擺動項目包含到另一個類的主類中

Adding Swing components from another class

我的代碼如下,

公共類common_items擴展的JFrame { 公共無效item_gui(字符串labelForMatrix){

JPanel contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 


    JLabel lbl = new JLabel(labelForMatrix); 
    lbl.setBounds(5, 5, 424, 14); 
    lbl.setHorizontalAlignment(SwingConstants.CENTER); 
    contentPane.add(lbl); 

    JTextField txt2 = new JTextField(); 
    txt2.setBounds(178, 117, 86, 20); 
    contentPane.add(txt2); 
    txt2.setColumns(10); 

    JTextField txt1 = new JTextField(); 
    txt1.setColumns(10); 
    txt1.setBounds(178, 64, 86, 20); 
    contentPane.add(txt1); 

    JLabel lblRowNumber = new JLabel("Row number"); 
    lblRowNumber.setBounds(189, 39, 67, 14); 
    contentPane.add(lblRowNumber); 

    JLabel lblColumnNumber = new JLabel("column number"); 
    lblColumnNumber.setBounds(155, 95, 119, 14); 
    contentPane.add(lblColumnNumber); 
    } 
    } 

我試圖顯示他們對主類,但它失敗 。我的主類代碼是

common_items itm=new common_items(); 
    itm.item_gui(); 

Plzz建議我該怎麼做。

回答

0

如果我理解正確的是你正在嘗試做的,你正試圖使主JFrame類,其中包括來自其他類,如JLabel或連接到班級,並把它們作爲普通組件JTextArea組件。因此,你需要做的就是創建一個主類(而不是JFrame)並製作一個JFrame類(例如稱爲mainFrame),然後創建組件類(例如textArea JLabel ...),然後再創建一個類組件您只需將這些類作爲框架類中的變量(我們將從主類執行)將其表示爲變量。

,也許你只是理解的代碼比言語更好:

主要類來啓動程序(加載框架類,它包含的所有內容)

public class Main { 

    public Main(){ 
     //execute the frame 
     new Frame(); 
    } 

    public static void main(String[] args) { 
     new Main(); //execute the function that launches the JFrame 
    } 

} 

框架類(明顯)包含框架

import javax.swing.JFrame;

public class Frame extends JFrame{ 

    private static final long serialVersionUID = -5151041547543472432L; //ignore this it is just the serial id of the JFrame you can omit this line from your code 

    ComponentsStorage cs = new ComponentsStorage(); //display class as variable so we can use it more efficnent 

    public Frame(){ 
     launch(); //execute function 
    } 

    public void launch() { //launch 
     setTitle("Frame Demo");//title 
     setSize(640, 480);//size (width * height) 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close when close button is pressed 

     //you can just do ComponentStorage.tf.blah blah blah... but it is much easier as a variable 
     cs.label.setSize(500, 500); // you can mess up witht the components you stored there 
     cs.tf.setSize(40, 5); //as you can see here I am just adding the components by 

     //add them here 
     add(cs.label); 
     add(cs.tf); 

     setLocationRelativeTo(null);//by setting this you place the screen always in the middle of the screen 
     setVisible(true);//there is no point of creating JFrames if they aren't visible 
    } 

} 

而只是存儲組件,您要使用(的JLabel,JTextField中...)其他類(也可以是隻包含這些東西即使有它我把它命名等功能的任何類ComponentStorage

import javax.swing.JTextField; 
import javax.swing.JLabel; 

public class ComponentsStorage { 

    JLabel label = new JLabel(); 
    JTextField tf = new JTextField(); 

} 
相關問題