我正在尝试在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);
}
});
}
}
简而言之,我想将一个TextArea添加到由标记为“License& info”的按钮触发的JFrame中。 JFrame本身工作正常,但是空的。
答案 0 :(得分:2)
您应该将文本区域添加到JScrollPane。
此外,我建议使用Swing编辑器,例如默认的NetBeans Swing编辑器,因为这些工具使界面编码更容易,让您专注于最重要的事情。
答案 1 :(得分:2)
永远不要对任何挥杆组件使用null
布局。处理GUI不仅是一场噩梦,而且还会在各种平台上显示问题。你应该让内置的Layouts
Swing来处理GUI渲染。您应该查看A Visual Guide to Layout Managers以了解如何在Swing中使用内置布局
此外,永远不要在应用程序中创建多个JFrame
。要完成此类任务,您可以使用JOptionPane
或JDialog
或JInternalFrames
。我建议你从头开始编写swing应用程序。首先了解Layouts
以及GUI组件在swing official tutorial的工作方式。在你进入舒适区后,然后创建大型应用程序,巩固你的知识。
答案 2 :(得分:0)
你可以take a look 此外,您可以将JTextArea添加到您的某个面板,默认情况下将其设置为setVisible(false),并使用JButton actionListener将其作为setVisible(true)将其恢复为您的用户