无法看到JTextField

时间:2016-02-01 19:55:17

标签: java swing

按钮addFinishingInformation的我的GUI窗口未显示。我有为其他按钮编写的相同方法,它们工作得很好。

我已经调试了一段时间但是却无法显示字段或任何内容。任何帮助表示赞赏。谢谢!

JButton addFinishingInformation = new JButton(new AbstractAction("Add Finishing Information") {

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        JFrame addFinishingFrame = new JFrame("Add Finishing Information");
        addFinishingFrame.setSize(700, 500);
        addFinishingFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel addFinishingPanel = new JPanel(new BorderLayout());

        JLabel finishingDateFieldLabel = new JLabel("Date:");
        finishingDateFieldLabel.setBounds(10, 40, 160, 25);
        addFinishingPanel.add(finishingDateFieldLabel);

        final JTextField finishingDateField = new JTextField(50);
        finishingDateField.setBounds(140, 40, 160, 25);
        addFinishingPanel.add(finishingDateField);

        Date todaysDateIs_2 = new Date();
        Calendar cal_2 = Calendar.getInstance();
        cal_2.setTime(todaysDateIs_2);
        String formatedTodaysDate_2 = cal_2.get(Calendar.DATE) + "/"
                + (cal_2.get(Calendar.MONTH) + 1) + "/"
                + cal_2.get(Calendar.YEAR);
        finishingDateField.setText(formatedTodaysDate_2);

        JLabel finishingChalanNoLabel = new JLabel("Chalan no. :");
        finishingChalanNoLabel.setBounds(10, 70, 160, 25);
        addFinishingPanel.add(finishingChalanNoLabel);

        final JTextField finishingChalanNumber = new JTextField(50);
        finishingChalanNumber.setBounds(140, 70, 160, 25);
        addFinishingPanel.add(finishingChalanNumber);

        addFinishingFrame.add(addFinishingPanel);
        addFinishingFrame.setLayout(null);

        JButton addFinishingInformationToDatabase = new JButton(
                new AbstractAction("Add finishing information to database") {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        Customers customer = new Customers();

                        Date todaysDate_3 = new Date();
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(todaysDate_3);
                        String formatedDate_3 = cal.get(Calendar.DATE)
                                + "/" + (cal.get(Calendar.MONTH) + 1)
                                + "/" + cal.get(Calendar.YEAR);
                        customer.addFinishingInformation(sentToFinishing.getText(),
                                formatedDate_3,
                                finishingChalanNumber.getText(),
                                quantityInWeightFinishing.getText(),
                                lengthInMeterFinishing.getText(),
                                totalShawls.getText());
                    }

                });
        addFinishingInformationToDatabase.setBounds(110, 300, 300, 70);
        addFinishingPanel.add(addFinishingInformationToDatabase);
        addFinishingFrame.setVisible(true);

    }

});

addFinishingInformation.setBounds(460, 360, 250, 70);
mainPanel.add(addFinishingInformation);

1 个答案:

答案 0 :(得分:4)

<script>
    function displayAllImages() {
        var smith = ["text instead of Smith pictures","text instead of Smith pictures2"];
        var jones = ["text instead of Jones pictures"];

        for (i=0;i<2;i++) {
          document.write("<li><p>" + smith[i] + "<p/></li>");
        }
    };
</script>
<select>
  <option value="smith">Smith</option>
  <option value="jones">Jones</option>
</select> 
<div>
    <ul>
        <script>displayAllImages();</script>
    </ul>
</div>

退出使用空布局!!!不要使用setBounds(...)!!!

Swing旨在与布局管理器一起使用。

你看不到任何东西,因为面板的大小是(0,0)所以没有什么可以画的。

这就是你使用布局管理器的原因。布局管理员会为您计算尺寸,因此您不必担心它。

阅读Layout Managers上Swing教程中的部分,了解更多信息和工作示例。