编写一个图形用户界面的Java Application 程序
编写一个图形用户界面的JavaApplication程序,有两个文本区和一个按钮。当在一个文本区中输入若干个英文单词时(用空格、逗号或回车作为单词之间的分隔符),另一个文...
编写一个图形用户界面的Java Application 程序,有两个文本区和一个按钮。当在一个文本区中输入若干个英文单词时(用空格、逗号或回车作为单词之间的分隔符),另一个文本区同时对输入的英文单词按字典序排序,也就是说随着输入的变化,另一个文本区不断地更新排序。单击“清空”按钮,程序将清空两个文本区中的内容
展开
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);
}
}
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |