0

我正在尝试在我的 Java Swing 应用程序中创建一个包含 TextArea 的新 JFrame。我已经制作了 JFrame 并将其分配给一个按钮,但是可以说在新窗口中制作一个 TextArea 似乎比我想象的要困难一些。到目前为止,我的应用程序看起来像这样:http: //i.imgur.com/FyO3gGj.jpg

这是我的代码:

public class UserInterface extends JFrame {

private JPanel contentPane;
private JTextField brugernavn;

String username = "";
String password = "";   
private JPasswordField adgangskode;

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

/**
 * Create the frame.
 */

public UserInterface() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 600, 400);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    brugernavn = new JTextField();
    brugernavn.setBounds(6, 41, 134, 28);
    contentPane.add(brugernavn);
    brugernavn.setColumns(10);

    JLabel lblBrugernavn = new JLabel("Brugernavn");
    lblBrugernavn.setBounds(22, 20, 98, 16);
    contentPane.add(lblBrugernavn);

    JLabel label = new JLabel("Adgangskode");
    label.setBounds(22, 88, 98, 16);
    contentPane.add(label);

    JButton btnFindMitSkema = new JButton("Find mit skema");
    btnFindMitSkema.setBounds(6, 175, 150, 29);
    contentPane.add(btnFindMitSkema);

    adgangskode = new JPasswordField();
    adgangskode.setBounds(6, 116, 134, 28);
    contentPane.add(adgangskode);

    JLabel titel = new JLabel("Skema-checker for Elevplan v1");
    titel.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
    titel.setBounds(151, 11, 298, 28);
    contentPane.add(titel);

    final JFrame LicensInfoFrame = new JFrame("Licens & info");

     final String GPL = "Elevplan checker version \n"
                + "Copyright (C) 2013  Philip Jakobsen \n"
                + "This program is free software: you can redistribute it and/or modify \n"
                + "it under the terms of the GNU General Public License as published by \n"
                + "the Free Software Foundation, either version 3 of the License, or \n"
                + "(at your option) any later version. \n"
                + "This program is distributed in the hope that it will be useful, \n"
                + "but WITHOUT ANY WARRANTY; without even the implied warranty of \n"
                + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the \n"
                + "GNU General Public License for more details. \n"
                + "You should have received a copy of the GNU General Public License \n"
                + "along with this program.  If not, see \n"
                + "http://www.gnu.org/licenses"; 

     final String licensinfoframeheader = "Licens & info";

    JButton btnLicensInfo = new JButton("Licens & info");
    btnLicensInfo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            LicensInfoFrame.pack();
            LicensInfoFrame.setVisible(true);

            LicensInfoFrame.setTitle(licensinfoframeheader);

        }
    });
    btnLicensInfo.setBounds(6, 245, 150, 29);
    contentPane.add(btnLicensInfo);

    final TextArea textArea = new TextArea();
    textArea.setBounds(162, 41, 428, 233);
    contentPane.add(textArea);
    textArea.setBackground(new Color(255, 255, 255));
    textArea.setEditable(false);

    btnFindMitSkema.addActionListener(new ActionListener() {

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

            username = brugernavn.getText();
            password = adgangskode.getText();
            String output = "";
            new Backend();
            try {
                output = Backend.main(username, password);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            textArea.setText(output);

        }
    });



}
    }

简而言之,我想向 JFrame 添加一个由标有“许可证和信息”的按钮触发的 TextArea。JFrame 本身工作正常但为空。

4

3 回答 3

2

切勿null对任何摆动组件使用布局。处理 GUI 不仅对您来说是一场噩梦,而且在各种平台上显示也会出现问题。你应该让内置Layouts的 Swing 来处理 GUI 渲染。您应该查看 A Visual Guide to Layout Managers以了解如何使用 Swing 中的内置布局。此外,永远不要在应用程序中
创建多个。JFrame要完成此类任务,您可以使用JOptionPaneorJDialogJInternalFrames。我建议您从头开始编写 Swing 应用程序。首先在swing 官方教程Layouts中了解GUI 组件以及如何工作. 在您进入舒适区之后,然后创建大型应用程序来巩固您的知识。

于 2013-05-08T19:01:01.217 回答
2

您应该将文本区域添加到 JScrollPane。

此外,我建议使用默认的 NetBeans Swing 编辑器之类的 Swing 编辑器,因为这些工具使界面编码变得更加容易,并允许您专注于最重要的事情。

于 2013-05-08T18:52:09.420 回答
0

您可以进一步查看 ,您可以将 JTextArea 添加到您的面板之一,使其默认设置为 setVisible(false),并使用 JButton actionListener 将其作为 setVisible(true) 为您的用户取回

于 2013-05-08T19:50:50.107 回答