小编Cod*_*Med的帖子

Java GridBagLayout自动构建

我使用GridBagLayout和GridBagConstraints设计了一个GUI.它包含可变数量的行,每行包含一些可能的列布局.为了测试代码,我构建了具有不同颜色的面板GUI,用于说明每行和每列中所有单元格的位置和大小调整行为.这个测试GUI工作正常,但现在我需要自动化它的构造.具体来说,我需要将行作为三种不同类型之一.如果您运行下面的代码并查看生成的GUI,您可以看到row1是一种类型,行2,6和7是另一种类型,而行3,4和5是第三种类型.我需要将这三种类型的行中的每一行封装在自己的类中.更重要的是,我需要我的代码能够创建第三种类型的可变数量的行(在示例中由行3,4和5说明).(这是用于数据分析软件.面板将加载数据视图和工具来操纵数据视图.)

当我尝试将行封装到自己的类中时,GUI看起来应该看起来不应该看起来.下面的测试代码以它应该看起来的方式生成GUI布局.任何人都可以告诉我如何更改此代码,以便它具有上面第一段中描述的功能?

您可以将我的测试代码粘贴到您的IDE中,以使其立即运行.测试代码分为两个单独的文件,如下所示:

GridBagLayoutDemo.java的代码是:

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

public class GridBagLayoutDemo {
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);}
        pane.setLayout(new GridBagLayout());

// top row (fill, gridx, gridy, gridwidth 1, weightx 0, weighty 0, ipady 0)
        TestPanel panelr1c1 = new TestPanel(Color.black);
        GridBagConstraints constraint_r1c1 = getGridBagConstraints(GridBagConstraints.NONE,0,0,1,0,0,0);
        pane.add(panelr1c1,constraint_r1c1);

        TestPanel panelr1c2 = new TestPanel(Color.blue);
        GridBagConstraints constraint_r1c2 = getGridBagConstraints(GridBagConstraints.HORIZONTAL,1,0,1,0.8,0,0);
        pane.add(panelr1c2,constraint_r1c2);

        TestPanel panelr1c2a = new TestPanel(Color.green);
        GridBagConstraints constraint_r1c2a = getGridBagConstraints(GridBagConstraints.HORIZONTAL,2,0,1,0.8,0,0);
        pane.add(panelr1c2a,constraint_r1c2a);

        TestPanel panelr1c3 = new TestPanel(Color.red);
        GridBagConstraints constraint_r1c3 …
Run Code Online (Sandbox Code Playgroud)

java layout user-interface swing gridbaglayout

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

在春天petclinic转换数据库

我试图在spring petclinic示例应用程序中从hsql切换到MySQL .有人能告诉我我做错了什么吗?

我按照petclinic_db_setup_mysql.txt文件中的说明进行操作,并确认数据库是在mysql中正确创建的,但是当我尝试在eclipse中运行tomcat 7上的应用程序时,我收到以下错误:

org.springframework.beans.factory.BeanDefinitionStoreException:  
Invalid bean definition with name 'entityManagerFactory' defined in class path resource  
[spring/business-config.xml]:  
Could not resolve placeholder 'jpa.showSql' in string value "${jpa.showSql}"
Run Code Online (Sandbox Code Playgroud)

以下是来自business-config的相关代码:

   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          p:dataSource-ref="dataSource">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                  p:database="${jpa.database}" p:showSql="${jpa.showSql}"/>
            <!-- the 'database' parameter refers to the database dialect being used.
                By default, Hibernate will use a 'HSQL' dialect because 'jpa.database' has been set to 'HSQL'
                inside file spring/data-access.properties
             -->
        </property>
        <!-- gDickens: BOTH Persistence Unit and Packages to Scan …
Run Code Online (Sandbox Code Playgroud)

java mysql spring spring-mvc

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

以yyyy-MM-dd hh.mm.ss格式获取当前日期时间

我有一个应用程序,它总是只在一个时区运行,所以我不需要担心时区之间的转换.但是,必须始终按以下格式打印日期时间:

yyyy-MM-dd hh.mm.ss 
Run Code Online (Sandbox Code Playgroud)

以下代码无法打印正确的格式:

