当前位置: 首页 > 知识库问答 >
问题:

无法添加到布局约束必须是gridbagconstraint

党博超
2023-03-14

我需要使用GridBagLayout,但我得到以下错误。你能帮忙吗?

java.security.IllegalArgumentException:不能添加到布局:约束必须是一个GridBagConstraint在ontroller.doGridBagLjava.security.LayoutComponent(Unknown Source)在mpl.doCjava.awt.Impl(Unknown Source)在ueue.dispatchCjava.awt.(Unknown Source)在havuzAracßForMu.(havuzAracßFhread.pump)在havuzAracßForMu$java.awt.(havuzAracßFormu.java:47)在java.awt.event.InvocationEvent.dispatch(Unknown Source)在java.awt.EventQueue.dispatchEventImpl(Unknown Source)在java.awt.EventQueue.access500美元(Unknown Source)在java.awt.EventQueue$3.run(Unknown Source)在java.awt.EventQueue$3.run(Unknown Source)在java.lang.AccessCjava.awt.Privileged(Native Method)在ayout.addProtexiDomain$JavaSecurityAccessIjava.awt.IntersectionPrivilge(Unknown Source)在ontainer.addEventQjava.awt.Event(Unknown Source)在ontainer.addEventDispatchTormu.java:221OneEventForFilters(Unknown Source)在1.runE

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFormattedTextField.AbstractFormatter;

import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
import java.awt.event.ActionEvent;

public class havuzAracıFormu extends JFrame {

    private JPanel contentPane;
    private JTextField textFieldAdSoyad;
    private JTextField textFieldGorev;
    private JTextField textFieldDepartman;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    havuzAracıFormu frame = new havuzAracıFormu();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws ClassNotFoundException 
     * @throws SQLException 
     */
    public havuzAracıFormu() throws ClassNotFoundException, SQLException {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 728, 992);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;

        setContentPane(contentPane);

        JLabel lblKullancBilgileri = new JLabel("Kullan\u0131c\u0131 Bilgileri");
        Font font = lblKullancBilgileri.getFont();
        lblKullancBilgileri.setFont(new Font(font.getFontName(), Font.BOLD, 14));
        contentPane.add(lblKullancBilgileri);       

        JLabel lblAdSoyad = new JLabel("Ad\u0131 Soyad\u0131");
        JLabel lblGorevi = new JLabel("Görevi");
        JLabel lblDepartmani = new JLabel("Departman\u0131");
        contentPane.add(lblAdSoyad,gbc);

        textFieldAdSoyad = new JTextField();
        contentPane.add(textFieldAdSoyad, gbc);
        textFieldAdSoyad.setColumns(10);

        contentPane.add(lblGorevi);

        textFieldGorev = new JTextField();
        contentPane.add(textFieldGorev, gbc);
        textFieldGorev.setColumns(5);

        contentPane.add(lblDepartmani, gbc);

        textFieldDepartman = new JTextField();
        contentPane.add(textFieldDepartman);
        textFieldDepartman.setColumns(10);


        JLabel lblAracBilgileri = new JLabel("Araç Bilgileri");
        Font font2 = lblAracBilgileri.getFont();
        lblAracBilgileri.setFont(new Font(font2.getFontName(), Font.BOLD, 14));
        contentPane.add(lblAracBilgileri, gbc);

        JLabel lblMarka = new JLabel("Marka");
        contentPane.add(lblMarka, gbc);
        JTextField textFieldMarka = new JTextField(5);
        contentPane.add(textFieldMarka,gbc);
        textFieldMarka.setColumns(5);

        JLabel lblModel = new JLabel("Model");
        contentPane.add(lblModel,gbc);
        JTextField textFieldModel = new JTextField();
        contentPane.add(textFieldModel,gbc);
        textFieldModel.setColumns(5);

        JLabel lblPlakaNo = new JLabel("PlakaNo");
        contentPane.add(lblPlakaNo,gbc);
        JTextField textFieldPlakaNo = new JTextField();
        contentPane.add(textFieldPlakaNo,gbc);
        textFieldPlakaNo.setColumns(5);

