Tag: java 7

JComboBox返回值

用什么方法返回用户选择的选择? JPanel ageSelection = new JPanel(); JLabel age = new JLabel(“Age:”); ArrayList ageList = new ArrayList(); for (int i = 1; i <= 100; ++i) { ageList.add(i); } DefaultComboBoxModel modelAge = new DefaultComboBoxModel(); for (Integer i : ageList) { modelAge.addElement(i); } JComboBox ageEntries = new JComboBox(); ageEntries.setModel(modelAge); ageEntries.addActionListener(new putInTextListener()); ageSelection.add(age); ageSelection.add(ageEntries); class putInTextListener implements ActionListener […]

在Runtime.getRuntime()。exec中有2个可执行文件的空格

我有一个命令,我需要在这些行中使用java运行: C:\path\that has\spaces\plink -arg1 foo -arg2 bar “path/on/remote/machine/iperf -arg3 hello -arg4 world” 当路径没有空格时,此命令工作正常,但是当我有空格时,我似乎无法使其工作。 我尝试过以下操作,运行Java 1.7 String[] a = “C:\path\that has\spaces\plink”, “-arg1 foo”, “-arg2 bar”, “path/on/remote/machine/iperf -arg3 hello -arg4 world” Runtime.getRuntime().exec(a); 以及 String[] a = “C:\path\that has\spaces\plink”, “-arg1 foo”, “-arg2 bar”, “path/on/remote/machine/iperf”, “-arg3 hello”, “-arg4 world” Runtime.getRuntime().exec(a); 但似乎都没有做任何事情。 我有什么问题吗?

这是一个Swing Java 7渲染错误吗?

我做了一个简单的Swing应用程序。 但渲染表现出错误。 我做错了什么或者它是一个错误? 这是一个简单的小型JFrame,带有文本字段,按钮和空列表。 如果我首先水平调整窗口大小然后键入文本字段,按钮突然消失。 这是我的代码: public class App extends JFrame { public App() { JTextField messageFld = new JTextField(); JButton saveBtn = new JButton(“Save”); JPanel inputPanel = new JPanel(new BorderLayout()); inputPanel.add(messageFld, BorderLayout.CENTER); inputPanel.add(saveBtn, BorderLayout.EAST); JList list = new JList(); JPanel panel = new JPanel(new BorderLayout()); panel.add(inputPanel, BorderLayout.NORTH); panel.add(list, BorderLayout.CENTER); this.getContentPane().add(panel); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle(“Test application”); this.pack(); […]

更改背景颜色可编辑JComboBox

我正在编写一个JFrame表单中的可编辑combobox,但我想改变背景颜色。 程序如何工作:如果我点击“按”按钮,那么combobox的背景需要变成黑色。 我试过了: 1。 cbo.setBackground(Color.BLACK); 但它没有做任何事情 2 cbo.getEditor().getEditorComponent().setBackground(Color.BLACK); ((JTextField) cbo.getEditor().getEditorComponent()).setOpaque(true); 做这个: 代码示例: public class NewJFrame extends javax.swing.JFrame { private JComboBox cboCategorie; public NewJFrame() { initComponents(); cboCategorie = new JComboBox(); cboCategorie.setBounds(10, 10, 250, 26); cboCategorie.setVisible(true); cboCategorie.setEditable(true); this.add(cboCategorie); } private void pressActionPerformed(java.awt.event.ActionEvent evt) { cboCategorie.getEditor().getEditorComponent().setBackground(Color.BLACK); ((JTextField) cboCategorie.getEditor().getEditorComponent()).setOpaque(true); } 我正在使用Java JDK7 任何sugestions?

为什么要将String编译切换为两个开关

我在编译开关时阅读了JVM规范 ,并对如何编译String上的switch语句感兴趣。 这是我检查的测试方法(JDK1.7.0_40): static int test(String i) { switch (i) { case “a”: return -100; case “45b”: return 1; case “c”: return 2; default: return -1; } } 我希望这个方法可以在string的hashCode上编译成简单的lookupswitch,但突然之间 static int test(java.lang.String); Code: 0: aload_0 1: astore_1 2: iconst_m1 3: istore_2 4: aload_1 5: invokevirtual #6 // Method java/lang/String.hashCode:()I 8: lookupswitch { // 3 97: […]

为什么在Java 7中使用菱形运算符进行类型推理?

List list = new ArrayList(); 将导致编译器警告。 但是,以下示例在没有任何警告的情况下编译: List list = new ArrayList(); 我很好奇为什么需要引入钻石操作员。 如果没有类型参数,为什么不在构造函数上进行类型推断(因为它已经针对java中的静态方法已经完成并且被像google guava这样的集合库利用) 编辑 :使用millimoose答案作为起点我查看了实际上是什么类型的擦除,它不只是删除所有类型信息。 编译器实际上做了一些(从官方文档复制): 如果类型参数是无界的,则将generics类型中的所有类型参数替换为其边界或对象。 因此,生成的字节码仅包含普通的类,接口和方法。 如有必要,插入类型铸件以保持类型安全。 生成桥接方法以保留扩展generics类型中的多态性。

Maven包编译错误

我在eclipse中使用maven创建了一个webapp项目。 当我在命令提示符下运行命令mvn package时,它显示了下面的错误。 [ERROR] COMPILATION ERROR : [INFO] ————————————————————- [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? [INFO] 1 error [INFO] ————————————————————- [INFO] ———————————————————————— [INFO] BUILD FAILURE [INFO] ———————————————————————— [INFO] Total time: 4.310s [INFO] Finished at: Tue Mar 05 14:32:23 IST 2013 [INFO] Final […]

使用try-with-resources静静地关闭资源

是否可以忽略使用try-with-resources语句关闭资源时抛出的exception? 例: class MyResource implements AutoCloseable{ @Override public void close() throws Exception { throw new Exception(“Could not close”); } public void read() throws Exception{ } } //this method prints an exception “Could not close” //I want to ignore it public static void test(){ try(MyResource r = new MyResource()){ r.read(); } catch (Exception e) { System.out.println(“Exception: […]

使用Java 7在JTextPane中使用样式文本进行奇怪的文本包装

我有两个不同的编辑器使用JTextPane和Java 7中的奇怪错误,这些错误在以前的JVM版本中没有出现。 它出现在包含样式文本或组件的长行中。 这是一个演示此错误的示例。 在此示例中,每次插入字符时,都会对所有文本应用默认样式。 我用JDK 1.7.0_04测试了它。 import java.awt.BorderLayout; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; public class BugWrapJava7 extends JFrame { JTextPane jtp; StyledDocument doc; public BugWrapJava7() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); jtp = new JTextPane(); add(jtp, BorderLayout.CENTER); jtp.setText(“\ntype some text in the above empty line and check the wrapping behavior”); doc = jtp.getStyledDocument(); doc.addDocumentListener(new DocumentListener() […]

将Java 7编译为Java 6

我知道Java 7的运行时function不适用于Java 6,但由于没有添加新的字节代码,新的字节代码invokedynamic仅与非Java语言相关,我想知道转换有多难Java 7源代码(新的switch语句,菱形运算符)到纯Java 6(即能够开始将源代码转换为Java 7而不会失去Java 6的兼容性)。 有什么指针吗?