为什么框架不显示内容?

时间:2013-01-28 18:33:26

标签: java swing user-interface

我正在使用windowbuilder pro构建Gui。测试显示了我应该得到的guil的漂亮图像,但是当我运行程序时,只出现了一个小方块。当我放大那个方格时,它显示空窗口。

img from test

img after run

主要

Nya gui = new Nya();
gui.Launch();
gui.action();

Nya构造函数

public Nya() {
    setBounds(new Rectangle(0, 0, 1000, 1000));
    setAlwaysOnTop(true);
    setSize(new Dimension(900, 900));
    setPreferredSize(new Dimension(900, 900));
    setMinimumSize(new Dimension(600, 600));
    setMaximumSize(new Dimension(1000, 1000));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 831, 631);
    contentPane = new JPanel();
    contentPane.setPreferredSize(new Dimension(1000, 1000));
    setContentPane(contentPane);
    contentPane.setLayout(new MigLayout("", "[222px][17px][110px][15px][25px][26px][116px][23px][215px]", "[373px][26px][25px][25px][20px][25px][15px][25px]"));

    JPanel panel_main = new JPanel();
    panel_main.setSize(new Dimension(350, 350));
    panel_main.setPreferredSize(new Dimension(350, 350));
    panel_main.setMinimumSize(new Dimension(350, 350));
    panel_main.setMaximumSize(new Dimension(350, 350));
    contentPane.add(panel_main, "cell 0 0 3 1,grow");
    panel_main.setLayout(new MigLayout("", "[349px]", "[50px][50px][242px]"));

    JPanel panel_2 = new JPanel();
    panel_main.add(panel_2, "cell 0 0,grow");
    panel_2.setLayout(null);

    Box horizontalBox_1 = Box.createHorizontalBox();
    horizontalBox_1.setBounds(0, 0, 349, 50);
    panel_2.add(horizontalBox_1);

    Box verticalBox_3 = Box.createVerticalBox();
    horizontalBox_1.add(verticalBox_3);

    btnMoveForward = new JButton("move forward");
    btnMoveForward.setPreferredSize(new Dimension(125, 25));
    verticalBox_3.add(btnMoveForward);
    btnMoveForward.setMinimumSize(new Dimension(125, 25));
    btnMoveForward.setMaximumSize(new Dimension(125, 25));

    btnMoveBackward = new JButton("move backward");
    btnMoveBackward.setPreferredSize(new Dimension(125, 25));
    verticalBox_3.add(btnMoveBackward);
    btnMoveBackward.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnMoveBackward.setMinimumSize(new Dimension(125, 25));
    btnMoveBackward.setMaximumSize(new Dimension(125, 25));

    Component horizontalGlue_2 = Box.createHorizontalGlue();
    horizontalGlue_2.setPreferredSize(new Dimension(5, 0));
    horizontalGlue_2.setSize(new Dimension(5, 0));
    horizontalBox_1.add(horizontalGlue_2);

    mainMoption1 = new JTextField();
    mainMoption1.setMinimumSize(new Dimension(15, 22));
    mainMoption1.setMaximumSize(new Dimension(15, 22));
    mainMoption1.setPreferredSize(new Dimension(15, 22));
    horizontalBox_1.add(mainMoption1);
    mainMoption1.setColumns(10);

    Component horizontalGlue_3 = Box.createHorizontalGlue();
    horizontalGlue_3.setSize(new Dimension(5, 0));
    horizontalGlue_3.setPreferredSize(new Dimension(5, 0));
    horizontalBox_1.add(horizontalGlue_3);

    mainMoption2 = new JTextField();
    mainMoption2.setMaximumSize(new Dimension(15, 22));
    horizontalBox_1.add(mainMoption2);
    mainMoption2.setColumns(10);

    JPanel panel_1 = new JPanel();
    panel_main.add(panel_1, "cell 0 1,grow");
    panel_1.setLayout(null);

    Box horizontalBox_2 = Box.createHorizontalBox();
    horizontalBox_2.setBounds(0, 0, 349, 50);
    panel_1.add(horizontalBox_2);

    Box verticalBox_2 = Box.createVerticalBox();
    horizontalBox_2.add(verticalBox_2);

    btnMoveLeftside = new JButton("move leftside");
    btnMoveLeftside.setPreferredSize(new Dimension(125, 25));
    verticalBox_2.add(btnMoveLeftside);
    btnMoveLeftside.setMinimumSize(new Dimension(125, 25));
    btnMoveLeftside.setMaximumSize(new Dimension(125, 25));

    btnMoveRightside = new JButton("move rightside");
    btnMoveRightside.setPreferredSize(new Dimension(125, 25));
    verticalBox_2.add(btnMoveRightside);
    btnMoveRightside.setMinimumSize(new Dimension(125, 25));
    btnMoveRightside.setMaximumSize(new Dimension(125, 25));

    Component horizontalGlue = Box.createHorizontalGlue();
    horizontalGlue.setSize(new Dimension(5, 0));
    horizontalGlue.setPreferredSize(new Dimension(5, 5));
    horizontalGlue.setMinimumSize(new Dimension(5, 0));
    horizontalGlue.setMaximumSize(new Dimension(5, 0));
    horizontalBox_2.add(horizontalGlue);

    sidemotoroption1 = new JTextField();
    sidemotoroption1.setPreferredSize(new Dimension(15, 22));
    sidemotoroption1.setMinimumSize(new Dimension(15, 22));
    sidemotoroption1.setMaximumSize(new Dimension(15, 22));
    horizontalBox_2.add(sidemotoroption1);
    sidemotoroption1.setColumns(10);

    Component horizontalGlue_1 = Box.createHorizontalGlue();
    horizontalGlue_1.setSize(new Dimension(5, 0));
    horizontalGlue_1.setPreferredSize(new Dimension(5, 0));
    horizontalBox_2.add(horizontalGlue_1);

    sidemotoroption2 = new JTextField();
    sidemotoroption2.setMinimumSize(new Dimension(15, 22));
    sidemotoroption2.setMaximumSize(new Dimension(15, 22));
    horizontalBox_2.add(sidemotoroption2);
    sidemotoroption2.setColumns(10);

    JPanel panel = new JPanel();
    panel_main.add(panel, "cell 0 2,grow");
    panel.setLayout(null);

    btnTurnLeft = new JButton("turn left");
    btnTurnLeft.setBounds(0, 0, 125, 25);
    panel.add(btnTurnLeft);
    btnTurnLeft.setPreferredSize(new Dimension(125, 25));
    btnTurnLeft.setMinimumSize(new Dimension(125, 25));
    btnTurnLeft.setMaximumSize(new Dimension(125, 25));

    turnLangle = new JTextField();
    turnLangle.setBounds(170, 0, 106, 25);
    panel.add(turnLangle);
    turnLangle.setColumns(10);

    btnTurnRight = new JButton("turn right");
    btnTurnRight.setBounds(0, 38, 125, 25);
    panel.add(btnTurnRight);
    btnTurnRight.setPreferredSize(new Dimension(125, 25));
    btnTurnRight.setMinimumSize(new Dimension(125, 25));
    btnTurnRight.setMaximumSize(new Dimension(125, 25));

    btnExtraTurn = new JButton("extra turn");
    btnExtraTurn.setBounds(0, 76, 125, 25);
    panel.add(btnExtraTurn);
    btnExtraTurn.setPreferredSize(new Dimension(125, 25));
    btnExtraTurn.setMinimumSize(new Dimension(125, 25));
    btnExtraTurn.setMaximumSize(new Dimension(125, 25));

    turnRangle = new JTextField();
    turnRangle.setBounds(170, 39, 106, 25);
    panel.add(turnRangle);
    turnRangle.setColumns(10);

    turnEangle = new JTextField();
    turnEangle.setBounds(170, 77, 106, 25);
    panel.add(turnEangle);
    turnEangle.setColumns(10);

    btnMoveAngle1 = new JButton("move sideway1");
    btnMoveAngle1.setBounds(0, 114, 125, 25);
    panel.add(btnMoveAngle1);
    btnMoveAngle1.setPreferredSize(new Dimension(125, 25));
    btnMoveAngle1.setMinimumSize(new Dimension(125, 25));
    btnMoveAngle1.setMaximumSize(new Dimension(125, 25));

    btnMoveAngle2 = new JButton("move sideway2");
    btnMoveAngle2.setBounds(0, 152, 125, 25);
    panel.add(btnMoveAngle2);
    btnMoveAngle2.setPreferredSize(new Dimension(125, 25));
    btnMoveAngle2.setMinimumSize(new Dimension(125, 25));
    btnMoveAngle2.setMaximumSize(new Dimension(125, 25));

    movesideA1 = new JTextField();
    movesideA1.setBounds(170, 115, 106, 25);
    panel.add(movesideA1);
    movesideA1.setColumns(10);

    movesideA2 = new JTextField();
    movesideA2.setBounds(170, 153, 106, 25);
    panel.add(movesideA2);
    movesideA2.setColumns(10);

    btnKick = new JButton("kick");
    btnKick.setBounds(0, 198, 230, 44);
    panel.add(btnKick);
    btnKick.setMinimumSize(new Dimension(125, 25));
    btnKick.setMaximumSize(new Dimension(125, 25));
    btnKick.setPreferredSize(new Dimension(125, 25));

    btnStop = new JButton("Stop");
    btnStop.setBounds(242, 201, 95, 38);
    panel.add(btnStop);

    JLabel lblPlaceForVideo = new JLabel("Place for video");
    lblPlaceForVideo.setHorizontalTextPosition(SwingConstants.CENTER);
    lblPlaceForVideo.setHorizontalAlignment(SwingConstants.CENTER);
    contentPane.add(lblPlaceForVideo, "cell 4 0 5 1,grow");

    JPanel panel_wasd = new JPanel();
    contentPane.add(panel_wasd, "cell 0 1 1 7,grow");
    panel_wasd.setLayout(null);

    JButton btnF = new JButton("F");
    btnF.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    btnF.setBounds(61, 0, 45, 33);
    panel_wasd.add(btnF);
    btnF.setHorizontalTextPosition(SwingConstants.CENTER);

    JButton btnL = new JButton("L");
    btnL.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    btnL.setBounds(4, 50, 45, 33);
    panel_wasd.add(btnL);
    btnL.setHorizontalTextPosition(SwingConstants.CENTER);

    JButton btnB = new JButton("B");
    btnB.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    btnB.setBounds(61, 50, 45, 33);
    panel_wasd.add(btnB);
    btnB.setHorizontalTextPosition(SwingConstants.CENTER);

    JButton btnR = new JButton("R");
    btnR.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    btnR.setBounds(118, 50, 45, 33);
    panel_wasd.add(btnR);
    btnR.setHorizontalTextPosition(SwingConstants.CENTER);

    JButton btnTL = new JButton("<");
    btnTL.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent arg0) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    btnTL.setBounds(4, 0, 45, 33);
    panel_wasd.add(btnTL);
    btnTL.setHorizontalTextPosition(SwingConstants.CENTER);

    JButton btnTR = new JButton(">");
    btnTR.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    btnTR.setBounds(118, 0, 45, 33);
    panel_wasd.add(btnTR);
    btnTR.setHorizontalTextPosition(SwingConstants.CENTER);

    JButton btnNewButton = new JButton("KKKick!");
    btnNewButton.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }
    });
    btnNewButton.setBounds(4, 96, 159, 44);
    panel_wasd.add(btnNewButton);

    JButton btnU = new JButton("U");
    btnU.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    btnU.setBounds(175, 50, 45, 33);
    panel_wasd.add(btnU);
    btnU.setHorizontalTextPosition(SwingConstants.CENTER);

    JButton btnD = new JButton("D");
    btnD.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    btnD.setBounds(175, 96, 45, 33);
    panel_wasd.add(btnD);
    btnD.setHorizontalTextPosition(SwingConstants.CENTER);

    extratextField = new JTextField();
    contentPane.add(extratextField, "cell 6 1,growx,aligny center");
    extratextField.setColumns(10);

    extratextField_1 = new JTextField();
    contentPane.add(extratextField_1, "cell 6 3,grow");
    extratextField_1.setColumns(10);

    extratextField_2 = new JTextField();
    contentPane.add(extratextField_2, "cell 6 5,grow");
    extratextField_2.setColumns(10);

    extratextField_3 = new JTextField();
    contentPane.add(extratextField_3, "cell 6 7,grow");
    extratextField_3.setColumns(10);

    btnNewButton_1extra = new JButton("New button");
    contentPane.add(btnNewButton_1extra, "cell 8 1,alignx left,aligny bottom");

    btnNewButton_2extra = new JButton("New button");
    contentPane.add(btnNewButton_2extra, "cell 8 3,alignx left,aligny top");

    btnNewButton_3extra = new JButton("New button");
    contentPane.add(btnNewButton_3extra, "cell 8 5,alignx left,aligny top");

    btnNewButton_4extra = new JButton("New button");
    contentPane.add(btnNewButton_4extra, "cell 8 7,alignx left,aligny top");

    JButton btnQuit = new JButton("Quit!");
    contentPane.add(btnQuit, "cell 2 1 3 7,grow");
}

启动

public void Launch() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public class ListenCloseWdw extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            int[] command = {5,0,0,0};
            try {
                comms.sendToRobot(command);
            } catch (IOException e1) {
                System.out.println("Could not send command");
                e1.printStackTrace();
            }
            System.out.println("Quit...");
            System.exit(0);
        }
    }

1 个答案:

答案 0 :(得分:1)

从你的评论中提到。您似乎缺少将主Jpanel添加到JFrame。 它应该像下面这样。我还建议考虑在评论中提到的建议,例如选择合适的布局管理器。

JPanel panel = new JPanel();
JFrame frame = new JFrame("Test");
frame.add(panel)