swing—如何连接java中不同类的按钮和文本区域

iyfamqjs  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(447)

我想做一个窗口,当你按下按钮时,它会在文本区显示不同语言的短语“我爱你”。我只是不知道如何连接按钮中的文本区域。我有三节课。我尝试了很多我可以思考的方法,我也搜索了与此相关的东西,但我找不到任何有用的

  1. import javax.swing.JFrame;
  2. import javax.swing.SwingUtilities;
  3. public class MainClass {
  4. public static void main(String[] args) {
  5. SwingUtilities.invokeLater(new Runnable() {
  6. public void run() {
  7. new MainFrame();
  8. }
  9. });
  10. }
  11. }
  1. import java.awt.BorderLayout;
  2. import java.awt.FlowLayout;
  3. import javax.swing.JFrame;
  4. import javax.swing.JScrollPane;
  5. import javax.swing.JTextArea;
  6. public class MainFrame extends JFrame {
  7. private ToolBar Tulbar = new ToolBar();
  8. private JTextArea textArea = new JTextArea();
  9. public MainFrame() {
  10. super("This window loves you");
  11. setLayout(new BorderLayout());
  12. add(Tulbar, BorderLayout.NORTH);
  13. add(new JScrollPane(textArea), BorderLayout.CENTER);
  14. setSize(600,600);
  15. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. setVisible(true);
  17. }
  18. }
  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JPanel;
  6. import javax.swing.JTextArea;
  7. public class ToolBar extends JPanel{
  8. private JButton Button1 = new JButton("Korean");
  9. private JButton Button2 = new JButton("Japanese");
  10. private JButton Button3 = new JButton("French");
  11. private JButton Button4 = new JButton("Italian");
  12. private JButton Button5 = new JButton("English");
  13. private JButton Button6 = new JButton("Tagalog");
  14. public ToolBar() {
  15. setLayout(new FlowLayout(FlowLayout.LEFT));
  16. //added buttons
  17. add(Button1);
  18. add(Button2);
  19. add(Button3);
  20. add(Button4);
  21. add(Button5);
  22. add(Button6);
  23. }
  24. public ToolBar(JTextArea frame) {
  25. Button1.addActionListener(new ActionListener() {
  26. @Override
  27. public void actionPerformed(ActionEvent e) {
  28. frame.append("Saranghae");
  29. }
  30. });
  31. Button2.addActionListener(new ActionListener() {
  32. @Override
  33. public void actionPerformed(ActionEvent e) {
  34. frame.append("Aishiteru");
  35. }
  36. });
  37. Button3.addActionListener(new ActionListener() {
  38. @Override
  39. public void actionPerformed(ActionEvent e) {
  40. frame.append("Je t\'aime");
  41. }
  42. });
  43. Button4.addActionListener(new ActionListener() {
  44. @Override
  45. public void actionPerformed(ActionEvent e) {
  46. frame.append("Ti\'amo");
  47. }
  48. });
  49. Button5.addActionListener(new ActionListener() {
  50. @Override
  51. public void actionPerformed(ActionEvent e) {
  52. frame.append("I Love You");
  53. }
  54. });
  55. Button4.addActionListener(new ActionListener() {
  56. @Override
  57. public void actionPerformed(ActionEvent e) {
  58. frame.append("Mahal Kita");
  59. }
  60. });
  61. }
  62. }
3vpjnl9f

3vpjnl9f1#

既然你打电话来 Button1.addActionListenerpublic ToolBar(JTextArea frame) 构造函数,您应该调用此构造函数来调用其中的代码。但是你却在召唤 public ToolBar() 建造师。
要解决此问题,请执行以下操作:

  1. private ToolBar Tulbar = new ToolBar();
  2. private JTextArea textArea = new JTextArea();

你应该写:

  1. private JTextArea textArea = new JTextArea();
  2. private ToolBar Tulbar = new ToolBar(textArea);

而且在 ToolBar 修复构造函数,而不是:

  1. public ToolBar() {
  2. // ... STUFF 1 ...
  3. }
  4. public ToolBar(JTextArea frame) {
  5. // ... STUFF 2 ...
  6. }

你应该写:

  1. public ToolBar(JTextArea frame) {
  2. // ... STUFF 1 ...
  3. // ... STUFF 2 ...
  4. }

  1. public ToolBar() {
  2. // ... STUFF 1 ...
  3. }
  4. public ToolBar(JTextArea frame) {
  5. this();
  6. // ... STUFF 2 ...
  7. }

请学习java命名约定并一直使用它。你可能不认为这很重要,但我保证这会让你避免犯愚蠢的错误,也会让你节省时间去改正错误。

展开查看全部

相关问题