好的,我有一个任务,我们要创建一个通用对类,然后测试它是否有效。pair 班有一定的要求必须遵守,但我们可以自由地对 test 班做我们想做的事。诚然,我有点过火了,但这只是因为玩弄想法可以帮助我学习课堂上没有涵盖的其他东西。现在,我不能展示通用类的东西,因为那部分是作业的重要部分,但至于我的测试类,我将全部展示。我正在尝试使我的第五个文本字段tf5
(myScrollPane
)。现在,我设置 keylistener 的方式是,它会将我在文本字段中输入的任何内容添加到 JScrollPanel 中,因为我正在输入它。因此,如果我键入 1、2、3、4,我会在面板上打印出 4 个数组列表(每个列表有 1 个数字)。我希望能够在文本字段中输入我想要的任何内容,并且只有在我按 Enter 后才将其传递到 JScrollPanel 中,但是对于我的生活,我只是无法弄清楚该怎么做。谁能帮我解决这个问题?这是我的代码:
package lab10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Integer.*;
import java.util.ArrayList;
public class Lab10 extends JFrame {
private Container content;
private JTextField tf1, tf2, tf3, tf4, tf5, tf6;
private JLabel lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8, lbl9, lbl10;
private Font myFont;
private JButton myBtn, myBtn2, tstb;
private JTextArea myTextArea;
private JScrollPane myScrollPane;
private KeyEvent x;
Integer I1, I2;
String S1, S2;
Double D1, D2;
ArrayList A1, A2;
Pair<Integer, Integer> myPairInt = new Pair<>(I1, I2);
Pair<String, Double> myPairStrDbl = new Pair<>(S1, D2);
Pair<ArrayList, String> myPairAryStr = new Pair<>(A1, S2);
public Lab10() {
this(640, 540, "Testing a Generic Pair");
}
public Lab10(int width, int height, String title) {
this.setVisible(true);
this.setTitle(title);
this.setSize(width, height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiComponent();
}
private void guiComponent() {
SpringLayout layout = new SpringLayout();
content = this.getContentPane();
content.setLayout(layout);
myFont = new Font("Magneto", Font.BOLD, 12);
myBtn = new JButton("Do It!");
myBtn2 = new JButton();
myBtn2.setLayout(new BorderLayout());
JLabel label1 = new JLabel("Click here to add elements to Array List");
JLabel label2 = new JLabel("(Before you click the bottom button!)");
myBtn2.add(BorderLayout.NORTH, label1);
myBtn2.add(BorderLayout.SOUTH, label2);
myBtn.addActionListener(new ButtonClick());
myBtn2.addActionListener(new ArrayListClickAdd());
lbl1 = new JLabel("Enter a string of numbers for me to count");
lbl2 = new JLabel("Now enter another string of numbers for me to count");
lbl3 = new JLabel("This is where I show you what you typed ");
lbl4 = new JLabel("Now type a String and I'll count the Ascii sum ");
lbl5 = new JLabel("Now type in a double, i.e. 2.4 or 2.3 or 5.6 ");
lbl6 = new JLabel("This is where I show you what you typed ");
lbl7 = new JLabel("Now Enter an Array List, (just a standard string of numbers) ");
lbl8 = new JLabel("Now Enter a String and I will convert it all to uppercase");
lbl9 = new JLabel("This is where I will show you what you've typed ");
lbl10 = new JLabel();
tf1 = new JTextField(8);
tf2 = new JTextField(8);
tf3 = new JTextField(8);
tf4 = new JTextField(8);
tf5 = new JTextField(8);
tf6 = new JTextField(8);
tf5.addKeyListener(new MyKeyEvent());
myTextArea = new JTextArea();
myTextArea.setColumns(12);
myTextArea.setRows(8);
myTextArea.setFont(myFont);
content.add(lbl1);
layout.putConstraint(SpringLayout.WEST, lbl1, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, lbl1, 10, SpringLayout.NORTH, content);
content.add(tf1);
layout.putConstraint(SpringLayout.WEST, tf1, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, tf1, 10, SpringLayout.SOUTH, lbl1);
content.add(lbl2);
layout.putConstraint(SpringLayout.WEST, lbl2, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, lbl2, 10, SpringLayout.SOUTH, tf1);
content.add(tf2);
layout.putConstraint(SpringLayout.WEST, tf2, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, tf2, 10, SpringLayout.SOUTH, lbl2);
content.add(lbl3);
layout.putConstraint(SpringLayout.WEST, lbl3, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, lbl3, 10, SpringLayout.SOUTH, tf2);
content.add(lbl4);
layout.putConstraint(SpringLayout.WEST, lbl4, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, lbl4, 10, SpringLayout.SOUTH, lbl3);
content.add(tf3);
layout.putConstraint(SpringLayout.WEST, tf3, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, tf3, 10, SpringLayout.SOUTH, lbl4);
content.add(lbl5);
layout.putConstraint(SpringLayout.WEST, lbl5, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, lbl5, 10, SpringLayout.SOUTH, tf3);
content.add(tf4);
layout.putConstraint(SpringLayout.WEST, tf4, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, tf4, 10, SpringLayout.SOUTH, lbl5);
content.add(lbl6);
layout.putConstraint(SpringLayout.WEST, lbl6, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, lbl6, 10, SpringLayout.SOUTH, tf4);
content.add(lbl7);
layout.putConstraint(SpringLayout.WEST, lbl7, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, lbl7, 10, SpringLayout.SOUTH, lbl6);
content.add(tf5);
layout.putConstraint(SpringLayout.WEST, tf5, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, tf5, 10, SpringLayout.SOUTH, lbl7);
content.add(lbl8);
layout.putConstraint(SpringLayout.WEST, lbl8, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, lbl8, 10, SpringLayout.SOUTH, tf5);
content.add(tf6);
layout.putConstraint(SpringLayout.WEST, tf6, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, tf6, 10, SpringLayout.SOUTH, lbl8);
content.add(lbl9);
layout.putConstraint(SpringLayout.WEST, lbl9, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, lbl9, 10, SpringLayout.SOUTH, tf6);
content.add(lbl10);
layout.putConstraint(SpringLayout.WEST, lbl10, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, lbl10, 2, SpringLayout.SOUTH, lbl9);
myScrollPane = new JScrollPane(myTextArea);
content.add(myScrollPane);
layout.putConstraint(SpringLayout.WEST, myScrollPane, 250, SpringLayout.EAST, tf4);
layout.putConstraint(SpringLayout.NORTH, myScrollPane, 1, SpringLayout.SOUTH, myBtn2);
content.add(myBtn2);
layout.putConstraint(SpringLayout.WEST, myBtn2, 3, SpringLayout.EAST, lbl7);
layout.putConstraint(SpringLayout.NORTH, myBtn2, 8, SpringLayout.SOUTH, lbl6);
content.add(myBtn);
layout.putConstraint(SpringLayout.WEST, myBtn, 10, SpringLayout.WEST, content);
layout.putConstraint(SpringLayout.NORTH, myBtn, 10, SpringLayout.SOUTH, lbl10);
}
private String upper(String y) {
return y.toUpperCase();
}
private String getAsciiSum(String z) {
int addSum = 0;
for (int i = 0; i < z.length(); i++) {
char x = z.charAt(i);
int j = (int) x;
addSum += x;
}
z = Integer.toString(addSum);
return z;
}
int sumNums(int x) {
String myNum = Integer.toString(x);
int addSum = 0;
for (int i = 0; i < myNum.length(); i++) {
addSum += Character.digit(myNum.charAt(i), 10);
}
return addSum;
}
int[] getArray(int num) {
String counter = "" + num;
int digits = counter.length();
int[] nums = new int[digits];
for (int i = digits - 1; i >= 0; i--) {
nums[i] = num % 10;
num /= 10;
}
return nums;
}
private class MyKeyEvent implements KeyListener {
public MyKeyEvent() {
}
@Override
public void keyTyped(KeyEvent ke) {
ke.setKeyCode(KeyEvent.VK_RIGHT);
if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
String arrNum = tf5.getText();
ArrayList arList = new ArrayList();
arList.add(arrNum);
myPairAryStr.setFirst(arList);
myTextArea.append(myPairAryStr.getFirst() + "\n");
tf5.setText("");
}
}
@Override
public void keyPressed(KeyEvent ke) {
}
@Override
public void keyReleased(KeyEvent ke) {
}
}
private class ArrayListClickAdd implements ActionListener {
public ArrayListClickAdd() {
}
@Override
public void actionPerformed(ActionEvent ae) {
String arrNum = tf5.getText();
ArrayList arList = new ArrayList();
arList.add(arrNum);
myPairAryStr.setFirst(arList);
myTextArea.append(myPairAryStr.getFirst() + "\n");
tf5.setText("");
}
}
private class ButtonClick implements ActionListener {
public ButtonClick() {
}
@Override
public void actionPerformed(ActionEvent ae) {
int num = Integer.parseInt(tf1.getText());
int num2 = Integer.parseInt(tf2.getText());
String num3 = tf3.getText();
double num4 = Double.parseDouble(tf4.getText());
String arrLst = tf5.getText();
String num6 = tf6.getText();
int count1 = sumNums(num);
int count2 = sumNums(num2);
String strSum = getAsciiSum(num3);
String goBig = upper(num6);
Integer int1 = new Integer(count1);
Integer int2 = new Integer(count2);
String str1 = strSum;
Double dbl2 = new Double(num4);
ArrayList arList = new ArrayList();
arList.add(arrLst);
String str2 = goBig;
myPairInt.setFirst(int1);
myPairInt.setSecond(int2);
myPairStrDbl.setFirst(str1);
myPairStrDbl.setSecond(dbl2);
myPairAryStr.setFirst(arList);
myPairAryStr.setSecond(str2);
lbl3.setText("Your numbers fromthe first field add up to : " + myPairInt.getFirst() + " and your numbers from the second field add up to : " + myPairInt.getSecond());
lbl6.setText("The Ascii sum of the first Field is : " + myPairStrDbl.getFirst() + " and you typed : " + myPairStrDbl.getSecond() + " In the Second Field");
lbl9.setText("You typed : " + myPairAryStr.getFirst() + " In the first Field,");
lbl10.setText("and : " + myPairAryStr.getSecond() + " In the Second");
myTextArea.append(myPairAryStr.getFirst() + "\n");
}
}
public static void main(String[] args) {
Lab10 test = new Lab10();
}
}