Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将标签和文本字段放在同一行中_Java_Swing_Grid Layout - Fatal编程技术网

Java 将标签和文本字段放在同一行中

Java 将标签和文本字段放在同一行中,java,swing,grid-layout,Java,Swing,Grid Layout,我想在JavaSwing中创建一个计算器,但我有一个问题,我想在同一行中放置一个标签和一个文本字段,而不是两列,而是在同一行中,例如:1:(标签)(这里的文本字段)在同一行中,对不起我的英语 我的代码: private JPanel mainPan; //= new JPanel(); private JLabel title; private JLabel nb1; private JLabel nb2; private JButton button; private JTextField t

我想在JavaSwing中创建一个计算器,但我有一个问题,我想在同一行中放置一个标签和一个文本字段,而不是两列,而是在同一行中,例如:1:(标签)(这里的文本字段)在同一行中,对不起我的英语

我的代码:

private JPanel mainPan; //= new JPanel();
private JLabel title;
private JLabel nb1;
private JLabel nb2;
private JButton button;
private JTextField textField; //= new JTextField();

public Fenetre() {
    super ("Calculator");
    mainPan = new JPanel();
    title = new JLabel();
    nb1 = new JLabel();
    nb2 = new JLabel();
    button= new JButton();
    textField = new JTextField();

    setMinimumSize(new Dimension(400, 500));
    setSize(400, 500);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(Fenetre.EXIT_ON_CLOSE);
    setResizable(true);

    setIconImage(new ImageIcon("C:\\Users\\sidot\\Desktop\\download.png").getImage());

    mainPan.setBackground(Color.darkGray);
    mainPan.setLayout(new GridLayout(2,0));
    Border border = LineBorder.createBlackLineBorder();


    title.setText("Calculator 1.0");
    title.setFont(new Font("MONOSPACED", Font.PLAIN, 20));
    title.setForeground(Color.WHITE);
    title.setHorizontalTextPosition(JLabel.CENTER);
    title.setVerticalTextPosition(JLabel.BOTTOM);
    title.setBorder(border);
    mainPan.add(title);

    nb1.setText("Number 1");
    nb1.setFont(new Font("MONOSPACED", Font.BOLD, 15));
    nb1.setForeground(Color.WHITE);
    nb1.setHorizontalTextPosition(JLabel.LEFT);
    nb1.setVerticalTextPosition(JLabel.CENTER);
    nb1.setBorder(border);
    mainPan.add(nb1);

    nb2.setText("Number 2");
    nb2.setFont(new Font("MONOSPACED", Font.BOLD, 15));
    nb2.setForeground(Color.WHITE);
    nb2.setHorizontalTextPosition(JLabel.LEFT);
    nb2.setVerticalTextPosition(JLabel.CENTER);
    nb2.setBorder(border);
    mainPan.add(nb2);

    this.setContentPane(mainPan);
    this.setVisible(true);
}
我想在1号和2号后面有一个文本字段。
谢谢一个
JPanel
的默认布局是
FlowLayout
。因此,您可以使用
JPanel
来保存标签和文本字段:

JPanel rowPanel = new JPanel();
rowPanel.add( label1 );
rowPanel.add( textField1 );
mainPanel.add( rowPanel );

要点是可以使用不同的布局管理器和组件嵌套面板,以实现所需的布局。阅读swing教程中的部分以了解更多信息。

谢谢,它可以工作,但它没有对齐“1号”,文本字段位于行窗格的顶部,如何更改它并将其放置在此行窗格的左中位置?您是否阅读了我提供给您的
布局管理器
链接?关于
如何使用FlowLayout
的部分解释了如何做到这一点。或者您是否阅读了
FlowLayout
API?可以将对齐方式更改为左对齐。或者您可能需要使用更复杂的布局管理器,如GridBagLayout。我不明白你的确切要求。当你阅读教程时,你会发现更多的信息,而不是一次只问一个问题。充分利用本教程的优势,并为其他Swing基础知识保留链接。