【问题标题】:Java setVisible issueJava setVisible 问题
【发布时间】:2014-01-19 13:35:50
【问题描述】:

我对@9​​87654321@ 有疑问。在我的 JFrame 类中,我有一个名为 changeMenu 的方法,它将改变 JFrame 的布局。

该方法从另一个类中获取一个字符串,并使用if 语句来确定应该将 JFrame 更改为什么。在if 语句中有几个setVisible 调用,但它们根本不起作用。

我确定if-statement 有效,因为该方法中有一个 println,它在接收到字符串时有效。我猜是因为我使用了另一种方法的setVisible

代码如下:

public class GUI extends JFrame{


    private JTextField song;
    private JList jl;
    private JButton add;
    private JButton edit;
    private JButton test;
    private JPanel jp;
    private JScrollPane sp;
    private JTextField artist;
    private JButton save;
    private JButton back;
    private JPopupMenu jpo;
    private JMenuItem ite;
    private JButton editsave;
    private JButton editback;
    private JTextField youtube;
    public JLabel ytl;
    public JLabel artl;
    public JLabel songl;

    DefaultListModel<String> m = new DefaultListModel<String>();

    public GUI(){
        super("MusicList - Alpha");
        setLayout(null);

        jl = new JList(m);
        add(jl);
        //creates a scrollpane, "implements jlist"
        sp = new JScrollPane(jl);
        sp.setBounds(30,30,195,200);
        add(sp);
        //creates the textfield to contain songname
        song = new JTextField(12);
        String afs = song.getText();
        song.setBounds(20,30,210,20);
        add(song);
        song.setVisible(false);
        //opens the add menu
        add = new JButton("add song");
        add.setBounds(20,250,100,20);
        add(add);
        //opens the edit menu
        edit = new JButton("edit song");
        edit.setBounds(135,250,100,20);
        add(edit);
        //this button checks if art and fl(arraylists) match to the index.
        test = new JButton("test");
        test.setBounds(300, 40, 80, 20);
        add(test);
        //the textfield which will pass the artist string.. used in add and edit
        artist = new JTextField();
        artist.setBounds(20,70,210,20);
        add(artist);
        artist.setVisible(false);
        //adds back button in "add" menu
        back = new JButton("back");
        back.setBounds(135,250,100,20);
        add(back);
        back.setVisible(false);
        //adds save button on "add" menu
        save = new JButton("save");
        save.setBounds(20,250,100,20);
        add(save);
        save.setVisible(false);
        //adds the back button on "edit" menu
        editback = new JButton("back");
        editback.setBounds(135, 250, 100, 20);
        add(editback);
        editback.setVisible(false);
        //adds the save button on "edit" menu
        editsave = new JButton ("save");
        editsave.setBounds(20,250,100,20);
        add(editsave);
        editsave.setVisible(false);
        //adds the youtube textfield
        youtube = new JTextField();
        youtube.setBounds(20,110,120,20);
        add(youtube);
        youtube.setVisible(false);
        //adds jlabel
        ytl = new JLabel("https://www.youtube.com/watch");
        ytl.setBounds(20,90,200,20);
        add(ytl);
        ytl.setVisible(false);

        artl = new JLabel("Artist");
        artl.setBounds(20,50,170,20);
        add(artl);
        artl.setVisible(false);

        songl = new JLabel("Song");
        songl.setBounds(20,10,170,20);
        add(songl);
        songl.setVisible(false);

    }

    public void addAL(){
        ActionListeners al = new ActionListeners();
        add.addActionListener(al);
        al.passObject(add);
        }
    public void changeMenu(String x){
        //0 is the "list" menu
        if(x.equals("0")){
            System.out.println("so far, so good...");
            jl.setVisible(true);
            sp.setVisible(true);
            add.setVisible(true);
            edit.setVisible(true);
            editback.setVisible(false);
            editsave.setVisible(false);
            youtube.setVisible(false);
            ytl.setVisible(false);
            song.setVisible(false);
            artist.setVisible(false);
            songl.setVisible(false);
            artl.setVisible(false);
            youtube.setText(null);
            song.setText(null);
            artist.setText(null);
        }
        //1 is the "add" menu
        if(x.equals("1")){
            System.out.println("this is the add menu");
            jl.setVisible(false);
            sp.setVisible(false);
            add.setVisible(false);
            edit.setVisible(false);
            back.setVisible(true);
            save.setVisible(true);
            song.setVisible(true);  
            artist.setVisible(true);
            youtube.setVisible(true);
            ytl.setVisible(true);
            songl.setVisible(true);
            artl.setVisible(true);

        }
        //2 is the "edit" menu
        if(x.equals("2")){
            jl.setVisible(false);
            sp.setVisible(false);
            add.setVisible(false);
            edit.setVisible(false);
            editback.setVisible(true);
            editsave.setVisible(true);
            song.setVisible(true);  
            artist.setVisible(true);
            youtube.setVisible(true);
            ytl.setVisible(true);
            songl.setVisible(true);
            artl.setVisible(true);
        }}
}

