没有带有JPanel对象的setLayout()方法

时间:2019-02-23 05:31:07

标签: java swing awt

我想将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);
    }
}

1 个答案:

答案 0 :(得分:2)

您的JPanel p1可以很好地获取GridBagLayout,但是您在GUI中看不到按钮,因为您从未添加它们。例如,您可以将按钮添加到他们自己的JPanel(使用GridLayout的按钮),然后将其添加到GUI的第三行,它们会很好地显示:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

@SuppressWarnings("serial")
public class CamInfo extends JFrame implements ActionListener {

    JLabel lb1, lbUsername, lbPassword;
    JTextField tfUsername, tfPassword;
    JButton btLogin, btSignup, btGuest;

    public CamInfo() {

        // setSize(400, 200);  // don't do this. pack() instead

        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);

        // can add the buttons to their own GridLayout using JPanel
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 5));
        buttonPanel.add(btLogin);
        buttonPanel.add(btSignup);
        buttonPanel.add(btGuest);

        // then add this JPanel to fill the 3rd row of your GUI
        addItem(p1, buttonPanel, 0, 2, 2, 1, GridBagConstraints.CENTER);

        add(p1);
        pack();  // do this instead of set size
        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.HORIZONTAL;  // this is better than NONE
        p.add(c, gc);
    }
}

显示为:

enter image description here