2014-03-26 83 views
1

我是一個java.a的新學生。這是一個大學項目,是一個gui的數學測試。 我有對齊問題。在java中對齊文本JPanel

我想做到這一點在我的GUI,當我運行程序

     Math Test - 10 exercises (this in the middle) 
          about 2 spaces here 

1. How much is one added to one? (this is an example. the math problems are going to be word problems with several sentences.) 

a space here 

Your answer, please. here goes a JTextField (4) 


about 2 spaces here 

2. here goes exercise number 2 


etc. etc. 

你認爲什麼是數學題的JLabel或JTextArea的更好嗎?

這裏我進口Swing和AWT

public class MathTest extends JPanel 
{ 
    public JTextField stu; 

    public MathTest() { 
     JLabel m=new JLabel(); 
     m.setText ("Math Test - 10 exercises"); 
     add(m); 

     JLabel exone= JLabel(); 
     exone.setText("1. how much is one added to one?"); 
     add(exone); 

     JLabel y= new JLabel(); 
     y.setText ("Your answer, please."); 
     add(y); 

     stu= JTextField (4); 
     add(stu);  
    } 
} 

的程序工作,但我不能對準標籤和JTextField的大小。

謝謝。

+2

您將需要使用['LayoutManager'](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)進行研究。我個人的建議是非常靈活的['MigLayout'](http://www.miglayout.com/)。學習需要一點時間,但是非常值得。 – MirroredFate

+0

它可能有助於截取它現在的樣子,然後畫出你想要的樣子。 – Charlie

回答

0

您可以根據您的選擇 使用

setSize(x,y); 

尺寸或乾脆用淨豆編輯器,可以爲拖動提供的設施和下降

0

如果你不想學習任何佈局管理器。對Jpanel使用絕對佈局。 然後你可以把你的標籤放在任何你想要的地方。

Jpanel.setLayout(null); 
JLabel lb=new JLabel("hi"); 
lb.setBounds(x,y,width,height);