gpt4 book ai didi

Java 在一个 Jframe 中获取输入字符串并在另一个 Jframe 中显示

转载 作者:行者123 更新时间:2023-11-30 10:46:37 26 4
gpt4 key购买 nike

我必须在一个 JFrame 中获取字符串输入并在另一个 JFrame 中显示。我的第二个任务是在第二帧中以 1 秒的间隔以更大的字体闪烁给定的字符串。如何进行?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Input{

String hinput;
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;

private void prepareGUI(){
mainFrame = new JFrame("STRING");
mainFrame.setSize(500,100);
headerLabel = new JLabel("", JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
}

private void showTextField(){

JLabel stringlabel= new JLabel("String ", JLabel.RIGHT);
final JTextField userText = new JTextField(20);
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new mylistener());
submitButton.setActionCommand("open");
controlPanel.add(stringlabel);
controlPanel.add(userText);
controlPanel.add(submitButton);
mainFrame.setVisible(true);

}
private class mylistener implements ActionListener{
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("open")){
mainFrame.dispose();
NewJFrame nj= new NewJFrame(hinput);
}
}
}
public static void main(String args[]){
Input Inp = new Input();
Inp.prepareGUI();
Inp.showTextField();
}
}
class NewJFrame{

JFrame mainFrame;
String text;
JLabel l1;
JTextField tb1;
public NewJFrame(String t){

text=t;
mainFrame=new JFrame("STRING");
mainFrame.setSize(800,800);

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("Entered string");
tb1.setText(text);
mainFrame.add(l1);
mainFrame.add(tb1);
mainFrame.setVisible(true);
}
}

单击“提交”按钮后,我得到了回溯。错误之处请指出。

最佳答案

您可以像这样在 NewJFrame 类中实例化 tb1 来消除错误:

class NewJFrame{

JFrame mainFrame;
String text;
JLabel l1;
JTextField tb1;
public NewJFrame(String t){

text=t;
mainFrame=new JFrame("STRING");
mainFrame.setSize(800,800);

// *** must init tb1!!! ***///
JTextField tb1 = new JTextField();

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("Entered string");
tb1.setText(text);
mainFrame.add(l1);
mainFrame.add(tb1);
mainFrame.setVisible(true);
}
}

至于让在一个 JFrame 中输入的文本在另一个 JFrame 中打开,我有一个稍微修改过的解决方案。可能在一个 JPanel 的 JTextField 中输入了文本,显示在另一个 JPanel 中。为此,您可以使用以下代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;

public class SimpleGUI extends JFrame {

private final JPanel firstPanel;
private final JPanel secondPanel;
private final JButton submitButton;
private final JTextField textField;
private final JLabel secondPanelLabel;

public SimpleGUI() {

// sets the title of the JFrame
super("SimpleGUI");

setLayout(new FlowLayout());

// inits both JPanels
firstPanel = new JPanel();
secondPanel = new JPanel();

// inits empty second JLabel and adds to the secondPanel
secondPanelLabel = new JLabel();
secondPanel.add(secondPanelLabel);

// makes the secondPanel invisible for the time being
secondPanel.setVisible(false);

// inits the submit button
submitButton = new JButton("Submit");

// event-handler for submit button, will set the text in the
// secondPanelLabel to the text in the JTextField the user types
// into. It then makes the firstPanel (with the text field and button),
// invisible, and then makes the second panel visible.
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
secondPanelLabel.setText(textField.getText());
firstPanel.setVisible(false);
secondPanel.setVisible(true);
}
});

// inits the textField
textField = new JTextField(10);

// adds the button and the text field to the firstPanel
firstPanel.add(submitButton);
firstPanel.add(textField);

// adds both panels to this JFrame
this.add(firstPanel);
this.add(secondPanel);
}
}

这里是一个类,它有一个构建 SimpleGUI 的主要方法,所以您可以自己测试一下:

import javax.swing.JFrame;

public class SimpleGUITest {
public static void main(String[] args) {
SimpleGUI frame = new SimpleGUI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}

关于Java 在一个 Jframe 中获取输入字符串并在另一个 Jframe 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36441231/

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