gpt4 book ai didi

java - 尝试使用按钮更改 JTextFields

转载 作者:行者123 更新时间:2023-12-01 23:57:42 27 4
gpt4 key购买 nike

我试图在点击下一个按钮后获取要在 JTextFields 中显示的下一组值。我是编程新手,所以我不太确定我错过了什么。我想要发生的是,当我点击下一个按钮时,当窗口显示下一组值时,下一组值现在将显示在适当的 JTextFields 中,不幸的是,最终发生的是什么都没有。我尝试了几种不同的方法来让它发挥作用,但到目前为止还没有任何效果。我知道这不是按钮本身,因为如果我更改旁边的 actionPerformed 为 setTitle(“5000”),它将更改窗口内的标题。如有任何帮助,我们将不胜感激。

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import java.awt.*;



public class GUI extends JFrame implements ActionListener {
JButton next;
JButton previous;
JButton first;
JButton last;

private JLabel itmNum = new JLabel("Item Number: ", SwingConstants.RIGHT);
private JTextField itemNumber;
private JLabel proNm = new JLabel ("Product Name: ", SwingConstants.RIGHT);
private JTextField prodName;
private JLabel yr = new JLabel("Year Made: ", SwingConstants.RIGHT);
private JTextField year;
private JLabel unNum = new JLabel("Unit Number: ", SwingConstants.RIGHT);
private JTextField unitNumber;
private JLabel prodPrice = new JLabel("Product Price: ", SwingConstants.RIGHT);
private JTextField price;
private JLabel restkFee = new JLabel("Restocking Fee", SwingConstants.RIGHT);
private JTextField rsFee;
private JLabel prodInValue = new JLabel("Product Inventory Value", SwingConstants.RIGHT);
private JTextField prodValue;
private JLabel totalValue = new JLabel("Total Value of All Products", SwingConstants.RIGHT);
private JTextField tValue;
private double toValue;

int nb = 0;
char x = 'y';
public GUI()
{

super ("Inventory Program Part 5");
setSize(800,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();




first = new JButton("First");
previous = new JButton("Previous");
next = new JButton("Next");
last = new JButton("Last");
first.addActionListener(this);
previous.addActionListener(this);
next.addActionListener(this);
last.addActionListener(this);




Movies[] titles = new Movies[9];

titles [0] = new Movies(10001, "King Arthur", 25 , 9.99, 2004, .05);

titles [1] = new Movies(10002,"Tron", 25, 7.99, 1982, .05);

titles [2] = new Movies(10003, "Tron: Legacy",25,24.99,2010,.05);

titles [3] = new Movies(10004,"Braveheart", 25,2.50,1995,.05);

titles [4] = new Movies(10005,"Gladiator",25,2.50,2000,.05);

titles [5] = new Movies(10006,"CaddyShack SE",25,19.99,1980,.05);

titles [6] = new Movies (10007,"Hackers",25,12.50,1995,.05);

titles [7] = new Movies (10008,"Die Hard Trilogy",25,19.99,1988,.05);

titles [8] = new Movies (10009,"Terminator",25,4.99,1984,.05);

Arrays.sort (titles, DVD.prodNameComparator);






itemNumber.setText(Double.toString(titles[nb].getitemNum()));
prodName.setText(titles[nb].getprodName());
year.setText(Integer.toString(titles[nb].getYear()));
unitNumber.setText(Integer.toString(titles[nb].getunitNum()));
price.setText(Float.toString(titles[nb].getprice()));
rsFee.setText(Double.toString(titles[nb].getRestkFee()));
prodValue.setText(Double.toString(titles[nb].getprodValue()));
tValue.setText("2636");





setLayout(new GridLayout(8,4));
add(itmNum);
add(itemNumber);
add(proNm);
add(prodName);
add(yr);
add(year);
add(unNum);
add(unitNumber);
add(prodPrice);
add(price);
add(restkFee);
add(rsFee);
add(prodInValue);
add(prodValue);
add(totalValue);
add(tValue);
add(first);
add(previous);
add(next);
add(last);
setLookAndFeel();
setVisible(true);


}



public void updateFields()
{
itemNumber.getText();
prodName.getText();
year.getText();
unitNumber.getText();
price.getText();
rsFee.getText();
prodValue.getText();
tValue.getText();
}




public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
if (source == next)
{
nb++;

}

}

private void setLookAndFeel()
{
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println("couln't use the system"+ "look and feel: " + e);
}

}


}

最佳答案

您的ActionListener只有一项分配

nb = 1;

重绘。这不会使用 Movies 数组 titles 中的值更新 JTextFields。您需要明确地执行此操作。

itemNumber.setText(Double.toString(titles[nb].getItemNum()));
...

为了实现这一点,您需要将您的 Movies titles 数组设置为类成员变量,以便可以从 ActionListener 访问它>.

此外,您实际上从未更改 ActionListenernb 的值。由于它是“下一步”按钮,您可能需要增加它:

nb++;

旁注:

  • Java 使用camelcase,这将使方法 getitemNum getItemNum
  • ArrayList 可以让您更灵活地在固定大小的数组上添加电影标题。

关于java - 尝试使用按钮更改 JTextFields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15327802/

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