我想在 JLabel(s) 之间创建 space 并将 JButtons 对齐到同一行
I want to create space between the JLabel(s) and align the JButtons to be on the same line
我在 Java 中使用不同的布局创建了一个注册页面,但我无法将按钮对齐在同一行并在每个 JLabel.You 之间创建 space 可以看到它在 link (https://postimg.org/image/jaur8rxz7/) 上。
这是我写的代码
package liblog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Aregistration extends JFrame {
private JTextField text;
private JTextField text2;
private JPasswordField pass;
private JButton log1;
private JButton sign1;
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());
JLabel label1 = new JLabel();
JLabel label3 = new JLabel();
JLabel label2 = new JLabel();
Aregistration(){
super("Admin Registration");
setLayout(new FlowLayout(FlowLayout.CENTER,15,15));
setBounds(500,500,500,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
con.add(panel);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
label1=new JLabel("First Name");
label1.setToolTipText("Enter Your First Name");
text = new JTextField("Name",20);
label2=new JLabel("Registration No");
label2.setToolTipText("Enter Your Registation no");
text2 = new JTextField("Registation No",20);
label3 = new JLabel("Password");
label3.setToolTipText("Enter Your Password");
pass = new JPasswordField("Password",20);
log1 = new JButton("Log In");
sign1 = new JButton("Register");
panel.add(Box.createRigidArea(new Dimension(0, 0)));
panel.add(Box.createVerticalStrut(HEIGHT));
panel.add(label1);
panel.add(text);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(pass);
panel.add(log1);
panel.add(sign1);
setVisible(true);
}
public static void main(String[]args){new Aregistration();
}
}
I'm having trouble aligning the buttons on the same line
创建第二个面板并将按钮添加到面板,然后将此面板添加到主面板:
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS));
buttonPanel.add( log1 );
buttonPanel.add( Box.createHorizontalStrut(5) );
buttonPanel.add( sign1 );
panel.add( buttonPanel );
and creating space between each JLabel
您也可以使用垂直支柱。
阅读有关 How to Use BoxLayout 的 Swing 教程部分,了解更多信息和示例。
我在 Java 中使用不同的布局创建了一个注册页面,但我无法将按钮对齐在同一行并在每个 JLabel.You 之间创建 space 可以看到它在 link (https://postimg.org/image/jaur8rxz7/) 上。 这是我写的代码
package liblog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Aregistration extends JFrame {
private JTextField text;
private JTextField text2;
private JPasswordField pass;
private JButton log1;
private JButton sign1;
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());
JLabel label1 = new JLabel();
JLabel label3 = new JLabel();
JLabel label2 = new JLabel();
Aregistration(){
super("Admin Registration");
setLayout(new FlowLayout(FlowLayout.CENTER,15,15));
setBounds(500,500,500,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
con.add(panel);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
label1=new JLabel("First Name");
label1.setToolTipText("Enter Your First Name");
text = new JTextField("Name",20);
label2=new JLabel("Registration No");
label2.setToolTipText("Enter Your Registation no");
text2 = new JTextField("Registation No",20);
label3 = new JLabel("Password");
label3.setToolTipText("Enter Your Password");
pass = new JPasswordField("Password",20);
log1 = new JButton("Log In");
sign1 = new JButton("Register");
panel.add(Box.createRigidArea(new Dimension(0, 0)));
panel.add(Box.createVerticalStrut(HEIGHT));
panel.add(label1);
panel.add(text);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(pass);
panel.add(log1);
panel.add(sign1);
setVisible(true);
}
public static void main(String[]args){new Aregistration();
}
}
I'm having trouble aligning the buttons on the same line
创建第二个面板并将按钮添加到面板,然后将此面板添加到主面板:
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS));
buttonPanel.add( log1 );
buttonPanel.add( Box.createHorizontalStrut(5) );
buttonPanel.add( sign1 );
panel.add( buttonPanel );
and creating space between each JLabel
您也可以使用垂直支柱。
阅读有关 How to Use BoxLayout 的 Swing 教程部分,了解更多信息和示例。