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