我试着保持这个简单,但仍然可以理解.如果有任何困惑,请告诉我!
我得到了这个哈希表:
private Hashtable<String, ArrayList<String>> allChannels = new Hashtable<String, ArrayList<String>>();
Run Code Online (Sandbox Code Playgroud)
我正在使用此方法插入客户端/用户:
public void connectChannel(String username, String channel) throws RemoteException{
allChannels.put(channel, new ArrayList<String>());
allChannels.get(channel).add(username);
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用这个方法,以获得一个连接到给定通道(参数)的用户的arraylist.怎么可以这样做?
@Override
public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
return **Code needed**
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:顺便说一句,连接方法得到了几个检查,但它很长,所以我没有打扰它,因为我知道它的工作很好.
编辑:因为使用return allChannels.get(channel)似乎是正确的,所以我的JList还有其他问题.我在GUILogic中使用了这些方法来运行JList.我已经使另外两个JList以相同的方式工作了.
DefaultListModel usersInChanDLM = new DefaultListModel();
public DefaultListModel getUsersInChannelAsDefaultListModel() throws RemoteException{
if(!(getChannel() == null)){
for(String a : cf.getUsersInChannel(getChannel())){
usersInChanDLM.addElement(a);
System.out.println(a);
}
}
return usersInChanDLM;
}
void updateUsersInChannelJlist(JList jList3) throws RemoteException {
usersInChanDLM.clear();
usersInChanDLM.removeAllElements();
for(Client c : cf.getClients()){
if(!(usersInChanDLM.contains(c.findName()))){
usersInChanDLM.addElement(c.findName());
}
} …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,需要能够使用java.util.date格式找到两个日期之间的差异.我正在使用SimpleDateFormat类来解析字符串输入,但它似乎解析错误.这是我的代码.我感谢任何帮助
SimpleDateFormat s = new SimpleDateFormat("yyyy/mm/dd");
Date startDate = s.parse(dateField.getText()); //input is 2012/03/10
System.out.println(START_DATE);//output is Tue Jan 10 00:03:00 MST 2012
Run Code Online (Sandbox Code Playgroud) 我有一个抽奖活动程序,曾经有很多命令和组件等.但我想通过省略许多命令或组件,如按钮或标签,使它更简单.当我试图这样做时,出现错误(42个错误),所有这些都说:
error: class, interface or enum expected.
Run Code Online (Sandbox Code Playgroud)
有人可以帮我追踪这些错误吗?
顺便说一句,这是我的代码(更新):
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.sql.*;
public class Case extends JFrame implements ActionListener
{
static Case app=new Case();
JDesktopPane desk=new JDesktopPane();
Connection cn=null;
ResultSet rs=null;
Statement stat=null;
JInternalFrame log=new JInternalFrame("WELCOME!", true,true,true,true);
JInternalFrame game=new JInternalFrame("Hi! Welcome to RAFFLE DRAW by BSIT22C");
int arr[]=new int[10];
int range=0;
JTextField gametext=new JTextField("",2);
JTextField txtuser=new JTextField("",20);
JMenuBar gamebar=new JMenuBar();
JMenu mnuf=new JMenu("File");
JMenuItem fnew=new JMenuItem("New Game");
JMenuItem fquit=new JMenuItem("Quit Game");
JMenuItem flogout=new …Run Code Online (Sandbox Code Playgroud) 因此,我必须制作一个程序,用户可以在其中输入他们为早餐,午餐和晚餐订购的每餐的数量.然后我必须显示他们每顿饭订购的金额以及每餐的总费用,税金等等.所以我在JFrame中制作了一个网格来显示结果但是我的Jlab标签一直出现错误,他们的数字是; 8,9,10,13,14,15,18,19,35,40,45.除了JLabel之外我还能用什么来显示结果吗?谢谢
import java.awt.*;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.*;
public class MealCost extends JFrame
{
// Constants
static Scanner console = new Scanner (System.in);
static final double Sales_Tax = .10;
static final double Breakfast = 5.50;
static final double DiscountB = .10;
static final double Lunch = 9.50;
static final double DiscountL = .15;
static final double Dinner = 16.50;
static final double DiscountD = .08;
public static void main(String[] args)
{
// Variables and their known values
int …Run Code Online (Sandbox Code Playgroud) 我有一个Swing界面,当我点击一个按钮时,一个线程是在无限远处创建的(属时为(true)...).
我的问题是:当我关闭主进程时,创建的线程会自动关闭吗?
我正在通过 Java 查询 MySQL 数据库,从结果集中提取数据并将其放入 DefaultTableModel,然后放入 JTable,然后放入 JScrollPane 以显示。
在 JTable 中,我通过在表中启用排序
table.setAutoCreateRowSorter(true);
Run Code Online (Sandbox Code Playgroud)
但是,如果我要排序的行中的单元格不是 String 类,那么我会收到 java.lang.ClassCastException。我的表中的数据具有以下类:
java.sql.Date
java.sql.Time
java.lang.Double
java.lang.Boolean
java.lang.String
Run Code Online (Sandbox Code Playgroud)
下面是当我尝试对 java.lang.Double 类的列进行排序时遇到的错误,然后是将结果集作为输入输出 JTable 的类。如果有人对我如何解决排序问题有任何想法,将不胜感激。
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Double `enter code here`cannot be cast to java.lang.String
at java.text.Collator.compare(Collator.java:327)
at javax.swing.DefaultRowSorter.compare(DefaultRowSorter.java:968)
at javax.swing.DefaultRowSorter.access$100(DefaultRowSorter.java:112)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:1376)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:1366)
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at javax.swing.DefaultRowSorter.sort(DefaultRowSorter.java:607)
at javax.swing.DefaultRowSorter.setSortKeys(DefaultRowSorter.java:319)
at javax.swing.DefaultRowSorter.toggleSortOrder(DefaultRowSorter.java:480)
at javax.swing.plaf.basic.BasicTableHeaderUI$MouseInputHandler.mouseClicked(BasicTableHeaderUI.java:112)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:269)
at java.awt.Component.processMouseEvent(Component.java:6508)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用junit测试用例根据工资对员工对象进行排序.
这是我的员工排序课
package day4;
import day4.Employee;
public class EmployeesInfoWithSalary {
private Employee[] employee;
private int numberOfEmployees;
public EmployeesInfoWithSalary(Employee[] employee, int numberOfEmployees) {
super();
this.employee = employee;
this.numberOfEmployees = numberOfEmployees;
}
public Employee[] getSortBasedOnSalary() {
String temp;
for (int iterator = 0; iterator < numberOfEmployees; iterator++) {
int minSalary = employee[iterator].getSalary();
int index = iterator;
for (int comparator = iterator; comparator < numberOfEmployees; comparator++) {
if (employee[comparator].getSalary() < minSalary) {
index = comparator;
minSalary = employee[comparator].getSalary();
}
}
employee[index].setSalary(employee[iterator].getSalary());
employee[iterator].setSalary(minSalary);
temp …Run Code Online (Sandbox Code Playgroud) 所以,我有一个带有菜单,工具栏和面板的JFrame.我在面板内加载图像,但由于一些奇怪的原因(至少对我来说),它们没有在面板中正确显示.有时它们从工具栏开始,有时在上面.此外,图像在底部切割.代码是完整的代码,可以编译和测试.提前致谢.
包含类的框架:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
public class SSCE extends JFrame {
private JComicPanel panel;
private JToolBar toolbar;
private JButton buttonZoom;
private JButton buttonPrev;
private JButton buttonNext;
private JMenuBar menuBar;
private Dimension dim;
private …Run Code Online (Sandbox Code Playgroud) JTable.setModel(DbUtils.resultSetToTableModel(resultaat));
Run Code Online (Sandbox Code Playgroud)
//将JTable中编辑的值更新到mysql数据库的最简单方法是什么.
-Jtable听众?上面的代码只是数据插入JTable的方式
查看JLabel的源代码我关注文本字段的可见性.我提取了必要部分来设置文本字段并检索oldValue.我认为如果String文本字段未声明为volatile,则可见性存在危险,因为firePropertyChange可能看不到之前由另一个线程保存的文本字段中检索到的oldValue.我是对的,还是我错过了什么?请注意,这不是讨论SwingUtility.
public class JLabel extends JComponent implements SwingConstants, Accessible {
...
private String text = ""; // "" rather than null, for BeanBox
...
public void setText(String text) {
String oldAccessibleName = null;
if (accessibleContext != null) {
oldAccessibleName = accessibleContext.getAccessibleName();
}
String oldValue = this.text;
this.text = text;
firePropertyChange("text", oldValue, text);
...
Run Code Online (Sandbox Code Playgroud)
提前谢谢了.