gpt4 book ai didi

Java 新手需要友好的帮助建议,在文本字段中阅读以使按钮出现/更改

转载 作者:行者123 更新时间:2023-11-30 08:19:19 24 4
gpt4 key购买 nike

首先,我今晚才刚刚开始用 Java 编码,所以如果我看起来像个新手并且我的代码很糟糕,我深表歉意,我是一个新手。基本上,当在单独的 j 框架上的文本字段中输入特定数字时,我希望图像改变颜色。因此,汽车在装载时显示为蓝色,如果按下 1,汽车就会移动并改变颜色。我的问题是我似乎无法访问文本变量来操作它。任何帮助将不胜感激,谢谢 x

public class ImageBackground extends JFrame 
{

public static void main(String[] args) throws Exception
{
JFrame F = new JFrame("CrossRoads Simulation");
TextDemo textDemo = new TextDemo();

try
{
F.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Stacy/workspace/Test/crossRoadsBackground.png")))));

}
catch(IOException e)
{
System.out.println("Image doesnt exist");
}

F.setResizable(false);
F.pack();
F.setVisible(true);


JButton button = new JButton(new ImageIcon("C:/Users/Stacy/workspace /Test/CarBlueOne.png"));
button.setSize(new Dimension(50, 50));
button.setLocation(130, 210);

button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);


F.add(button);
textDemo.createAndShowGUI();

//Make sure the new text is visible, even if there
//was a selection in the text area.
textDemo.textArea.setCaretPosition(textDemo.textArea.getDocument().getLength( ));
}
}

TextDemo.java

public class TextDemo extends JPanel implements ActionListener {
public JTextField textField;
public JTextArea textArea;
public final static String newline = "\n";
JFrame F = new JFrame("CrossRoads Simulation");
ImageBackground imageBackground = new ImageBackground();
public TextDemo() {
super(new GridBagLayout());
textField = new JTextField(20);
textField.addActionListener(this);
textArea = new JTextArea(5, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);

//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;

c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);

c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}

protected static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Add contents to the window.
frame.add(new TextDemo());

//Display the window.
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent evt) {

String text = textField.getText();
int input = Integer.parseInt(text);

if (input == 1)
{

JButton button = new JButton(new ImageIcon("C:/Users/Stacy/workspace /Test/CarRedOne.png"));
button.setSize(new Dimension(50, 50));
button.setLocation(170, 210);
}
textArea.append(text + newline);
textField.selectAll();

}
}

最佳答案

您没有任何代码将 TextDemo 中的文本变量公开给其他类。您应该从 TextField 变量中读取/修改文本。它应该类似于:

公共(public)字符串 getText() { 返回textField.getText();}

公共(public)无效setText(字符串文本){ textField.setText(文本);}

关于Java 新手需要友好的帮助建议,在文本字段中阅读以使按钮出现/更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29177991/

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