public void setCreated(){
    DateTime now = new org.joda.time.DateTime();
    String pattern = "yyyy-MM-dd hh.mm.ss";
    created  = DateTime.parse(now.toString(), DateTimeFormat.forPattern(pattern));
    System.out.println("''''''''''''''''''''''''''' created is: "+created);
}  
Run Code Online (Sandbox Code Playgroud)

setCreated()方法产生以下输出:

"2013-12-16T20:06:18.672-08:00"
Run Code Online (Sandbox Code Playgroud)

如何更改setCreated()中的代码,以便打印出以下代码:

"2013-12-16 20:06:18"
Run Code Online (Sandbox Code Playgroud)

java

4
推荐指数
1
解决办法
3万
查看次数

tomcat 7未能从日食中开始

我试图run as...run on server在eclipse中做一个spring mvc web应用程序,我收到以下对话框错误:

有人可以告诉我如何解决这个问题吗?

这是完整的堆栈跟踪:

Mar 18, 2014 2:03:22 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: list of lotsa files with their paths.
Mar 18, 2014 2:03:22 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:KnowledgeManager' did not find a matching property.
Mar 18, 2014 2:03:22 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 18, 2014 2:03:22 …
Run Code Online (Sandbox Code Playgroud)

java eclipse spring tomcat spring-mvc

4
推荐指数
1
解决办法
5万
查看次数

为 html 5 准备 mp4 文件

在 html 5 中,我想像这样嵌入 mp4:

<video width="640" height="480" controls>
    <source src="somefile.mp4" type="video/mp4">
    Your browser does not support the video tag.
</video>
Run Code Online (Sandbox Code Playgroud)

该视频在我的开发机器上的浏览器中运行良好,但该文件为 500MB,因为它是以全高清录制的。 如何对文件进行缩减采样,使其大小约为 50MB,视频更小但音质相同,并且仍然能够使用上面显示的 html5 视频标签在浏览器中播放?

我正在使用Windows。我一直在使用FlashIntegro,但它将文件转换为 avi 格式,无法使用 html 5 中的视频标签查看该格式。我想要一个 mp4 格式,我可以使用 html 5 中的视频标签发布。我想使用免费软件。

我不想使用 Flash 或 Adob​​e Creative Cloud,但我添加这些标签是为了查找可能了解免费替代方案的查看者。

html flash video adobe multimedia

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

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

A spring mvc app uses JavaMail to connect to dovecot/postfix. Both the spring mvc app and the dovecot/postfix installation are running on the same CentOS 7 physical server box. The app was able to use JavaMail to successfully make an imap connection until just recently, when some scripts for setting up OpenVPN seem to have changed things including the SSL configuration. How can I change the JavaMail code (or other configuration parameters) to get the IMAP connection to work again? …

java email ssl imap jakarta-mail

4
推荐指数
2
解决办法
2万
查看次数

在java中排序2d数据结构

我需要按值对2d键/值对进行排序.我已经在网上阅读了很多关于这个的参考资料,并且最终编写了我自己的类来使用HashMaps(见下文).我将代码放入一个精简的工作类中,用最少量的代码重现问题,这样您就可以将其剪切并粘贴到IDE中以进行快速诊断.

正如您所看到的,我编写的方法是在将值输入sortedMap之前正确对值进行排序.但是,出于某种原因,当我尝试随后迭代sortedMap时,它们的值再次以不同的方式排序.

任何人都可以告诉我如何修复下面的代码,以便迭代生成的2D数据对象,以降序为我提供数据?


编辑:使用TreeMaps重写,我仍然遇到类似的问题.这是重写的代码:

import java.util.*;

public class HashMapDemo {
    public static void main(String args[]) {

        // Code that creates and populates the unordered HashMap:
        TreeMap<Integer, Double> unSortedMap = new TreeMap<Integer, Double>();
        unSortedMap.put(1343, 0.521851);
        unSortedMap.put(1950, -0.301208);
        unSortedMap.put(3667, -0.0280762);
        unSortedMap.put(3879, 0.154724);
        unSortedMap.put(4124, 0.022583);

        // Code that calls the ordering method:
        TreeMap<Integer, Double> sortedMap = new TreeMap<Integer, Double>(
                sortTreeMap(unSortedMap));

        // Code that iterates through the "sorted" hashmap.
        System.out.println("now iterate through sortedMap: ");
        for (Integer key : sortedMap.keySet()) {
            System.out.println("key, sortedMap.get(key) are: …
Run Code Online (Sandbox Code Playgroud)

java arrays sorting 2d hashmap

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

具有复合主键的SELECT查询

在使用hibernate和jpa的spring mvc应用程序中,我最近使用@Embeddable类切换到了复合主键。结果,我需要更新JPA查询,该查询基于其唯一ID返回给定对象。以下是曾经使用过的JPA代码,但不再返回结果:

@SuppressWarnings("unchecked")
public Concept findConceptById(BigInteger id) {
    Query query = this.em.createQuery("SELECT conc FROM Concept conc WHERE conc.id =:cid");
    query.setParameter("cid", id);
    return (Concept) query.getSingleResult();
}
Run Code Online (Sandbox Code Playgroud)

如何更改上面的查询,以便它返回effectiveTime给定值的最新的Concept id 注意,ideffectiveTime是的两个属性ConceptPK 复合主键,并且从而为属性定义和getter和setter ideffectiveTime是在ConceptPK类,而不是在Concept类。

上面抛出的错误是:

Caused by: java.lang.IllegalArgumentException:  
Parameter value [786787679] did not match expected type [myapp.ConceptPK]  
Run Code Online (Sandbox Code Playgroud)

这是现在在Concept类中定义主键的方式:

private ConceptPK conceptPK;  
Run Code Online (Sandbox Code Playgroud)

这是ConceptPK该类的代码:

@Embeddable
class ConceptPK implements Serializable {

    @Column(name="id", nullable=false)
    protected BigInteger id; …
Run Code Online (Sandbox Code Playgroud)

java sql hibernate jpa

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

设置蒲公英数据表中可见行的数量

我希望蒲公英数据表一次显示15行,而不是默认的10行。有人可以告诉我如何做到这一点吗?

这是一些我使用分页控件一次显示10行的代码,以在10行的集合之间滚动:

<datatables:table id="mydata" data="${mydataset}" cdn="true" row="mr" theme="bootstrap2" 
    cssClass="table table-striped" paginate="true" info="false" 
    cssStyle="width: 150px;" align="left" dom="frtp">
    <datatables:column title="Concept Type" cssStyle="width: 150px;" display="html">
        <c:out value="${mr.something}"/>
    </datatables:column>
</datatables:table>
Run Code Online (Sandbox Code Playgroud)

java spring jsp spring-mvc dandelion

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

InputStream.getResourceAsStream()给出空指针异常

该行persistenceProperties.load(is);正在抛出nullpointerexception以下方法. 我该如何解决这个错误?

public void setUpPersistence(){

    final Properties persistenceProperties = new Properties();
    InputStream is = null;
    try {
        is = getClass().getClassLoader().getResourceAsStream("src/test/samples/persistence.properties");
        persistenceProperties.load(is);
    }catch (IOException ignored) {}
    finally {
        if (is != null) {try {is.close();} catch (IOException ignored) {}}
    }

    entityManagerFactory = Persistence.createEntityManagerFactory(
        "persistence.xml", persistenceProperties);
    }  
Run Code Online (Sandbox Code Playgroud)

我试图通过将包含该方法的类移动到应用程序结构中的各个其他位置,并通过以下方式更改错误之前的代码行来尝试此操作:

is = getClass().getClassLoader().getResourceAsStream("persistence.properties");
is = getClass().getClassLoader().getResourceAsStream("/persistence.properties");
is = getClass().getClassLoader().getResourceAsStream("/src/test/samples/persistence.properties");
is = getClass().getClassLoader().getResourceAsStream("other/paths/after/moving/persistence.properties");
Run Code Online (Sandbox Code Playgroud)

但是每次调用该方法时仍会抛出错误.

这是eclipse项目目录结构的打印屏幕.调用包含该方法的类TestFunctions.java,并显示其位置persistence.properties:


**编辑:**

根据下面的反馈,我将方法更改为:

public void setUpPersistence(){
    final Properties persistenceProperties = new Properties();
    InputStream is …
Run Code Online (Sandbox Code Playgroud)

java inputstream

3
推荐指数
2
解决办法
1万
查看次数