java - 寻找一种通过单击按钮来更改 JTextField 中文本的简单方法

标签 java swing button actionlistener

我正在尝试获取名为 French 的操作监听器和按钮,以将输出字段中的文本更改为 Bonjour,但当我单击该按钮时,控制台中会出现许多错误。我想用尽可能少的行来完成此操作。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;

public class TranslatorFrame {

public static void main(String[] args) {

JFrame Words = new JFrame("Greetings Translator");
Words.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Did some research to find the line so the program 
//would appear in the center of the screen

Font Font1 = new Font("SansSerif",Font.BOLD,40);
JPanel WorkSpace = new JPanel();
JTextField Instructions = new JTextField("Enter \"Hello\" Here: ");
JTextField Input = new JTextField(); 
JPanel Center = new JPanel();
Center.setBackground(Color.lightGray);
Center.add(Instructions, BorderLayout.EAST);
Center.add(Input, BorderLayout.WEST);
JTextField Output = new JTextField("");
Output.setFont(Font1);
Output.setPreferredSize(new Dimension(195, 100));
JTextField Header = new JTextField();   
Header.setPreferredSize(new Dimension(195, 100));
Input.setPreferredSize(new Dimension(150,50));
Input.setFont(Font1);
Instructions.setPreferredSize(new Dimension(375,50));
Instructions.setBackground(Color.cyan);
//set the back ground color of my Instructions field so that it wouldn't
//look so gray and boring. There don't seem to be many preset colors to choose from. 
Instructions.setFont(Font1);
Instructions.setEditable(false);
JPanel ButtonsArea = new JPanel();
ButtonsArea.setBackground(Color.lightGray);
ButtonsArea.setPreferredSize(new Dimension(300,300));
JButton French = new JButton("French");
French.setPreferredSize(new Dimension(200, 150));
French.setFont(Font1);
French.addActionListener(new ChangeListener());

JButton German = new JButton("German");
German.setPreferredSize(new Dimension(200, 150));
German.setFont(Font1);
JButton Spanish = new JButton("Spanish");
Spanish.setPreferredSize(new Dimension(200, 150));
Spanish.setFont(Font1);
ButtonsArea.add(French, BorderLayout.EAST);
ButtonsArea.add(German, BorderLayout.CENTER);
ButtonsArea.add(Spanish, BorderLayout.WEST);
ButtonsArea.add(Output,BorderLayout.SOUTH);
//Tried many different things to get the buttons and textfields 
//to have space between them but nothing seems to work. 

//Header.setSize(100,100);
Header.setFont(Font1);
Header.setText("Welcome!");
Header.setEditable(false);
WorkSpace.setBackground(Color.LIGHT_GRAY);

class ChangeListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent arg0) {

    if (arg0.getSource()==French);
    Output.setText("Bonjour!");
    }}
Words.pack();
Words.setSize(new Dimension(1000, 600));
Words.setLocationRelativeTo(null); 
WorkSpace.add(Header);
Words.add(WorkSpace, BorderLayout.NORTH);
Words.add(Center, BorderLayout.CENTER);
/*Words.add(Instructions, BorderLayout.CENTER);
Words.add(Input, BorderLayout.CENTER);*/
Words.add(ButtonsArea, BorderLayout.SOUTH);
//Words.add(Output, BorderLayout.SOUTH);
Words.setVisible(true);
    }

}

最佳答案

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TranslatorFrame {

    public static void main(String[] args) {

        JFrame Words = new JFrame("Greetings Translator");
        Words.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Did some research to find the line so the program
        // would appear in the center of the screen

        Font Font1 = new Font("SansSerif", Font.BOLD, 40);
        JPanel WorkSpace = new JPanel();
        JTextField Instructions = new JTextField("Enter \"Hello\" Here: ");
        JTextField Input = new JTextField();
        JPanel Center = new JPanel();
        Center.setBackground(Color.lightGray);
        Center.add(Instructions, BorderLayout.EAST);
        Center.add(Input, BorderLayout.WEST);
        final JTextField Output = new JTextField("");
        Output.setFont(Font1);
        Output.setPreferredSize(new Dimension(195, 100));
        JTextField Header = new JTextField();
        Header.setPreferredSize(new Dimension(195, 100));
        Input.setPreferredSize(new Dimension(150, 50));
        Input.setFont(Font1);
        Instructions.setPreferredSize(new Dimension(375, 50));
        Instructions.setBackground(Color.cyan);
        // set the back ground color of my Instructions field so that it
        // wouldn't
        // look so gray and boring. There don't seem to be many preset colors to
        // choose from.
        Instructions.setFont(Font1);
        Instructions.setEditable(false);
        JPanel ButtonsArea = new JPanel();
        ButtonsArea.setBackground(Color.lightGray);
        ButtonsArea.setPreferredSize(new Dimension(300, 300));
        final JButton French = new JButton("French");
        French.setPreferredSize(new Dimension(200, 150));
        French.setFont(Font1);
        French.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (arg0.getSource() == French)
                    ;
                Output.setText("Bonjour!");

            }
        });

        JButton German = new JButton("German");
        German.setPreferredSize(new Dimension(200, 150));
        German.setFont(Font1);
        JButton Spanish = new JButton("Spanish");
        Spanish.setPreferredSize(new Dimension(200, 150));
        Spanish.setFont(Font1);
        ButtonsArea.add(French, BorderLayout.EAST);
        ButtonsArea.add(German, BorderLayout.CENTER);
        ButtonsArea.add(Spanish, BorderLayout.WEST);
        ButtonsArea.add(Output, BorderLayout.SOUTH);
        // Tried many different things to get the buttons and textfields
        // to have space between them but nothing seems to work.

        // Header.setSize(100,100);
        Header.setFont(Font1);
        Header.setText("Welcome!");
        Header.setEditable(false);
        WorkSpace.setBackground(Color.LIGHT_GRAY);

        Words.pack();
        Words.setSize(new Dimension(1000, 600));
        Words.setLocationRelativeTo(null);
        WorkSpace.add(Header);
        Words.add(WorkSpace, BorderLayout.NORTH);
        Words.add(Center, BorderLayout.CENTER);
        /*
         * Words.add(Instructions, BorderLayout.CENTER); Words.add(Input,
         * BorderLayout.CENTER);
         */
        Words.add(ButtonsArea, BorderLayout.SOUTH);
        // Words.add(Output, BorderLayout.SOUTH);
        Words.setVisible(true);
    }

}

关于java - 寻找一种通过单击按钮来更改 JTextField 中文本的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30270889/

相关文章:

java - 为计时器创建 ActionListener

c# - WPF 重置按钮单击的焦点

facebook - 使用框计数布局使 Facebook 赞按钮评论弹出窗口锚定在右侧

java - Spring Boot在application.properties中重命名server.port

java - 使用 value 生成 jpanels 并将其添加到水平组布局

java - 阻止所有浏览器访问某个网站

java - JFileChooser 在顶部显示目录,就像在 Windows 中一样

java - 我可以只使用一种概念还是需要使用java中的继承?

java - 使用回车键在java中激活一个按钮

c# - 单击按钮更新