在java JPanel中对齐文本

时间:2014-03-26 21:41:55

标签: java swing layout jpanel jlabel

我是java的新生。这是一个大学项目,是一个gui的数学考试。 我对齐问题。

当我运行程序

时,我想在我的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 个答案:

答案 0 :(得分:0)

你可以使用

setSize(x,y);

尺寸根据您的选择 或者只是使用可以提供拖放工具的Net bean编辑器

答案 1 :(得分:0)

如果您不想学习任何布局管理器。对Jpanel使用绝对布局。 然后,您可以将标签放在任何地方。

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