Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButtons随JPanel一起移动_Java_Eclipse_Jpanel - Fatal编程技术网

Java JButtons随JPanel一起移动

Java JButtons随JPanel一起移动,java,eclipse,jpanel,Java,Eclipse,Jpanel,所以我的代码有一个非常令人沮丧的问题。在我的主课中,我在另一个课中调用一个方法,它应该给我一个问题和4个可能的答案。这些不同的问题长度不一样。问题是,如果问题比上一个问题长,那么按钮就会彼此分离,而我不会更改代码中的任何内容。所以我开始寻找一些解决方案,并找到了一些东西。我把问题放在不同的面板上,而不是按钮上 面板1: public JPanel getPanel(){ code } public JPanel getPanel1(){ code } private void

所以我的代码有一个非常令人沮丧的问题。在我的主课中,我在另一个课中调用一个方法,它应该给我一个问题和4个可能的答案。这些不同的问题长度不一样。问题是,如果问题比上一个问题长,那么按钮就会彼此分离,而我不会更改代码中的任何内容。所以我开始寻找一些解决方案,并找到了一些东西。我把问题放在不同的面板上,而不是按钮上

面板1:

public JPanel getPanel(){
    code
}
public JPanel getPanel1(){
   code
}
private void initializeAnswer(String jsonFileName, int questionNumber) {

   JPanel panel = new Answer(json.getCorrectAnswer(questionNumber, 
   jsonFileName)).getPanel();
   JPanel panel1 = new Answer(json.getCorrectAnswer(questionNumber, 
   jsonFileName)).getPanel1();
   frame.setContentPane(panel);
   frame.setContentPane(panel1);

}
面板2:

public JPanel getPanel(){
    code
}
public JPanel getPanel1(){
   code
}
private void initializeAnswer(String jsonFileName, int questionNumber) {

   JPanel panel = new Answer(json.getCorrectAnswer(questionNumber, 
   jsonFileName)).getPanel();
   JPanel panel1 = new Answer(json.getCorrectAnswer(questionNumber, 
   jsonFileName)).getPanel1();
   frame.setContentPane(panel);
   frame.setContentPane(panel1);

}
然后在我的主要部分我写了以下内容:

public JPanel getPanel(){
    code
}
public JPanel getPanel1(){
   code
}
private void initializeAnswer(String jsonFileName, int questionNumber) {

   JPanel panel = new Answer(json.getCorrectAnswer(questionNumber, 
   jsonFileName)).getPanel();
   JPanel panel1 = new Answer(json.getCorrectAnswer(questionNumber, 
   jsonFileName)).getPanel1();
   frame.setContentPane(panel);
   frame.setContentPane(panel1);

}
我以为这会同时在画面上显示两个面板,但我错了。我甚至使用以下代码将panel1的背景色更改为透明,但它不起作用:

panel1.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.5f));

您可以使用根JPanel

JPanel root = new JPanel();
root.add(panel1);
root.add(panel2);
frame.setContentPane(root);
额外使用布局…

一个框架不能有两个内容窗格。你是在找像“也许”这样的东西吗?@Khelwood说实话。。我不知道。我刚读了那本书,不得不试一试@khelwood我说过一个框架不能有两个内容窗格,并不是说你不能在一个框架中添加两个面板。我想你需要
frame.setLayout(新的GridLayout(0,1));框架。添加(面板);框架。添加(面板1)但这只是一个猜测。邮递