小编Sup*_*tar的帖子

如何将JPanel划分为左右段?

我想将JPanel划分为左右段.我怎么做 ?之后,我会在左右两侧放置面板.

java layout swing jpanel

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

命令行args没有价值,但代码给出了输出?

我在Eclipse中将其作为Java应用程序运行.令人惊讶的是,它打印出第一行.我期待它打印第二个.我从未在命令行中输入任何内容,为什么会发生这种情况?

class Game
{   
    public static void main(String args[])
    {
        if(args != null){
            System.out.println("Lets do something with args");
        }else{
            System.out.println("Cant do something until args is not null"); 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑 -

还尝试了另一件事来回答其中一个答案.删除main中的所有代码并将其替换为 -

System.out.println("Main method..."); 
for(int i = 0; i< args.length; i++){
System.out.println("i = " + i); 
} 
Run Code Online (Sandbox Code Playgroud)

输出是主要方法.为什么?

java

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

如何获取从JComboBox中选择的项目的索引?

这就是我创建JComboBox的方法 -

String[] options = {"First", "Second" , "Third"};
JComboBox optionsCombo = new JComboBox(options);
Run Code Online (Sandbox Code Playgroud)

选择其中一个项目时,如何获取所选项目的索引?我不希望该项目是所选项目.

java swing selection jcombobox

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

将ActionListener添加到Panel - Panel实现ActionListener vs Panel HAS ActionListener

我为我的节目制作了一个小组.它仅由RadioButtons组成.当选择radiobutton时,我想在其他代码中设置一个布尔值.此面板将用作更大面板或框架的组件,该面板或框架还应能够收听此面板内发生的事件.

那么,我应该选择以下哪个选项来收听事件 -

1 -

RadioButtonPanel extends JPanel implements ActionListener{

  public void actionPerformed(ActionEvent e){}
  //code to add the action listener to the radio buttons 
  oneRadioButton.addActionListener(this);   
}
Run Code Online (Sandbox Code Playgroud)

2 -

RadioButtonPanel extends JPanel{

  class InnerStrength implements ActionListener{
     public void actionPerformed(ActionEvent e){} 
   }    

  //code to add the action listener to the radio buttons     
  oneRadioButton.addActionListener(Anonymous InnerStrength) 

}
Run Code Online (Sandbox Code Playgroud)

3 - 我没想到的任何其他方式吗?

java inheritance swing composition actionlistener

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