编写一个图形用户界面的Java Application 程序

2024-12-29 02:17:17
推荐回答(1个)
回答1:

你好,按照你的要求代码如下,可以直接运行

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class SplitFrame extends JFrame {

private JTextArea textArea_1;
private JTextArea textArea;

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

public SplitFrame() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 383);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
textArea.setText(null);
textArea_1.setText(null);
}
});
button.setText("清空");
button.setBounds(195, 310, 106, 28);
getContentPane().add(button);

final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(23, 46, 207, 240);
getContentPane().add(scrollPane);

textArea = new JTextArea();
scrollPane.setViewportView(textArea);
textArea.addKeyListener(new KeyAdapter() {
public void keyReleased(final KeyEvent e) {
String str = textArea.getText();
String[] a = str.split("[ ,\n]");
Arrays.sort(a);
textArea_1.setText(null);
for (String s : a) {
if (!"".equals(s.trim())) {
textArea_1.append(s + "\n");
}
}
}
});

final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(252, 46, 207, 240);
getContentPane().add(scrollPane_1);

textArea_1 = new JTextArea();
scrollPane_1.setViewportView(textArea_1);
textArea_1.setEditable(false);

final JLabel label = new JLabel();
label.setText("在此输入");
label.setBounds(93, 22, 66, 18);
getContentPane().add(label);

final JLabel label_1 = new JLabel();
label_1.setText("即时排序");
label_1.setBounds(328, 22, 66, 18);
getContentPane().add(label_1);
}
}