关于windowbuilder的一些例外

时间:2018-03-19 14:06:34

标签: java swing

我刚刚开始使用windowbuilder来学习Java的GUI,当这些问题发生时,我感到很茫然......还有更好的可视化工具来学习Java的GUI吗?

Here is the code:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import java.awt.Toolkit;
import java.awt.Window.Type;
import java.awt.Rectangle;

public class Calculator
{

    private JFrame frame;
    private JTextField textField_3;
    private JTextField textField_1;
    private JTextField textField_2;
    private final Action action1 = new SwingAction();
    private final Action action2 = new SwingAction_1();

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

    /**
     * Create the application.
     */
    public Calculator() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(new Rectangle(0, 20, 0, 0));
        frame.setForeground(new Color(0, 0, 0));
        frame.setType(Type.POPUP);
        frame.setTitle("\u826F\u5FC3\u8F6F\u4EF6");
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage("F:\\\u56FE\u7247\\\u58C1\u7EB8\\Win10\u7CBE\u7F8E\u58C1\u7EB8\u2014\u6536\u85CF\\91592-106.jpg"));
        frame.getContentPane().setForeground(Color.GREEN);
        frame.setBounds(100, 100, 905, 557);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel label1 = new JLabel("\u674E\u54E5\u8BA1\u7B97\u5668\u4E4B\u7CBE\u7B80\u7248");
        label1.setBackground(new Color(0, 255, 102));
        label1.setForeground(new Color(0, 51, 255));
        label1.setFont(new Font("华文楷体", Font.BOLD, 24));
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        label1.setBounds(276, 22, 333, 57);
        frame.getContentPane().add(label1);

        JLabel label2 = new JLabel("\u7B2C\u4E00\u4E2A\u6570");
        label2.setFont(new Font("华文楷体", Font.BOLD, 20));
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        label2.setForeground(new Color(51, 0, 255));
        label2.setBounds(99, 129, 145, 27);
        frame.getContentPane().add(label2);

        JLabel label3 = new JLabel("\u8FD0\u7B97\u7B26");
        label3.setForeground(new Color(51, 0, 255));
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        label3.setFont(new Font("华文楷体", Font.BOLD, 20));
        label3.setBounds(394, 132, 89, 21);
        frame.getContentPane().add(label3);

        JLabel label4 = new JLabel("\u7B2C\u4E8C\u4E2A\u6570");
        label4.setHorizontalAlignment(SwingConstants.CENTER);
        label4.setForeground(new Color(51, 0, 255));
        label4.setFont(new Font("华文楷体", Font.BOLD, 20));
        label4.setBounds(634, 130, 120, 25);
        frame.getContentPane().add(label4);

        JLabel label = new JLabel("\u7ED3\u679C");
        label.setForeground(new Color(51, 0, 255));
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setFont(new Font("华文楷体", Font.BOLD, 20));
        label.setBounds(302, 277, 58, 27);
        frame.getContentPane().add(label);

        textField_3 = new JTextField();
        textField_3.setBounds(370, 276, 145, 34);
        frame.getContentPane().add(textField_3);
        textField_3.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(126, 175, 97, 34);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(651, 175, 97, 34);
        frame.getContentPane().add(textField_2);

        String[] a = { "+", "-", "*", "/" };
        @SuppressWarnings({ "rawtypes", "unchecked" })
        JComboBox comboBox = new JComboBox(a);
        comboBox.setFont(new Font("宋体", Font.PLAIN, 13));
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"     +", "      -", "      *", "      /"}));
        comboBox.setSelectedIndex(4);
        comboBox.setBounds(404, 177, 76, 29);
        frame.getContentPane().add(comboBox);

        JButton button1 = new JButton("\u8BA1\u7B97");
        button1.setForeground(new Color(51, 102, 255));
        button1.setFont(new Font("华文楷体", Font.BOLD, 20));
        button1.setBounds(238, 391, 97, 23);
        frame.getContentPane().add(button1);

        JButton button2 = new JButton("\u9000\u51FA");
        button2.setAction(action2);
        button2.setForeground(new Color(51, 0, 255));
        button2.setFont(new Font("华文楷体", Font.BOLD, 20));
        button2.setBounds(519, 391, 97, 23);
        frame.getContentPane().add(button2);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        mnFile.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));
        mnFile.setForeground(new Color(0, 0, 0));
        mnFile.setHorizontalAlignment(SwingConstants.CENTER);
        menuBar.add(mnFile);

        JMenuItem mntmQuit = new JMenuItem("quit");
        mntmQuit.setAction(action1);
        mntmQuit.setHorizontalAlignment(SwingConstants.LEFT);
        mnFile.add(mntmQuit);
    }
    private class SwingAction extends AbstractAction {
        public SwingAction() {
            putValue(NAME, "quit");
            putValue(SHORT_DESCRIPTION, "Some short description");
        }
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
    private class SwingAction_1 extends AbstractAction {
        public SwingAction_1() {
            putValue(NAME, "SwingAction_1");
            putValue(SHORT_DESCRIPTION, "Some short description");
        }
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}

例外可以列举如下,我想知道我的代码发生了什么,为什么windowbuilder提供的代码有这些问题..

    java.lang.IllegalArgumentException: setSelectedIndex: 4 out of bounds
    at java.desktop/javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at com.indi.imagedeal.Calculator.initialize(Calculator.java:137)
    at com.indi.imagedeal.Calculator.<init>(Calculator.java:63)
    at com.indi.imagedeal.Calculator$1.run(Calculator.java:49)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.desktop/java.awt.EventQueue.access$500(Unknown Source)
    at java.desktop/java.awt.EventQueue$3.run(Unknown Source)
    at java.desktop/java.awt.EventQueue$3.run(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

"id"["uri"]的行for会引发错误,因为您尝试仅使用4个元素访问数组的第5个元素。

请注意,perfis = {} for chave, valor in deputados.items(): print(valor) print(chave) resposta = requests.get(valor) answer = resposta.json() perfis.setdefault(chave, {})['uri'] = answer['dados']['uri'] perfis.setdefault(chave, {})['nomeCivil'] = answer['dados']['nomeCivil'] perfis.setdefault(chave, {})['nomeEleitoral'] = answer['dadultimoStatusos']['nomeEleitoral'] perfis.setdefault(chave, {})['siglaPartido'] = answer['dadultimoStatusos']['siglaPartido'] perfis.setdefault(chave, {})['siglaUf'] = answer['dadultimoStatusos']['siglaUf'] perfis.setdefault(chave, {})['urlFoto'] = answer['dadultimoStatusos']['urlFoto'] perfis.setdefault(chave, {})['telefone'] = answer['ultimoStatus/gabinete']['telefone'] perfis.setdefault(chave, {})['email'] = answer['ultimoStatus/gabinete']['email'] perfis.setdefault(chave, {})['sexo'] = answer['ultimoStatus/gabinete']['sexo'] perfis.setdefault(chave, {})['dataNascimento'] = answer['ultimoStatus/gabinete']['dataNascimento'] 始终为0。因此,最大可访问元素为3(comboBox.setSelectedIndex(4);