【发布时间】:2016-12-06 23:57:52
【问题描述】:
我正在编写一个 gui 程序,当单击某些按钮时,它基本上会删除和添加 jpanel。我正在尝试创建 jpanels 的层次结构: 这是层次结构
Opener
Mammal
Tiger
Tiger_caged //terminating button
Tiger_riding //terminating button
Reptile
Frog
Frog_eating //terminating button
Frog_null //terminating button
初始 jpanel 是 opener,在启动时添加到 JFrame。
Opener 会有哺乳动物和爬行动物。
每个按钮在单击时都会调用swap(currentJPanel),然后调用removeall()、repaint() 和revalidate(),然后在它们下面添加下一个带有x 按钮的JPanel,每个都调用swap(currentJPanel) 方法。这将一直持续到您到达带有按钮的终止 JPanel,当单击该按钮时,会执行 x 操作,然后调用 reset() 询问用户是否要继续游戏或获得结果。如果选择继续游戏,则removeall()、repaint()、revalidate() 并再次添加opener。
我创建了一个名为content 的jPanel 来放置我的每个JPanel,将opener 添加到content,并将content 添加到JFrame。在单击某个按钮之前,不应将不在 opener 中的按钮添加到 JFrame 中。以下是我当前的代码。当我运行 gui 时,我创建的所有按钮都是可见的。只有按钮 Reptile 和 Mammal 应该在启动时可见。此外,当单击其中一个按钮时,不会删除或添加任何内容。唯一能正常工作的按钮是getResult 按钮,它是唯一一个不调用swap() 的按钮。我最初的想法是我不能将 jButton 传递给一个方法,但是当我调用 swap() 时没有出现错误,所以我不确定。任何帮助和/或建议将不胜感激。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class DreamInterpreter extends JFrame implements ActionListener{
private final LayoutManager layoutMain;
private String result = "";
private JLabel question = new JLabel("What was in your dream?");
private String reset = "What was in your dream?";
private JTextArea showResult;
//BUTTONS
private JButton exit = new JButton("Exit");
private JButton getResult= new JButton("INTERPRET MY DREAM");
private JButton continueGame = new JButton("Add more to my dream");
private JButton animal = new JButton("Animal");
private JButton mammal = new JButton("Mammal");
private JButton tiger= new JButton("Tiger");
private JButton tiger_caged= new JButton("The tiger is in a cage");
private JButton tiger_riding= new JButton("The tiger is being ridden");
private JButton reptile = new JButton("Reptile");
private JButton frog= new JButton("Frog");
private JButton frog_eating= new JButton("Eating");
private JButton frog_null= new JButton("None of these");
private JButton person = new JButton("Person");
private JButton location = new JButton("Location");
//PANELS
//DEFAULT PANEL
JPanel standard;
JPanel content;
JPanel opener;
JPanel jContinueGame;
JPanel jAnimal, jPerson, jLocation;
JPanel jMammal, jReptile;
JPanel jTiger;
JPanel jFrog;
public DreamInterpreter(){
super("Dream Interpreter");
layoutMain = new BorderLayout(); // add components with add(component, BorderLayout.CENTER (NORTH, SOUTH, EAST, WEST)
setLayout(layoutMain);
exit.addActionListener(this);
getResult.addActionListener(this);
continueGame.addActionListener(this);
animal.addActionListener(this);
person.addActionListener(this);
location.addActionListener(this);
mammal.addActionListener(this);
reptile.addActionListener(this);
frog.addActionListener(this);
frog_eating.addActionListener(this);
frog_null.addActionListener(this);
tiger.addActionListener(this);
tiger_caged.addActionListener(this);
tiger_riding.addActionListener(this);
//FORMATTING LAYOUT
showResult = new JTextArea(15,20);
//DEFAULT
standard = new JPanel();
standard.setLayout(new FlowLayout(FlowLayout.CENTER,10,8));
//HEADER
JPanel header = new JPanel();
header.add(question);
//OPENER
opener = standard;
opener.add(animal);
opener.add(location);
opener.add(person);
//CONTENT
content = new JPanel();
content.add(opener);
//CONTINUE GAME
jContinueGame = standard;
jContinueGame.add(continueGame);
jContinueGame.add(getResult);
//DECLARE OTHER JPANELS
jAnimal = jPerson = jLocation = standard;
jMammal = jReptile = standard;
jFrog = jTiger = standard;
add(header, BorderLayout.NORTH);
add(content, BorderLayout.CENTER);
//ADDING
//jAnimal
jAnimal.add(mammal);
jAnimal.add(reptile);
jMammal.add(tiger);
jTiger.add(tiger_caged);
jTiger.add(tiger_riding);
jReptile.add(frog);
jFrog.add(frog_eating);
jFrog.add(frog_null);
//jPerson
//jLocation
}
public void actionPerformed(ActionEvent event){
if(event.getSource() == animal){
swap(jAnimal);
question.setText("What kind of animal was in your dream?");
}
if(event.getSource() == mammal){
swap(jMammal);
question.setText("What kind of mammal was in your dream?");
}
if(event.getSource() == tiger){
swap(jTiger);
question.setText("What was the tiger doing?");
}
if(event.getSource() == tiger_caged){
result += "";
reset();
}
if(event.getSource() == tiger_riding){
result+="";
reset();
}
if(event.getSource() == reptile){
swap(jReptile);
question.setText("What kind of reptile was in your dream?");
}
if(event.getSource()==frog){
swap(jFrog);
}
if(event.getSource()==frog_eating){
result+="";
reset();
}
if(event.getSource()==frog_null){
result+="";
reset();
}
if(event.getSource() == continueGame){
swap(opener);
question.setText("What was in your dream?");
}
if(event.getSource() == getResult){
content.removeAll();
content.revalidate();
content.repaint();
content.add(showResult);
showResult.setText(""+result);
}
}
public static void main(String[] args){
DreamInterpreter dreamInterpreter = new DreamInterpreter();
dreamInterpreter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dreamInterpreter.setSize(450, 350);
dreamInterpreter.setVisible(true);
}
public void reset(){
content.removeAll();
content.revalidate();
content.repaint();
question.setText("What would you like to do?");
content.add(jContinueGame);
}
public void swap(JPanel panel){
content.removeAll();
content.revalidate();
content.repaint();
content.add(panel);
}
}
【问题讨论】:
标签: java user-interface jframe jpanel