假设 changeMenu 接收字符串“1”,控制台是 println“这是添加菜单”。

public class ActionListeners extends MouseAdapter implements ActionListener{


private JList jl;
private JButton add;
private JButton edit;
private JButton save;
private JButton back;
private JPopupMenu jpo;
private JMenuItem ite;
private JButton editsave;
private JButton editback;

public void actionPerformed(ActionEvent e){
GUI gui = new GUI();
    if(e.getSource()==add){
        Model Model = new Model();
        Model.changeMenu("1");
        System.out.println("hey");
    }
}
public void passObject( JList jl, JButton add, JButton edit, JButton save, JButton back, JMenuItem ite, JButton editsave, JButton editback){
    this.add = add;
}
}

最后,模型类:

public class Model {

GUI gui = new GUI();

public void changeMenu(String x){
    if(x.equals("0")){
        gui.changeMenu("0");
    }
    if(x.equals("1")){
        gui.changeMenu("1");

    }
    if(x.equals("2")){
        gui.changeMenu("2");
    }
}

}

【问题讨论】:

  • 你的主要方法在哪里?

标签: java swing jframe


【解决方案1】:

通过使用 CardLayout 和交换 JPanel“视图”而不是设置可见或不可见组件,您的问题可以更轻松、更正确地解决。教程可以在这里找到:The CardLayout

您还需要学习使用布局管理器,而不是设置组件大小和位置或使用空布局。否则,您将面临创建非常难以更新或改进 GUI 的风险。


请注意,您的代码不完整,您尚未发布任何 ActionListener 代码,因此我们无法重现您的问题。


注意:当我从计时器中调用您的 changeMenu 方法时,它似乎对我有用:

// your constructor:
public GUI() {
  super("MusicList - Alpha");
  setLayout(null);  // **** ugh, don't do this ****
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // **** added ****

  jl = new JList(m);
  add(jl);
  // creates a scrollpane, "implements jlist"
  sp = new JScrollPane(jl);
  sp.setBounds(30, 30, 195, 200);
  add(sp);
  // creates the textfield to contain songname


  // ...... etc ...... code abbreviated for sake of clarity


  songl = new JLabel("Song");
  songl.setBounds(20, 10, 170, 20);
  add(songl);
  songl.setVisible(false);

  //  ****** I've added this code below *****
  int timerDelay = 2000;
  new Timer(timerDelay, new ActionListener() {
     private String[] menuValues = {"0", "1", "2"};
     private int index = 0;

     @Override
     public void actionPerformed(ActionEvent evt) {
        changeMenu(menuValues[index]);
        System.out.println("Index: " + index);
        index++;
        index %= menuValues.length;
     }
  }).start();
}

因此,您的问题不是由于“setVisible 无法正常工作”而是由于其他原因,也许您在错误的 GUI 对象上调用了您的 changeMenu 方法,但未显示。无论如何,我的测试表明问题不在于您发布的代码,而在于其他地方。


编辑
您行为不端的原因在于您的模型类:

public class Model {

  GUI gui = new GUI();

  // ...

}

您正在这个类中创建一个 new GUI 对象并调用该对象的方法,但猜猜看,它与 GUI 对象不同显示,因此在其上调用方法不会对显示的 GUI 产生影响。而是将正确的对象传入此类:

public class Model {

  private GUI gui;

  public Model(GUI gui) {
    this.gui = gui;
  }
  // ...    
}

【讨论】:

  • 嗯,我会考虑使用布局管理器,以前做过,但我喜欢(空)布局的结果。我会发布我所有的代码,所以你可以重现我的场景。谢谢您的帮助! :-)
  • @OliverBak:见编辑回答。您实际上是在 wrong GUI 对象上调用方法。
  • 哦,这并没有我想的那么复杂。谢谢你的帮助,我很感激 :-)
  • @OliverBak:不客气,但最好的解决方案还是用 CardLayout 交换视图并使用布局管理器。相信我,你不会后悔做出这种改变。
  • 嘿嘿,我稍后再看看.. 但是我听说像 MIGLayout 这样的东西更好,不确定:-)
猜你喜欢
  • 2011-07-23
  • 1970-01-01
  • 2017-02-09
  • 2011-12-10
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
相关资源
最近更新 更多