gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:32:59 26 4
gpt4 key购买 nike

我正在尝试获取名为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com