        JLabel lblCikisTarihiveSaati = new JLabel("Çıkış Tarihi ve Saati");
        contentPane.add(lblCikisTarihiveSaati,gbc);
        JTextField textFieldCikis = new JTextField();
//      contentPane.add(textFieldCikis);
        UtilDateModel model = new UtilDateModel();
        Properties p = new Properties();
        p.put("text.today", "Today");
        p.put("text.month", "Month");
        p.put("text.year", "Year");
        JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
        JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());
        contentPane.add(datePicker,gbc);

        textFieldCikis.setColumns(5);

        JLabel lblCikisKm = new JLabel("Çıkış Km");
        contentPane.add(lblCikisKm,gbc);
        JTextField textFieldCikisKm = new JTextField();
        contentPane.add(textFieldCikisKm,gbc);
        textFieldCikisKm.setColumns(5);

        JLabel lblDonusTarihiveSaati = new JLabel("Dönüş Tarihi ve Saati");
        contentPane.add(lblDonusTarihiveSaati,gbc);
        JTextField textFieldDonus = new JTextField();
        UtilDateModel model2 = new UtilDateModel();
        Properties p2 = new Properties();
        p2.put("text.today", "Today");
        p2.put("text.month", "Month");
        p2.put("text.year", "Year");
        JDatePanelImpl datePanel2 = new JDatePanelImpl(model2, p);

        JDatePickerImpl datePicker2 = new JDatePickerImpl(datePanel2, new DateLabelFormatter());
        contentPane.add(datePicker2,gbc);
        textFieldDonus.setColumns(5);

        JLabel lblDonusKm = new JLabel("Dönüş Km");
        contentPane.add(lblDonusKm,gbc);
        JTextField textFieldDonusKm = new JTextField();
        contentPane.add(textFieldDonusKm,gbc);
        textFieldDonusKm.setColumns(5);

        JLabel lblNeden = new JLabel("Aracın Kullanılacağı Yer-Kullanım Neden Geçici Araç");
        contentPane.add(lblNeden,gbc);
        JTextField textFieldNeden = new JTextField();
        contentPane.add(textFieldNeden,gbc);
        textFieldNeden.setColumns(5);

        JLabel lblKullanimSuresi = new JLabel("Kullanım Süresi");
        contentPane.add(lblKullanimSuresi,gbc);
        JTextField textFieldSure = new JTextField();
        contentPane.add(textFieldSure,gbc);
        textFieldSure.setColumns(5);

        JLabel lblHasarveEksiklikler = new JLabel("Hasar ve Eksiklikler");
        contentPane.add(lblHasarveEksiklikler,gbc);

        JLabel lblAractaGorulenHasarlar = new JLabel("Araçta Görülen Hasarlar veya Eksiklikler (Zincir, İstetme, İlk Yardım Çantası vs.");
        contentPane.add(lblAractaGorulenHasarlar,gbc);

        JTextField textFieldHasar = new JTextField();
        contentPane.add(textFieldHasar,gbc);
        textFieldHasar.setColumns(5);

        JButton btnNewButton = new JButton("Gönder");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String url = "jdbc:sqlserver://WIN-J53H5BK2ANC;databaseName=test;integratedSecurity=true;";
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

                Connection conn = DriverManager.getConnection(url);
                Statement st = conn.createStatement();
                String queryString = "insert into havuzAracıFormu values ('" + textFieldAdSoyad.getText() + "'," + 
                                                                          "'" + textFieldGorev.getText() + "'," +
                                                                          "'" + textFieldDepartman.getText() + "'," +
                                                                          "'" + textFieldMarka.getText() + "'," +
                                                                          "'" + textFieldModel.getText() + "'," +
                                                                          "'" + textFieldPlakaNo.getText() + "'," +
                                                                          "'" + datePicker.getJFormattedTextField().getText() + "'," +
                                                                          "'" + textFieldCikisKm.getText() + "'," +
                                                                          "'" + datePicker2.getJFormattedTextField().getText()+ "'," +
                                                                          "'" + textFieldDonusKm.getText() + "'," +
                                                                          "'" + textFieldNeden.getText() + "'," +
                                                                          "'" + textFieldSure.getText() + "'," +
                                                                          "'" + textFieldHasar.getText() + "')";
                st.executeUpdate(queryString);
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        contentPane.add(btnNewButton, BorderLayout.SOUTH);
    }

}

