我正在做一个testJComboBox程序。一旦我选择了jCombobox的输出,我就会得到我需要的字符串值。但是,它不起作用。
这是我的代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestJCB extends JFrame {
private JLabel genderL;
private JComboBox genderJCB;
private String[] test = {"male", "female"};
private JButton gB;
public TestJCB() {
setSize(400, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setVisible(true);
JPanel frame = new JPanel();
frame.setSize(400, 400);
frame.setLocation(0, 0);
frame.setLayout(null);
frame.setVisible(true);
JLabel genderL = new JLabel("Gender");
genderL.setBounds(10, 200, 100, 30);
JComboBox genderJCB = new JComboBox(test);
genderJCB.setBounds(60, 10, 100, 30);
JButton gB = new JButton("Gender");
gB.setBounds(10, 50, 60, 30);
aaa a = new aaa();
gB.addActionListener(a);
frame.add(genderL);
frame.add(genderJCB);
frame.add(gB);
add(frame);
}
public class aaa implements ActionListener {
public void actionPerformed(ActionEvent sHandler) {
if (genderJCB.getSelectedItem().equals("female")) {
System.out.print("yes");
} else {
System.out.print("no");
}
}
}
public static void main(String[] args) {
TestJCB test = new TestJCB();
test.setVisible(true);
}
}
我也试过了combo.getEditor().getItem()
,但它没有用。
答案 0 :(得分:4)
我也尝试过combo.getEditor()。getItem()但它没有用。
您最重要的问题是,您声明了从未使用过或未初始化的变量
private JComboBox genderJCB;
因为公开内部TestJCB(){
覆盖并创建了
JComboBox genderJCB = new JComboBox(test);// same issue with JLabel too
如果您想在ActionPerformed
进行收听,则将其更改为
genderJCB = new JComboBox(test);
最好是阅读JComboBox tutorial
答案 1 :(得分:1)
错误是NullPointerException。它扔到这里if (genderJCB.getSelectedItem().equals("female")){
。发生这种情况是因为genderJCB为null。为什么是这样?你没有在这里设置JComboBox genderJCB= new JComboBox(test);
吗?不。你创建了一个新的局部变量,而不是一个字段,所以你从未声明过该字段。
答案 2 :(得分:0)
这是一个可能的解决方案
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class main extends JFrame
{
private JLabel genderL;
private JComboBox genderJCB;
private String[] test = { "male", "female" };
private JButton gB;
public main( )
{
setSize( 400, 400 );
setLocationRelativeTo( null );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setLayout( null );
setVisible( true );
JPanel frame = new JPanel( );
frame.setSize( 400, 400 );
frame.setLocation( 0, 0 );
frame.setLayout( null );
frame.setVisible( true );
JLabel genderL = new JLabel( "Gender" );
genderL.setBounds( 10, 200, 100, 30 );
JComboBox genderJCB = new JComboBox( test );
genderJCB.setBounds( 60, 10, 100, 30 );
JButton gB = new JButton( "Gender" );
gB.setBounds( 10, 50, 60, 30 );
aaa a = new aaa( genderJCB );
gB.addActionListener( a );
frame.add( genderL );
frame.add( genderJCB );
frame.add( gB );
add( frame );
}
public class aaa implements ActionListener
{
private JComboBox genderJCB;
public aaa( JComboBox genderJCB )
{
this.genderJCB = genderJCB;
}
public void actionPerformed( ActionEvent sHandler )
{
if( genderJCB.getSelectedItem( ).equals( "female" ) )
{
System.out.print( "yes" );
}
else
{
System.out.print( "no" );
}
}
}
public static void main( String[] args )
{
main test = new main( );
test.setVisible( true );
}
}