gpt4 book ai didi

java - JComboBox - 越界异常

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

我有一个小问题。我正在尝试将登录名从数据库导入到 vector ,然后将该 vector 用于 JComboBox。我下载登录名的方法:

public void loginReader (Vector<String> loginy, String tableName)
{
String query = "select login from " + tableName;

try {

Statement statement = mConnection.createStatement();
ResultSet rs = statement.executeQuery(query);
while (rs.next())
{
Vector<String> vstring = new Vector<String>();

vstring.add(rs.getString("login"));


loginy.addAll(vstring);
}
} catch (SQLException e)
{
e.printStackTrace();

}
}

这是在DatabaseManagement 类中。我创建了另一个类(GUI),这就是 JComboBox。为什么它不起作用?

package DataBase_Hospital;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Properties;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class Message extends JFrame implements ActionListener {



JButton SEND_MESSAGE;
JButton READ_MESSAGE;
public JLabel background;

JLabel NAME_LABEL;

JTextField NAME_FIELD;

JTextArea DATABASE_FIELD;
static Vector<String> loginy = new Vector<String>();

private static DatabaseManagement DATABASE;

public Message() {



setSize(290, 500);
setTitle("Message Panel");


setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JLabel background=new JLabel(new ImageIcon("/Users/Dominik/Desktop/messageFrame.png"));
add(background);


DATABASE_FIELD = new JTextArea(3,3);
JScrollPane scrollPane = new JScrollPane(DATABASE_FIELD);
scrollPane.setBounds(45, 50, 200, 200);
background.add(scrollPane);
DATABASE_FIELD.setEditable(true);


NAME_LABEL = new JLabel("Odbiorca :");
NAME_LABEL.setBounds(40, 380, 140, 20);
background.add(NAME_LABEL);

SEND_MESSAGE = new JButton();
SEND_MESSAGE.setIcon(new ImageIcon("/Users/Dominik/Desktop/sendMail.jpg"));
SEND_MESSAGE.setBounds(75, 270, 60, 60);
background.add(SEND_MESSAGE);
SEND_MESSAGE.addActionListener(this);
SEND_MESSAGE.setToolTipText("Send message");

READ_MESSAGE = new JButton();
READ_MESSAGE.setIcon(new ImageIcon("/Users/Dominik/Desktop/jwj.png"));
READ_MESSAGE.setBounds(150, 270, 60, 60);
background.add(READ_MESSAGE);
READ_MESSAGE.addActionListener(this);
READ_MESSAGE.setToolTipText("Read message");


JComboBox loginList = new JComboBox(loginy);
loginList.setSelectedIndex(loginy.capacity());
loginList.addActionListener(this);
loginList.setBounds(145, 380, 100, 20);
background.add(loginList);

}

public static void main(String[] args) {

Message window = new Message();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
DATABASE.loginReader(loginy,"uzytkownicy");
}

public void actionPerformed(ActionEvent e)
{
Object EVENT_SOURCE = e.getSource();
DATABASE = new DatabaseManagement("pacjent");

if (EVENT_SOURCE == SEND_MESSAGE)
{
DATABASE.sendMessage(DATABASE_FIELD.getText(), "uzytkownicy", NAME_FIELD.getText()) ;
}

}
}

最佳答案

使用空的 Vector 创建 JComboBox 后,将 selectedIndex 设置为 loginy.capacity () 。问题在于,虽然 Vector 的容量为 10(如 JavaDoc for the default constructor 中所述),但它的实际大小为 0。因此会出现 ArrayOutOfBoundsException。在设置 JComboBox 的选定索引之前,您应该检查 Vector 的大小。

关于java - JComboBox - 越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21243973/

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