小编Ash*_*yan的帖子

基于对象属性的ArrayList排序

以下是Employee bean类.

public class Employee {
    public String name;
    public int age;

    public Employee()
    {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }
}
Run Code Online (Sandbox Code Playgroud)

我有其他EmployeeTest类,在其中我创建Employee类的对象并存储在ArrayList中.

import java.util.ArrayList;

public class EmployeeTest {
    public static void main(String[] args) 
    {
        ArrayList<Employee> empList = new ArrayList<Employee>();
        Employee emp1 = new Employee();
        emp1.setAge(15);
        emp1.setName("Employee1");
        Employee emp2 = new Employee();
        emp2.setAge(10); …
Run Code Online (Sandbox Code Playgroud)

java sorting collections

1
推荐指数
1
解决办法
2950
查看次数

类abstractButton中的addActionListener方法不能应用于给定类型

我正在尝试使用swing创建一个Calculator,但是在编译它时遇到了一个问题,这是由于错误导致类AbstractButton中的addActionListener方法无法应用于给定类型而引起的。

我的代码是

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {
    private String str = "0";
    JTextField l_tf = new JTextField(100);
    JButton button[] = new JButton[31];
    Northwindow panelNorth = new Northwindow();
    Centerwindow panelCenter = new Centerwindow();

    Calculator(String l_s) {
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add("North", panelNorth);
        getContentPane().add("Center", panelCenter);

        setSize(300, 400);
        setVisible(true);
    }

    //Northwindow

    class Northwindow extends JPanel {
        public Northwindow() {
            Dimension textf = new Dimension(50, 50);
            setLayout(new GridLayout(0, 1));
            setBackground(new Color(230, 230, 255));
            l_tf.setPreferredSize(textf);
            add(l_tf);
        }
    } …
Run Code Online (Sandbox Code Playgroud)

java swing

1
推荐指数
1
解决办法
1497
查看次数

Key Listener无法正常工作

我一直在尝试创建一个Java游戏,但我遇到了障碍,即使我只是使用print语句测试它,我也无法让java听我的任何键.根据我的理解,我已经正确实现了KeyListener,并将关键监听器添加到Applet,但它仍然无法正常工作.

我的主要课程:

import java.awt.*;
import javax.swing.*;


public class Container extends JApplet implements Runnable {
    private static final long serialVersionUID = 1L;

    public static Dimension size = new Dimension(720,560); //Size of Screen

    private static final int PIXELSIZE = 2;
    public static Dimension pixel = new Dimension(size.width/PIXELSIZE,
            size.height/PIXELSIZE); // Dimesions of screen in terms of pixels


    public static final String NAME = "Game";
    public static boolean isRunning = false;
    private Image screen;

    public static Level level;
    public static MainCharacter p1;

    public Container(){ …
Run Code Online (Sandbox Code Playgroud)

java swing keylistener

1
推荐指数
1
解决办法
133
查看次数

中断java中正常运行的线程

我试图中断正常运行的线程(未处于 sleep() 或 wait() 状态)。

在网络中浏览时,我知道中断正常运行的线程只会将标志设置为 true 并继续该过程。

代码片段是

one.java

......
......
actionperformedmethod {

if (actionCmd.equals("cancel")) {
    try {
        r1.stop();  // to two.java
    } catch (InterruptedException ex) {
        ....
        ....
    }
}
}
Run Code Online (Sandbox Code Playgroud)

two.java

.....
.....
stop method() throws InterruptedException{
        if(!(t.isInterrupted())){
            t.interrupt();
            throw new InterruptedException();
        }
}
Run Code Online (Sandbox Code Playgroud)

从 Two.java 当我抛出 InterruptedException 时,我可以在 one.java 处获取异常块,但是之后如何停止线程,因为即使在该线程似乎继续正常过程之后。

我对线程概念不熟悉,请帮忙..

java multithreading interrupted-exception

1
推荐指数
1
解决办法
6797
查看次数

java.lang.IllegalArgumentException:启动tomcat时需要'dataSource'或'jdbcTemplate'

提出关于spring和tomcat的问题.我有以下代码

BookDAOImpl.java

@Repository
public class BookDAOImpl extends NamedParameterJdbcDaoSupport implements BookDAO {

    private class BookMapper implements RowMapper<Book> {

        @Override
        public Book mapRow(ResultSet resultSet, int rowNum) throws SQLException {
            Book book = new Book();
            book.setId(resultSet.getInt("id"));
            book.setTitle(resultSet.getString("title"));
            book.setAuthor(resultSet.getString("author"));
            book.setPublisher(resultSet.getString("publisher"));
            book.setPublicationDate(resultSet.getDate("publication_date"));
            book.setIsbn(resultSet.getString("isbn"));
            book.setAvailable(resultSet.getBoolean("available"));
            return book;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

LibraryDataSource.java

@Component("dataSource")
public class LibraryDataSource extends DriverManagerDataSource {

    @Autowired
    public LibraryDataSource(@Value("${url}")String url, @Value("${user}")String username, @Value("${password}")String password) {
        super(url, username, password);
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序的context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" 
    default-autowire="byName">

    <context:annotation-config/> …
Run Code Online (Sandbox Code Playgroud)

java spring tomcat spring-mvc autowired

1
推荐指数
1
解决办法
6073
查看次数

使用 resttemplate 发布请求,但 401 未经授权

String url = "https://api.assembla.com/token?";

RestTemplate restTemplate = new RestTemplate();

MultiValueMap<String, String> body = 
        new LinkedMultiValueMap<String, String>();

body.add("client_id", "myid");
body.add("client_secret", "mysecret");
body.add("grant_type", "client_credentials");
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-type", "application/json");
HttpEntity<?> entity = new HttpEntity<Object>(body, headers);

ResponseEntity<JsonResponseType> res = 
        restTemplate.exchange(url, HttpMethod.POST, entity, JsonResponseType.class);

System.out.println("Hello....." + res);
Run Code Online (Sandbox Code Playgroud)

我收到 401 未经授权的错误,有人可以删除它吗

也用

MultiValueMap<String, String> body = 
        new LinkedMultiValueMap<String, String>();

body.add("x-api-key", "myapikey");
body.add("x-api-secret", "myapisecret");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", "application/json");
headers.set("Content-type", "application/json");

HttpEntity<?> entity = new HttpEntity<Object>(body, headers);
entity.getHeaders().setContentType(MediaType.APPLICATION_JSON);
RestTemplate …
Run Code Online (Sandbox Code Playgroud)

java spring resttemplate

1
推荐指数
1
解决办法
8866
查看次数

有阵列的麻烦,也许分裂

String realstring = "&&&.&&&&";
Double value = 555.55555;
String[] arraystring = realstring.split(".");
String stringvalue = String.valueof(value);
String [] valuearrayed = stringvalue.split(".");
System.out.println(arraystring[0]);
Run Code Online (Sandbox Code Playgroud)

对不起,如果它看起来不好 在我的手机上重写了.我一直ArrayIndexOutOfBoundsException: 0在努力System.out.println.我看了,无法搞清楚.谢谢您的帮助.

java arrays split

0
推荐指数
2
解决办法
95
查看次数

Java 语言在哪里定义是否检查 Throwable?

Exception 和 RuntimeException 都继承自 Throwable 并且没有实现任何接口来判断它们是否被检查。

哪里指定了 Exception 被检查并且 RuntimeException 未被检查?它是在语言或 JVM 中硬连线的吗?

java exception

0
推荐指数
1
解决办法
309
查看次数

在java servlet中提交响应后无法转发

我有登录JSP,它将重定向到名为DisplayData的servlet,它必须检查用户凭据并仅为注册用户显示数据.但是它会抛出一个名为"在响应提交后无法转发"的错误.这是servlet代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException
{
    HttpSession session = request.getSession(true);
    HttpSession usersession = request.getSession(true);
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String query;
    Connection conn;
    Statement stmt;
    ResultSet res;
    DatabaseConnection dbconn;

    String username="";
    String hiddenname = request.getParameter("hiddenname");

    if (hiddenname.equals("login"))
    {
        username = request.getParameter("username");
        //System.out.println(username);
        String password = request.getParameter("password");
        // System.out.println(password);
        usersession.setAttribute("uname", username);
        usersession.setAttribute("upass", password);
        Connection con = dbconnection.getCon();
        System.out.println(con);
        PreparedStatement statemt = null;
        try {
            statemt = con.prepareStatement("select User_name,Password from login_details where User_name = ? and Password …
Run Code Online (Sandbox Code Playgroud)

java jsp servlets

0
推荐指数
1
解决办法
1413
查看次数

从VB到Java Factorial Method

在VBA代码中,这是循环:

For i = 0 To 50
    sum = sum + Exp(-lambda * T) * (lambda * T) ^ i / Application.Fact(i) * X
 Next
Run Code Online (Sandbox Code Playgroud)

在Java中我转换为这样的代码:

for (int i = 0; i < 50; i++) 
{
   sum = sum + Math.exp(-lambda * T) * Math.pow(lambda * T , i) / (i*=1) * X;           
}
Run Code Online (Sandbox Code Playgroud)

但它没有用.知道如何在Java中编写Application.Fact(i)函数吗?

java vba

-2
推荐指数
1
解决办法
100
查看次数

java中的学校数据库

我想知道是否有人有时间/耐心帮我解决这个问题.基本上我所要做的就是创建一个非常简单的学生数据库系统,它似乎并不适合我.这是我的主班,学生班和科目班.

科目类

import java.util.ArrayList;//ArrayList Import.
public class Subjects
{
    /*(Public Variables)*/
    public static String subjectName;
    public static String subjectTutor;
    public static ArrayList<Student> studentList = new ArrayList<Student>();
    public static ArrayList<Student> mathsList  = new ArrayList<Student>();
    public static ArrayList<Student> excelList = new ArrayList<Student>();
    public static ArrayList<Student> javaList = new ArrayList<Student>();
    public static ArrayList<Student>classList= new ArrayList<Student>();

    public Subjects(String sName)//If statement that will select the correct
    tutor for the class
    {
        subjectName = sName;

        if (subjectName == "maths")
        {
            subjectTutor= "Jennifer";
        }
        if …
Run Code Online (Sandbox Code Playgroud)

java

-4
推荐指数
1
解决办法
1674
查看次数