java JPanel中的文本对齐
我是java的一名新生。这是一个大学项目,一个gui中的数学测试。 我对对齐有问题 我希望在运行程序时在gui中执行此操作java JPanel中的文本对齐,java,swing,layout,jpanel,jlabel,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 prob
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对齐
谢谢。您可以使用
setSize(x,y);
尺寸根据您的选择而定
或者,如果您不想学习任何布局管理器,只需使用NetBean编辑器即可提供拖放功能。对Jpanel使用绝对布局。 然后你可以把你的标签放在任何你想放的地方
Jpanel.setLayout(null);
JLabel lb=new JLabel("hi");
lb.setBounds(x,y,width,height);
你会想研究一下如何使用一个。我个人的推荐是非常灵活的。这需要一点时间来学习,但很值得。它可能有助于现在截图,然后画出你想要的样子。