Я хочу применить gridBagLayout к моей форме и создаю объект JPanel, но метод setLayout () не отображается.Может кто-нибудь помочь, пожалуйста?
Путь:
import javax.swing.*;
JPanel p = new JPanel();
p.setLayout(new GridBagLayout());
Вот мой полный исходный код: Нет ошибки, просто не работает Layout Manager.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class CamInfo extends JFrame implements ActionListener{
JLabel lb1, lbUsername, lbPassword;
JTextField tfUsername, tfPassword;
JButton btLogin, btSignup, btGuest;
public CamInfo() {
setSize(400,200);
JPanel p1 = new JPanel();
p1.setLayout(new GridBagLayout()); // no setLayout() method for p1
this.setLocationRelativeTo(null);
lb1 = new JLabel("Travelling Guide");
lbUsername = new JLabel("Username");
lbPassword = new JLabel("Password");
tfUsername = new JTextField(10);
tfPassword = new JTextField(10);
btLogin = new JButton("Login");
btSignup = new JButton("Sign up");
btGuest = new JButton("Guest");
addItem(p1, lbUsername, 0,0,1,1, GridBagConstraints.EAST);
addItem(p1, lbPassword, 0,1,1,1, GridBagConstraints.EAST);
addItem(p1, tfUsername, 1,0,2,1, GridBagConstraints.WEST);
addItem(p1, tfPassword, 1,1,1,1, GridBagConstraints.WEST);
add(p1);
setVisible(true);
btLogin.addActionListener(this);
btSignup.addActionListener(this);
btGuest.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new CamInfo();
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == btLogin){
}
if(ae.getSource() == btSignup){
}
if(ae.getSource() == btGuest){
}
}
void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align){
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.weightx = 100.0;
gc.weighty = 100.0;
gc.insets = new insets(5, 5, 5, 5);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c, gc);
}
}
Вот мой полный исходный код: нет ошибки, просто не работает Layout Manager.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class CamInfo extends JFrame implements ActionListener{
JLabel lb1, lbUsername, lbPassword;
JTextField tfUsername, tfPassword;
JButton btLogin, btSignup, btGuest;
public CamInfo() {
setSize(400,200);
JPanel p1 = new JPanel();
p1.setLayout(new GridBagLayout()); // no setLayout() method for p1
this.setLocationRelativeTo(null);
lb1 = new JLabel("Travelling Guide");
lbUsername = new JLabel("Username");
lbPassword = new JLabel("Password");
tfUsername = new JTextField(10);
tfPassword = new JTextField(10);
btLogin = new JButton("Login");
btSignup = new JButton("Sign up");
btGuest = new JButton("Guest");
addItem(p1, lbUsername, 0,0,1,1, GridBagConstraints.EAST);
addItem(p1, lbPassword, 0,1,1,1, GridBagConstraints.EAST);
addItem(p1, tfUsername, 1,0,2,1, GridBagConstraints.WEST);
addItem(p1, tfPassword, 1,1,1,1, GridBagConstraints.WEST);
add(p1);
setVisible(true);
btLogin.addActionListener(this);
btSignup.addActionListener(this);
btGuest.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new CamInfo();
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == btLogin){
}
if(ae.getSource() == btSignup){
}
if(ae.getSource() == btGuest){
}
}
void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align){
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.weightx = 100.0;
gc.weighty = 100.0;
gc.insets = new insets(5, 5, 5, 5);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c, gc);
}
}