import java.io.*;
import java.util.Properties;
public class NewClass {
public static void main(String args[]) throws IOException {
Properties p = new Properties();
p.load(new FileInputStream("DBDriverInfo.properties"));
String url=p.getProperty("url");
String user=p.getProperty("username");
String pass=p.getProperty("password");
System.out.println(url+"\n"+user+"\n"+pass);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然文件DBDriverInfo.properties文件位于同一目录中,但会引发以下异常.
Exception in thread "main" java.io.FileNotFoundException: DBDriverInfo.properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at NewClass.main(NewClass.java:7)
Run Code Online (Sandbox Code Playgroud)
javac在命令行界面中编译时,相对路径工作正常.但NetBeans中的异常引发了异常.
我正在尝试JButton使用图标和一些文本创建一个.我的问题是我希望图标左对齐并且文本右对齐(没有必要右对齐,但我不希望文本粘在图标上).
我自己无法做到这一点,我尝试了一个略有不同的解决方案.我用它iconTextGap在图标和文本之间创建了一些空间,原则上工作得很好但是当我创建多个按钮时,它们都具有最宽的宽度,图标不再在左边(除了在带有最长文本的按钮).
我提供了一个代码来证明:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.net.MalformedURLException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Main{
private JFrame frame;
private JPanel buttonPanel;
private GridBagConstraints constraints;
public Main() throws MalformedURLException{
frame = new JFrame();
buttonPanel = new JPanel();
frame.add(buttonPanel);
buttonPanel.setLayout(new GridBagLayout());
constraints = new GridBagConstraints();
constraints.insets = new Insets(5, 5, 3, 5);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 0;
String[] text = { …Run Code Online (Sandbox Code Playgroud) 给出两个集合:如何在Java中有效地比较它们?
Lists,对它们进行排序并进行比较.(Comparable)Sets并比较hashCode套装?背景:
需要进行许多比较集很小(通常每组<5个元素).
我正在为我的计算机科学课开发一个应用程序.任务是编写计算器,但不使用JTextFields或JTextAreas.我想出了一个实现的想法,KeyListener它在appletviewer中都很好用JFrame但在Google Chrome(可能还有其他浏览器)中根本不起作用.
这是我的代码片段.
//- BinaryCalc.java
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class BinaryCalc extends JApplet implements KeyListener {
private JPanel panel;
public BinaryCalc() {
super();
panel = new JPanel();
this.add(panel);
panel.addKeyListener(this);
panel.requestFocusInWindow();
}
@Override
public void init() {
JOptionPane.showMessageDialog(this, "applet");
panel.setFocusable(true);
panel.requestFocus();
}
public void keyPressed(KeyEvent e) {
JOptionPane.showMessageDialog(this, (char) e.getKeyCode());
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public …Run Code Online (Sandbox Code Playgroud) 我正在Struts2中做一个项目,我需要在URL中设置一个参数,如下面链接中的user参数.
我希望在单击表单提交按钮而不是单独的任何链接时传递此参数.我知道如何做到这一点,<s:url>但我需要创建一个链接而不是表单提交.
有人可以帮我一个代码示例如何做到这一点?我知道有一种方法可以用HTML或Struts1来做,但是如何用Struts2做呢?如果有办法做到这一点struts.xml,请举例说明.
<form action="/example/xyz.action?user=george" method="POST">
Run Code Online (Sandbox Code Playgroud) 将JComboBox组件放入一个内部时,我遇到了一种奇怪的行为GroupLayout.我已经减少了代码到下面小例子,正好特色的一个JComboBox奠定了通过GroupLayout.
观察到的行为如下:
我已经发现了什么:
这是示例代码 - 欢迎提出意见:
import java.awt.BorderLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class DummyUI_cbdiagnosis extends javax.swing.JPanel {
private javax.swing.JComboBox cbCategory;
public DummyUI_cbdiagnosis() {
initComponents();
}
private void initComponents() {
cbCategory = new JComboBox();
cbCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"a", "b", "c" }));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addComponent(cbCategory,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addComponent(cbCategory)
));
}
public static void main(String[] args) …Run Code Online (Sandbox Code Playgroud) 我想回滚事务不是在EJB内部而是在JSF托管bean内部.在EJB内部我们可以使用SessionContext.setRollBackOnly()但是我可以在托管bean中使用什么?
@Stateless
@Local(AccountLocal.class)
public class AccountBean implements AccountLocal {
public void test1() throws CustomException(){
...
}
public void test2() throws CustomException(){
...
throw new CustomException();
}
public void test3() throws CustomException(){
...
}
public void all() throws CustomException(){
test1();
test2();
test3();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的托管bean中:
@SessionScoped
public class LoginBean implements Serializable{
public void test(){
try{
accountBean.test1();
accountBean.test2();
accountBean.test3();
}catch(CustomException e){
// WHAT HERE TO ROLLBACK TRANSACTION ?
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我怎么能保证的是,如果一个test1,test2或test3回滚,其他人将回滚呢?
我测试了这段代码, …
我会描述我的问题:
我正在开发异步模式下的文档生成(Office文档)应用程序,所以我使用Threads
由于性能限制,我必须同时启动2个线程,
为了在两个线程运行时不阻止用户启动文档生成,我想开发一个包含ArrayDeque对象作为线程堆栈的主线程,用户可以在其中添加创建请求(作为实现Collable或Runnable接口的对象)并使用Concurrent API在FIFO模式下启动线程
问题是:
如何确保将主线程作为所有用户的单例运行?
如何在主运行线程上调用ArrayDeque
什么postgres列类型将映射到基于hibernate的列中的Java Float类型?
我在我的遗留postgres数据库中有float(19)但是如果启用了hibernate模式验证,我会收到此错误消息.
Wrong column type in realtorprint.templatetype for column height.
Found: float8, expected: float4
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我的列是float(8)而不是float(19)我可能在遗留数据库中更改了这个.
我有一个以下格式的日期对象:
2013 年 1 月 20 日星期日 10:12:27 GMT+02:00
上述时间正确显示在 Microsoft Outlook 中:
2013 年 1 月 20 日星期日 12:12 PM(这是 GMT+2 >> 客户端时区的时间)
当尝试将日期对象格式化为SimpleDateFormat在 Outlook 中显示时,使用以下代码:
SimpleDateFormat sdf=new SimpleDateFormat(
"EEE M/d/yyyy hh:mm a");
String receivedDate = sdf.format(email.getDateTimeReceived());
Run Code Online (Sandbox Code Playgroud)
格式化的结果是:
2013 年 1 月 20 日星期日 10:12
所以缺少两个小时的时区差异。
请告知如何解决该问题,谢谢。
java ×8
swing ×3
alignment ×1
asynchronous ×1
comparable ×1
datetime ×1
ejb ×1
fifo ×1
hibernate ×1
icons ×1
japplet ×1
jbutton ×1
jcombobox ×1
jframe ×1
jpa ×1
jsf ×1
keylistener ×1
netbeans ×1
netbeans-7 ×1
postgresql ×1
properties ×1
set ×1
struts ×1
struts2 ×1
transactions ×1
url ×1