【问题标题】:Aligning text in java JPanel在java JPanel中对齐文本
【发布时间】:2014-03-26 21:41:54
【问题描述】:

我是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。

谢谢。

【问题讨论】:

  • 你会想看看使用LayoutManager。我个人的建议是非常灵活的MigLayout。学习需要一点时间,但非常值得。
  • 截取它现在的样子可能会有所帮助,然后画出你想要的样子。

标签: java swing layout jpanel jlabel


【解决方案1】:

你可以使用

setSize(x,y);

尺寸根据您的选择 或者简单地使用可以提供拖放功能的 Net bean 编辑器

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-02-14
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 2012-07-10
      • 2012-07-28
      • 1970-01-01
      相关资源
      最近更新 更多