将 Label 和 TextField 放在同一行
Put a Label and a TextField in a same row
我想在 Java Swing 中创建一个计算器,但我有一个问题,我想将一个标签和一个文本字段放在同一行中,而不是两列,而是放在同一行中,例如:数字 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 );
重点是您可以嵌套具有不同布局管理器和组件的面板,以实现您想要的布局。阅读 Layout Manger 上的 swing 教程部分了解更多信息。
我想在 Java Swing 中创建一个计算器,但我有一个问题,我想将一个标签和一个文本字段放在同一行中,而不是两列,而是放在同一行中,例如:数字 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 );
重点是您可以嵌套具有不同布局管理器和组件的面板,以实现您想要的布局。阅读 Layout Manger 上的 swing 教程部分了解更多信息。