gpt4 book ai didi

java - Java中的JComboBox,声明错误

转载 作者:行者123 更新时间:2023-12-02 07:36:20 24 4
gpt4 key购买 nike

我用 Java 得到了下一个简单的程序,我在网上得到了它,因为我正在尝试学习 Java,但不知道如何修复它:

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

public class java2 extends JPanel {

public java2 () {
setLayout(new Gridlayout(2,1));
JList lista = new JList(java2.ids);
add(new JScrollpane(lista));
JComboBox combo=new JComboBox();
for (int i=0;i<100 ;i++ ) {
combo.addItem(Integer.toString(i));
add(combo);
}
}
public static void main(String args[]) {
java1 lista=new Java1();
JFrame ventana=new JFrame();
ventana.getContentPane().add(lista,BorderLayout.CENTER);
ventana.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent evt){System.exit(0);}});
ventana.setSize(200,200);
ventana.setVisible(true);
}
}

每次编译时我都会遇到下一个问题,正如我所说,我是 java 新手,可以说我知道如何修复它们:

java2.java:8: error: cannot find symbol
setLayout(new Gridlayout(2,1));
^
symbol: class Gridlayout
location: class java2
java2.java:9: error: cannot find symbol
JList lista = new JList(java2.ids);
^
symbol: variable ids
location: class java2
java2.java:10: error: cannot find symbol
add(new JScrollpane(lista));
^
symbol: class JScrollpane
location: class java2
java2.java:18: error: cannot find symbol
java1 lista=new Java1();
^
symbol: class java1
location: class java2
java2.java:18: error: cannot find symbol
java1 lista=new Java1();
^
symbol: class Java1
location: class java2
Note: java2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
5 errors

最佳答案

试试这个:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class JavaDemo extends JPanel {

public JavaDemo() {
setLayout(new GridLayout(2, 1));
JList lista = new JList(new String[] {"1", "2", "3", "4"});
add(new JScrollPane(lista));
JComboBox combo = new JComboBox();
for (int i = 0; i < 100; i++) {
combo.addItem(Integer.toString(i));
add(combo);
}
}

public static void main(String args[]) {
JavaDemo lista = new JavaDemo();
JFrame ventana = new JFrame();
ventana.getContentPane().add(lista, BorderLayout.CENTER);
ventana.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
ventana.setSize(200, 200);
ventana.setVisible(true);
}
}

关于java - Java中的JComboBox,声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12184980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com