- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个程序,该程序将在从“获得的总积分”旁边的 JComboBox 中选择名称后显示积分,并且我不知道如何使用 System.out.println("");
但我希望它显示在程序内部的 pointEarned JTextField 中,旁边是“获得的总积分:”字样,而不是在我的编译器框中。
这是我的代码:
import java.awt.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.*;
import java.awt.event.*;
public class UserHistory extends JPanel {
private JLabel jcomp1;
private JComboBox userList;
private JLabel jcomp5;
private JTextField pointsEarned;
private JLabel jcomp7;
private JList choresCompleted;
public UserHistory() {
//construct preComponents
ManageUsersGUI list = new ManageUsersGUI();
String[] userListItems = new String[list.users.size()];
String[] choresCompletedItems = {"Item 1", "Item 2", "Item 3"};
//construct components
jcomp1 = new JLabel ("User History");
userList = new JComboBox(userListItems);
userList.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
userList.removeAllItems();
for (User user: list.users) {
userList.addItem(user.getUserName());
}
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
}
});
jcomp5 = new JLabel ("Total points earned: ");
pointsEarned = new JTextField ();
userList.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myBox(e);
}
});
jcomp7 = new JLabel ("List of chores completed: ");
choresCompleted = new JList (choresCompletedItems);
//set components properties
userList.setToolTipText ("Select a user");
//adjust size and set layout
setPreferredSize (new Dimension (465, 343));
setLayout (null);
//add components
add (jcomp1);
add (userList);
add (jcomp5);
add (pointsEarned);
add (jcomp7);
add (choresCompleted);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (120, 20, 70, 25);
userList.setBounds (210, 20, 100, 25);
jcomp5.setBounds (125, 105, 140, 25);
pointsEarned.setBounds (245, 105, 100, 25);
jcomp7.setBounds (95, 140, 160, 25);
choresCompleted.setBounds (245, 145, 100, 75);
}
protected void myBox(ActionEvent e) {
if(userList.getSelectedItem() != null) {
//System.out.println(User.getPoints());
}
}
public static void main (String[] args) {
JFrame frame = new JFrame ("UserHistory");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new UserHistory());
frame.pack();
frame.setVisible (true);
ManageUsersGUI.manageUsers();
}
}
这是我的用户类:
public class User {
private String userName;
private int points = 0;
public User(String userName) {
this.userName = userName;
}
public User() {
userName = "";
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void addPoints(int chorePoints) {
points += chorePoints;
}
public int getPoints() {
return points;
}
public String toString() {
return userName + "\n";
}
}
这是我的 ManageUsersGUI 类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.JCheckBox;
public class ManageUsersGUI extends JPanel {
public static ArrayList<User> users = new ArrayList<>();
private final JLabel addNewUserLabel;
private final JTextField addNewUserTextField;
private final JLabel deleteUsersLabel;
private final JButton addButton;
private final JButton deleteButton;
private final JPanel namePanel;
/**
*
*/
public ManageUsersGUI() {
//construct components
addNewUserLabel = new JLabel ("Add new User here:");
addNewUserTextField = new JTextField (0);
deleteUsersLabel = new JLabel ("Select which User(s) you would like to delete:");
addButton = new JButton ("Add");
deleteButton = new JButton ("Delete");
namePanel = new JPanel();
namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.Y_AXIS));
//set components properties
addNewUserTextField.setToolTipText ("Enter name and click on Add button.");
addButton.setToolTipText ("Click here to Add new user.");
deleteButton.setToolTipText ("Click here to delete User(s) selected.");
//adjust size and set layout
setPreferredSize (new Dimension (580, 485));
setLayout (null);
//add components
add (addNewUserLabel);
add (addNewUserTextField);
add (deleteUsersLabel);
add (namePanel);
add (addButton);
add (deleteButton);
//set component bounds (only needed by Absolute Positioning)
addNewUserLabel.setBounds (85, 130, 120, 25);
addNewUserTextField.setBounds (235, 130, 125, 25);
deleteUsersLabel.setBounds (135, 225, 281, 25);
addButton.setBounds (385, 130, 100, 25);
namePanel.setBounds(225, 270, 140, 0);
deleteButton.setBounds (230, 335, 100, 25);
addButton.addActionListener(new AddButtonListener());
deleteButton.addActionListener(new DeleteButtonListener());
}
/**
*
* @return
*/
public ArrayList<User> getUser() {
return users;
}
private class AddButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String text = addNewUserTextField.getText();
users.add(new User(text));
// Display the changes.
JOptionPane.showMessageDialog(null, text + " has been added.");
JCheckBox nameCheckBox = new JCheckBox();
nameCheckBox.setText(addNewUserTextField.getText());
namePanel.add(nameCheckBox);
namePanel.setBounds(225, 270, 140, namePanel.getHeight() + 25);
deleteButton.setBounds(230, deleteButton.getY() + 25, 100, 25);
JFrame frame = (JFrame) getRootPane().getParent();
frame.setSize(frame.getWidth(), frame.getHeight() + 25);
frame.pack();
}
}
private class DeleteButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
for(Component component : namePanel.getComponents()) {
if(component instanceof JCheckBox) {
if(((JCheckBox)component).isSelected())
namePanel.remove(component);
}
}
namePanel.revalidate();
namePanel.repaint();
}
}
public static void main (String[] args) {
JFrame frame = new JFrame ("AddUsersPanel");
frame.setTitle("Manage Users");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new ManageUsersGUI());
frame.pack();
frame.setVisible (true);
}
// in ManageUsersGUI
public static void manageUsers() {
JFrame frame = new JFrame ("AddUsersPanel1");
frame.setTitle("Manage Users");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new ManageUsersGUI());
frame.pack();
frame.setVisible (true);
}
}
最佳答案
您似乎有一个 JTextField
、pointsEarned
,这似乎就是您想要用来打印值的内容。
如果你看一下JavaDocs for JTextField
,你会发现一个 setText
方法,它接受一个 String
因此,问题实际上变成了“如何将 int
转换为 String
?”。好吧,有很多方法......
Integer.toString
String.valueOf
NumberFormat.getNumberInstance
""+ intValue
您将使用哪个取决于您想要实现的目标。
protected void myBox(ActionEvent e) {
Object obj = userList.getSelectedItem();
if(obj != null) {
if (obj instanceof User) {
User user = (User)obj;
pointsEarned.setText(NumberFormat.getNumberInstance().format(user.getPoints())
}
}
}
因此,在花了一些时间研究代码之后,似乎出现了许多问题。
一、静态的依赖
public class ManageUsersGUI extends JPanel {
public static ArrayList<User> users = new ArrayList<>();
static
不是跨对象通信机制,此信息应在需要时传递到 ManageUsersGUI
的实例,或者应生成并返回列表,具体取决于你想要实现什么。
其次,您只需使用用户名填充JComboBox
,虽然这是可以完成的并且有多种方法可以管理它,但只需添加User 会更简单
直接反对它,这样就解决了将两个同名用户添加到列表中时发生的问题
for (User user: list.users) {
userList.addItem(user);
}
您还应该使用泛型来定义您的异常...
private JComboBox<User> userList;
这意味着 addItem
将仅接受 User
的实例,而 getSelectedItem
将返回 User
而不是 >对象
JComboBox
应该在发生变化时从 List
中填充,而不是在弹出窗口可见时填充,您会发现用户体验要好得多。
您还应该使用模式对话框,从用户那里收集少量信息,从长远来看,这将使您的代码更易于管理
看看:
了解更多详情
关于java - 从 JComboBox 中选择名称后,如何获得积分以显示在我的程序上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34282409/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
所以我正在开发一个黑 jack 程序,但我有点卡住了。我会警告大家,我对编程真的很陌生,而且,我正在项目中期......所以有一些松散的结局和未使用的变量,以及一些不必要的逻辑(用于测试),但这就是我
我正在尝试创建一个可用作 OpenGL 测试工具的示例程序。到目前为止,我的那个似乎可以工作,但似乎忽略了通过统一变量 MVPMatrix 传递的 MVP 矩阵。当我添加代码以读回制服并检查它是否确实
感谢您帮助我,这是有关我的代码的部分。 printf("Thank you, now please enter the logic gate"); scanf("%s", &C); if (C ==
public static void ejemplosString(String palabra){ char[] letras = palabra.toCharArray();
所以,我有一个 php 应用程序,通过 cgi 和 nginx 运行。我有一个 .jar 程序,用于在条形码打印机(Zebra)上打印条形码,猜猜看是什么!。 我的 php 应用程序使用 exec()
我遇到的唯一问题是 getAll() 方法,它似乎在 PersonnelController 类中的位置立即运行。我也曾在其他很多地方尝试过,但都没有成功。 setAll() 方法看起来不错,我已经测
我是一名优秀的程序员,十分优秀!