第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確定位 JButtons 和大小 JTextFields?

如何正確定位 JButtons 和大小 JTextFields?

富國滬深 2022-05-21 17:22:12
我正在嘗試創(chuàng)建一個密碼庫,但每當(dāng)我嘗試運(yùn)行它時,JTextFields 都非常小,并且面板有點(diǎn)被JTable.我嘗試過使用尺寸和位置,GridBag但沒有任何效果。這是給我的 IB 計(jì)算機(jī)科學(xué) IA 的。任何幫助都會很棒。    JFrame passwordVault;    JTable passwordTable;    Object[] columnNames = {"Name of Application", "Application Password", "Description"};    JTextField appName, appPass, appDesc;    JButton add, delete, update;    JLabel nameOfApp, passOfApp, descOfApp;    passwordVault = new JFrame("Password Vault");    passwordTable = new JTable();    JPanel passwordPanel = new JPanel();    DefaultTableModel tableModel = new DefaultTableModel();    tableModel.setColumnIdentifiers(columnNames);    passwordTable.setModel(tableModel);    nameOfApp = new JLabel("App Name: ");    passOfApp = new JLabel("App Password: ");    descOfApp = new JLabel("Description: ");        appName = new JTextField();    appPass = new JTextField();    appDesc = new JTextField();    add = new JButton("Add");    delete = new JButton("Delete");    update = new JButton("Update");      appName.setBounds(400, 220, 100, 25);    appPass.setBounds(400, 250, 100, 25);    appDesc.setBounds(400, 280, 100, 25);    add.setBounds(530, 220, 100, 25);    update.setBounds(530, 250, 100, 25);    delete.setBounds(530, 280, 100, 25);    JScrollPane scrollPane = new JScrollPane(passwordTable);    scrollPane.setBounds(0, 0, 1000, 200);    passwordVault.add(scrollPane);    passwordPanel.add(add);    passwordPanel.add(update);    passwordPanel.add(delete);    passwordPanel.setLayout(new GridBagLayout());
查看完整描述

1 回答

?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個贊

這是您的程序的編輯版本,它按照我認(rèn)為您嘗試的方式格式化布局:


        JFrame passwordVault;

        JTable passwordTable;

        Object[] columnNames = {"Name of Application", "Application Password", "Description"};

        JTextField appName, appPass, appDesc;

        JButton add, delete, update;

        JLabel nameOfApp, passOfApp, descOfApp;


        passwordVault = new JFrame("Password Vault");

        passwordTable = new JTable();

        JPanel passwordPanel = new JPanel();


        DefaultTableModel tableModel = new DefaultTableModel();

        tableModel.setColumnIdentifiers(columnNames);

        passwordTable.setModel(tableModel);


        nameOfApp = new JLabel("App Name: ");

        passOfApp = new JLabel("App Password: ");

        descOfApp = new JLabel("Description: ");    


        appName = new JTextField();

        appPass = new JTextField();

        appDesc = new JTextField();


        add = new JButton("Add");

        delete = new JButton("Delete");

        update = new JButton("Update");  


        // Used setPreferredSize and setLocation instead of setBounds

        appName.setLocation(new Point(400, 220));

        appName.setPreferredSize(new Dimension(100, 25));

        appPass.setLocation(new Point(400, 250));

        appPass.setPreferredSize(new Dimension(100, 25));

        appDesc.setLocation(new Point(400, 280));

        appDesc.setPreferredSize(new Dimension(100, 25));


        add.setLocation(new Point(530, 220));

        add.setPreferredSize(new Dimension(100, 25));

        update.setLocation(new Point(530, 250));

        update.setPreferredSize(new Dimension(100, 25));

        delete.setLocation(new Point(530, 280));

        delete.setPreferredSize(new Dimension(100, 25));


        JScrollPane scrollPane = new JScrollPane(passwordTable);

        scrollPane.setBounds(0, 0, 1000, 200);


        // Create a JPanel to contain the scrollPane and passwordPanel

        JPanel framePanel = new JPanel();

        framePanel.setLayout(new GridLayout(2, 1));


        framePanel.add(scrollPane);


        passwordPanel.setLayout(new GridBagLayout());


        passwordPanel.add(nameOfApp, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,

            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

        passwordPanel.add(appName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,

            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

        passwordPanel.add(passOfApp, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,

            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

        passwordPanel.add(appPass, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,

            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

        passwordPanel.add(descOfApp, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,

            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

        passwordPanel.add(appDesc, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,

            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

        passwordPanel.add(add, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,

                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

        passwordPanel.add(update, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,

                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

        passwordPanel.add(delete, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0,

                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));


        framePanel.add(passwordPanel);


        // Add the framePanel (which contains the other 2 panels) to the JFrame

        passwordVault.add(framePanel);


        passwordVault.setSize(1000,500);

        passwordVault.setLocationRelativeTo(null);

        passwordVault.setVisible(true);

因此,主要變化是:

  1. 使用setPreferredSizeandsetLocation代替ssetBounds和sJTextFieldJButton

  2. 創(chuàng)建了一個 main JPanel(稱為 it framePanel)來包含scrollPaneand passwordPanel,然后添加framePanelJFrame

JFrame它通過創(chuàng)建一個 main來幫助組織 a 中的不同元素,而不是僅僅向自身JPanel添加多個JPanels 。JFrame這樣,一個主要JPanel的決定框架的整體布局。


查看完整回答
反對 回復(fù) 2022-05-21
  • 1 回答
  • 0 關(guān)注
  • 87 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號