- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下是多文件项目的部分代码。我只发布这段代码,因为我不清楚为什么 setText 方法不起作用。并未包含此项目的所有代码。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class Controller extends JFrame implements ActionListener,ListSelectionListener
{
private JButton addButton;
private JButton deleteButton;
private JButton viewDetailsButton;
private JPanel btnsPnl = new JPanel();
private JPanel mainPnl = new JPanel();
private JPanel dataPnl = new JPanel();
private JPanel listPnl = new JPanel();
private JLabel loanIdLabel = new JLabel("LoanId");
private JLabel loanName = new JLabel("Loan Name");
private JLabel loanLender = new JLabel("Lender");
private JLabel loanAmount = new JLabel("Loan Amount");
private JTextField loanIdFld = new JTextField(15);
private JTextField loanNameFld = new JTextField(15);
private JTextField lenderFld = new JTextField(15);
private JTextField loanamountFld = new JTextField(15);
private DefaultListModel dlm = new DefaultListModel();
private JList jlst=new JList(dlm);
private loansModel model;
private loansView loansView;
private int selectedListIndex;
private ListSelectionModel listSelectionModel;
public loansController(loansModel mo)
{
addButton = new JButton("Add Loan");
deleteButton = new JButton("Delete Loan");
viewDetailsButton = new JButton("View Details of all Loans");
model = mo;
loansView = new loansView(mo);
}
/* Builds GUI for the controller component of the
* Model-View-Controller implementation
*/
public void buildGUI()
{
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx=0;
gbc.gridy=0;
dataPnl.setLayout(new GridLayout(4,2));
dataPnl.add(loanIdLabel);
dataPnl.add(loanIdFld);
dataPnl.add(loanName);
dataPnl.add(lenderFld);
dataPnl.add(authorLabel);
dataPnl.add(authorFld);
dataPnl.add(priceLabel);
dataPnl.add(loanamountFld);
gbl.setConstraints(dataPnl, gbc);
mainPnl.add(dataPnl);
Dimension listDim = new Dimension(50,90);
jlst.setPreferredSize(listDim);
gbc.gridx = 0;
gbc.gridy = 1;
gbl.setConstraints(listPnl,gbc);
listPnl.add(jlst);
listSelectionModel= jlst.getSelectionModel();
listSelectionModel.addListSelectionListener(new loansController(model));
gbc.gridx=1;
gbc.gridy=1;
gbc.gridwidth=2;
gbl.setConstraints(btnsPnl,gbc);
addButton.addActionListener(this);
deleteButton.addActionListener(this);
viewDetailsButton.addActionListener(this);
btnsPnl.add(addButton);
btnsPnl.add(deleteButton);
btnsPnl.add(viewDetailsButton);
mainPnl.add(listPnl);
mainPnl.add(btnsPnl);
getContentPane().add(mainPnl);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,400);
setVisible(true);
}
/* Handles events coming from add,delete, and view Details
* buttons */
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==addButton)
{
String bId = loanIdFld.getText();
String titFld = lenderFld.getText();
String authFld = authorFld.getText();
String priFld = loanamountFld.getText();
boolean isAnumber;
isAnumber = isNumeric(priFld);
if (!isAnumber)
{
JOptionPane.showMessageDialog(null,
"Enter the correct loan amount",
"Error: Price entry",
JOptionPane.INFORMATION_MESSAGE);
priFld = loanamountFld.getText();
isAnumber = isNumeric(priFld);
}
else
{
Loan loanItem;
loanItem = new Loan(bId, titFld,
authFld,Double.parseDouble(priFld));
model.addLoan(loanItem);
dlm.addElement(bId);
setClearTextFld();
}
}
else if (ae.getSource()== deleteButton)
{
model.deleteLoan(model.getSelectedListIndex());
dlm.removeElementAt(model.getSelectedListIndex());
setClearTextFld();
}
else if (ae.getSource()==viewDetailsButton)
{
loansView.buildGUI();
}
}
/* This method clears the text field after the action Performed
* method for the add and delete Buttons
*/
public void setClearTextFld()
{
loanIdFld.setText("");
lenderFld.setText("");
authorFld.setText("");
loanamountFld.setText("");
}
/* This value changed method gets the selected item in the JList
* for performing the
*/
public void valueChanged(ListSelectionEvent e)
{
listSelectionModel = (ListSelectionModel)e.getSource();
int maxselectedListIndex = listSelectionModel.getMaxSelectionIndex();
int minselectedListIndex = listSelectionModel.getMinSelectionIndex();
System.out.println(selectedListIndex);
if (!listSelectionModel.isSelectionEmpty())
{
for (int i = minselectedListIndex; i <= maxselectedListIndex; i++) {
if (listSelectionModel.isSelectedIndex(i))
{
selectedListIndex=i;
System.out.println("inside valueChanged. selectedListIndex");
System.out.println(selectedListIndex);
model.setSelectedListIndex(i);
Loan selectedLoan = model.getLoan(i);
System.out.println(selectedLoan.getTitle().toString());
System.out.println(selectedLoan.getId());
System.out.println(selectedLoan.getAuthor());
System.out.println(selectedLoan.getTitle());
System.out.println((selectedLoan.getPrice()));
// lenderFld.setText(selectedLoan.getTitle().toString());
// authorFld.setText(selectedLoan.getAuthor().toString());
// loanamountFld.setText(Double.toString(selectedLoan.getPrice()));
loanIdFld.setText("1234");
lenderFld.setText("Test LoaN NAME");
authorFld.setText("Test lender");
loanamountFld.setText("Test loan amount");
}
}
}
}
}
我想要的是当有人单击 GridBagLayout 的 Loanid 实例时,会显示有关贷款的详细信息。我可以看到 valueChanged() 被称为“inside valueChanged.selectedListIndex”,但被打印,但 LoanIdFld.setText("1234") 不会用 1234 更新 LoadId 文本字段。1234 是我插入的随机数,以确定是否可以显示但也未显示。有些函数这里没有定义,但是没有语法错误。
最佳答案
我的猜测:具有上面调用的方法(您设置其 JTextFields 的文本)的贷款 Controller 对象与用户可视化的贷款 Controller 对象不同。
但在没有更多信息的情况下,这只是猜测。
编辑 1
以下证据证明我可能是对的:
listSelectionModel.addListSelectionListener(new loansController(model));
您正在创建一个新贷款 Controller 对象作为监听器,而不是使用this:
listSelectionModel.addListSelectionListener(this);
编辑2
现在,通过下面的几乎 SSCCE 代码(对于真正的 SSCCE 来说太长,但足够短),我已经证明我的猜测实际上是正确的,您正在设置错误对象的文本,而不是一个不正确的对象的文本。可视化。如果您找到注释行,请注释掉一行并取消注释另一行以查看行为的变化:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class loansController extends JFrame implements ActionListener,
ListSelectionListener {
private JButton addButton;
private JButton deleteButton;
private JButton viewDetailsButton;
private JPanel btnsPnl = new JPanel();
private JPanel mainPnl = new JPanel();
private JPanel dataPnl = new JPanel();
private JPanel listPnl = new JPanel();
private JLabel loanIdLabel = new JLabel("LoanId");
private JLabel loanName = new JLabel("Loan Name");
private JTextField loanIdFld = new JTextField(15);
private JTextField lenderFld = new JTextField(15);
private JTextField loanamountFld = new JTextField(15);
private DefaultListModel dlm = new DefaultListModel();
private JList jlst = new JList(dlm);
private int selectedListIndex;
private ListSelectionModel listSelectionModel;
public loansController() {
addButton = new JButton("Add Loan");
deleteButton = new JButton("Delete Loan");
viewDetailsButton = new JButton("View Details of all Loans");
}
public void buildGUI() {
String[] data = {"one", "two", "three", "four"};
for (String datum : data) {
dlm.addElement(datum);
}
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
dataPnl.setLayout(new GridLayout(4, 2));
dataPnl.add(loanIdLabel);
dataPnl.add(loanIdFld);
dataPnl.add(loanName);
dataPnl.add(lenderFld);
dataPnl.add(loanamountFld);
gbl.setConstraints(dataPnl, gbc);
mainPnl.add(dataPnl);
Dimension listDim = new Dimension(50, 90);
jlst.setPreferredSize(listDim);
gbc.gridx = 0;
gbc.gridy = 1;
gbl.setConstraints(listPnl, gbc);
listPnl.add(jlst);
listSelectionModel = jlst.getSelectionModel();
// ****** here, comment one line and uncomment the other
listSelectionModel.addListSelectionListener(new loansController());
// !! listSelectionModel.addListSelectionListener(this);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbl.setConstraints(btnsPnl, gbc);
addButton.addActionListener(this);
deleteButton.addActionListener(this);
viewDetailsButton.addActionListener(this);
btnsPnl.add(addButton);
btnsPnl.add(deleteButton);
btnsPnl.add(viewDetailsButton);
mainPnl.add(listPnl);
mainPnl.add(btnsPnl);
getContentPane().add(mainPnl);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 400);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == addButton) {
} else if (ae.getSource() == deleteButton) {
} else if (ae.getSource() == viewDetailsButton) {
}
}
/*
* This method clears the text field after the action Performed method for
* the add and delete Buttons
*/
public void setClearTextFld() {
loanIdFld.setText("");
lenderFld.setText("");
loanamountFld.setText("");
}
public void valueChanged(ListSelectionEvent e) {
listSelectionModel = (ListSelectionModel) e.getSource();
int maxselectedListIndex = listSelectionModel.getMaxSelectionIndex();
int minselectedListIndex = listSelectionModel.getMinSelectionIndex();
System.out.println(selectedListIndex);
if (!listSelectionModel.isSelectionEmpty()) {
for (int i = minselectedListIndex; i <= maxselectedListIndex; i++) {
if (listSelectionModel.isSelectedIndex(i)) {
selectedListIndex = i;
System.out.println("inside valueChanged. selectedListIndex");
System.out.println(selectedListIndex);
loanIdFld.setText("1234");
lenderFld.setText("Test LoaN NAME");
loanamountFld.setText("Test loan amount");
}
}
}
}
public static void main(String[] args) {
loansController controller = new loansController();
controller.buildGUI();
}
}
请注意,这是您应该做的:简化您的代码,直到隔离问题,因为它可以让您更轻松地解决问题,如果失败,那么如果您发布此可编译的可运行代码,它会让 我们帮助您更轻松地解决问题。此过程称为创建 SSCCE 或 Short, Self Contained, Correct (Compilable), Example
编辑3
更好的 SSCCE:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
@SuppressWarnings("serial")
public class loansController extends JFrame implements ListSelectionListener {
private JPanel mainPnl = new JPanel();
private JPanel dataPnl = new JPanel();
private JPanel listPnl = new JPanel();
private JTextField loanIdFld = new JTextField(15);
private JTextField lenderFld = new JTextField(15);
private JTextField loanamountFld = new JTextField(15);
private DefaultListModel dlm = new DefaultListModel();
private JList jlst = new JList(dlm);
private ListSelectionModel listSelectionModel;
public void buildGUI() {
String[] data = {"one", "two", "three", "four"};
for (String datum : data) {
dlm.addElement(datum);
}
dataPnl.setLayout(new GridLayout(4, 2));
dataPnl.add(loanIdFld);
dataPnl.add(lenderFld);
dataPnl.add(loanamountFld);
mainPnl.add(dataPnl);
Dimension listDim = new Dimension(50, 90);
jlst.setPreferredSize(listDim);
listPnl.add(new JScrollPane(jlst));
listSelectionModel = jlst.getSelectionModel();
// ****** here, comment one line and uncomment the other
// !! listSelectionModel.addListSelectionListener(new loansController());
listSelectionModel.addListSelectionListener(this);
mainPnl.add(listPnl);
getContentPane().add(mainPnl);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void valueChanged(ListSelectionEvent e) {
System.out.println("inside valueChanged. ");
loanIdFld.setText("1234");
lenderFld.setText("Test LoaN NAME");
loanamountFld.setText("Test loan amount");
}
public static void main(String[] args) {
new loansController().buildGUI();
}
}
关于swing - JTextField 的 setText 方法不会更新 JTextField 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7061172/
//java swing 新手,需要帮助获取第一个 Jtextfield 中的文本以显示在//第二个 jtextfield 中???我很年轻,刚刚开始接触java,需要一些帮助。下面是我已经完成的代码
我正在制作一个用户界面,其中有两个 jtextfields。我想要的是当用户输入第一个jtextfield 它应该以 1 的增量镜像在第二个 jtextfield 中,并且如果用户在第二个 Jtext
我正在设计许可证激活部分。它在 Java Swing 中有两个文本字段。 Java代码: jTextField1.setDocument(new JTextFieldLimit(8)); jTex
我需要将文本从 JFrame 中的 JTextField 传递到另一个 JFrame 中的第二个 JTextField。 GUI 是使用 GUI Editor netbeans 6.9 创建的。 最佳
在从另一个 JTextField 读取一个 String 后,在另一个类中更新一个 JTextField 时遇到一些问题。这是有问题的方法: public JTextField buyVowel(pl
我正在制作身份验证 GUI,它应该包含 2 个文本字段,用户名 JTextField 和密码 JPasswordField。我想让密码字段位于用户名字段下方。我目前的代码如下: public clas
当鼠标在该文本字段中单击时,我需要让该程序清除文本字段中的文本。我尝试了一些方法,但还没有一个对我有用。 完整代码如下: public class TimerClassPanel extends JF
我目前正在学习如何在 Java 中使用 Swing。将 JTextFields、JLabels 或 JButtons 添加到 JPanel 时,我通常用 4 行来完成,如下所示: gbc.gridx
我不确定之前是否已经回答过这个问题;我尝试搜索看看是否有,但没有成功。 我正在做一项家庭作业,并且必须有两个 JTextField:一个接受输入,第二个反转显示给定的输入。例如,如果有人输入 1234
如何将内容从一个 JTextField 逐个字符复制到另一个不可编辑的 JTextField? 第一个 JTextField 中输入的每个字符都应动态显示在第二个 JTextField 中。 最佳答案
我想找到jtextfields(sht1到sht13)的总和并将结果放入llog jtextfield中,但如果我不在每个jtextfield(sht1到sht13)中放入值,它就不起作用 我如何修复
如何验证文本字段在 Swing 中仅输入 10 位数字的手机号码? 我有三个文本字段。 姓名 联系方式 没有。对于电子邮件 我想点击提交按钮,然后检查姓名、联系方式和电子邮件是否正确。我还想设置字符限
我已将 JPopupMenu 添加到 JTextField。不幸的是,当我从 JTextField 更改焦点时,或者当我“最小化”窗口时,JPopupMenu 仍然可见。如果 JTextField 失
我的 JTextField 遇到问题。我不知道如何将变量从 JTextField (位于 JFrame A 中)传递到另一个 JTextField (位于 JFrame B 中)。我尝试这样做,但它没
我正在用java创建一个小程序,用户在四个不同的四个不同文本字段中输入一些数据,单击一个按钮,然后从这些字段中获取数据并将其存储在旁边的文本字段中。 我制作了一个按钮,它实现了 ActionListe
以下是多文件项目的部分代码。我只发布这段代码,因为我不清楚为什么 setText 方法不起作用。并未包含此项目的所有代码。 import javax.swing.*;
大家好,我有一个连接到 Oracle 数据库的 Swing 应用程序,我希望一旦我在 JTextField 中键入一个值,JFrame 上的其他 JTextfield 就会加载来自数据库的后续数据,但
我有一个名为 Name 的 JTextField 和一个名为 Address 的 JTextArea。我的要求是,当用户在 JTextField 中输入名称并按 Enter 键时,光标应转到下一个文本
在 Netbeans 中,每当 JTextField、JButton 获得焦点时,边框就会被蓝色突出显示覆盖,我如何更改其颜色或完全删除它? 最佳答案 您可以通过右键单击设计 View 中的组件,选择
double B=Double.parseDouble(emp_txt2.getText()); double C=Double.parseDouble(nopay_txt3.getText(
我是一名优秀的程序员,十分优秀!