【问题标题】:New JFrame with TextArea带有 TextArea 的新 JFrame
【发布时间】:2013-05-08 18:47:56
【问题描述】:

我正在尝试在我的 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 本身工作正常,但为空。

【问题讨论】:

  • 那么你的问题是什么?
  • 您需要更加具体。你有错误吗?
  • 对不起,如果我没有具体说明。我想在我的新 JFrame 中添加一个 TextArea,它由标有“许可证和信息”的按钮触发。

标签: java swing jframe


【解决方案1】:

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

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

【讨论】:

  • 它们的开销也很大,而且程序员对接口如何组合一无所知。
  • 我目前正在使用带有 Eclipse 的 WindowBuilder。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    您可以take a look 此外,您可以将 JTextArea 添加到您的面板之一,使其默认设置为 setVisible(false),并使用 JButton actionListener 将其作为 setVisible(true) 为您的用户取回

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      相关资源
      最近更新 更多