class DateLabelFormatter extends AbstractFormatter{

    private String datePattern = "yyyy-MM-dd";
    private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);

    @Override
    public Object stringToValue(String text) throws ParseException {

        return dateFormatter.parseObject(text);
    }

    @Override
    public String valueToString(Object value) throws ParseException {
        if(value != null){
            Calendar cal = (Calendar) value;
            return dateFormatter.format(cal.getTime());
        }
        return "";
    }

}

共有1个答案

齐英韶
2023-03-14

一旦该代码形成了一种更简单的形式,可以在没有任何瑕疵的情况下编译,那么与自定义(即您的)代码相关的堆栈跟踪的第一行是:

contentPane.add(btnNewButton, BorderLayout.SOUTH);

BorderLayout。SOUTH是一个用于边界布局的约束,但在前面的代码中我们看到了。。

contentPane.setLayout(new GridBagLayout()); // I.E. **NOT** a BorderLayout
  • 查看什么是堆栈跟踪,以及如何使用它调试应用程序错误?作为程序员,在我们的职业生涯中,我们会看到很多例外情况,我们需要学习如何解决它们,或者至少解决其中的大部分。只有当您知道如何解决大多数异常,但不确定特定代码行中发生特定异常的原因时,您才应该这样问
  • 正如一些人在评论中提到的,当有疑问时,将代码简化到显示错误的程度,但删除所有不相关的代码
  • 我调查此事的唯一原因是我特别无聊,睡不着觉。我通常会投票决定结束,然后进入下一个问题,在这个问题上,这个人可以创建一个MCVE。所以不要指望将来有人会同情你

 类似资料:
  • 这是我第一次使用Android studio,我不知道那些错误是什么意思,我看到了很多解决方案,包括将appcomat改为alpha1,将theme改为Base.theme和很多,但没有人在工作 呈现问题“在当前主题中找不到样式'Coordinator LayoutStyle'提示:尝试刷新布局。” Gradle脚本>Build.Gradle 依赖项{实现文件树(dir:'libs',includ

  • 问题内容: 因此,我作为项目需求试图将外键约束添加到数据库中,并且它第一次或在两个不同的表上运行,但是在尝试添加外键约束时,我在两个表上遇到错误。我收到的错误消息是: 错误1215(HY000):无法添加外键约束 这是我用来创建表的SQL,两个有问题的表是和。 问题答案: 要查找特定错误,请运行以下命令: 并查看该部分。 子列的数据类型必须与父列完全匹配。例如,由于是,也需要是,而不是。 另外,您

  • 我有两个组件和。每个组件都是由<code>ConstraintLayout</code>创建的。现在,我从上面的组件创建<code>WelcomeScreen</code>。 用例1:使用静态< code>ConstraintLayout(在< code>ConstraintLayout内的< code>ConstraintLayout) 结果:布局预览不显示任何内容 用例2:<代码>Welcom

  • 我有两张桌子。第一个是人口;第二个是空的。 我希望第二个有一个外键,它引用第一个中的一列。 null 架构: 填充。为空。 不能成为的外键: MySQL没有提供错误的原因;不返回: 我拥有所需的数据库权限。 我已经仔细检查了列(甚至表)是否具有相同的排序规则(字符集不适用于INT列): MySQL无法添加外键约束 MySQL:错误1215(HY000):无法添加外键约束 无法添加外键约束-MySQ

  • 我正在尝试在Laravel中创建外键,但是当我使用迁移表时,出现以下错误: 我的迁移代码如下: 优先级迁移文件 用户迁移文件 任何关于我做错了什么的想法,我现在就想知道,因为我有很多表需要创建,例如用户、客户、项目、任务、状态、优先级、类型、团队。理想情况下,我希望创建使用外键保存此数据的表,即和等。 希望有人能帮助我开始。