2

JFrame因此,当字符串等于某个值时,我希望从我的所有内容中删除,但是当我调用removeAll();后跟revalidate();andrepaint();时,它不会改变任何内容。

我试过按照这里getContentPane.removeAll();的指示打电话,但这并没有做任何事情。

我的代码如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MTGSAMPServerReference extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    private static JList list1;
    private static JButton select1;
    public static String selectionMenu = "Main";

    public MTGSAMPServerReference() {
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
            Object[]mainMenu = {"Vehicles", "Bikes/Bicycles", "Boats", "Houses", "Businesses", "Objects", "Jobs", "Ranks", "Licenses", "VIP", "FAQ's"};
                Object[]VehiclesValueMenu = {"Lower Class", "Upper Class", "VIP"};
        if ("Main".equals(selectionMenu)) {
            JPanel controls = new JPanel(new BorderLayout(5,5));
            list1 = new JList<Object>(mainMenu);
            list1.setVisibleRowCount(10);
            select1 = new JButton("Select");
            select1.addActionListener(this);
            controls.add(new JScrollPane(list1));
            controls.add(select1, BorderLayout.PAGE_END);
            controls.setBorder(new EmptyBorder(25,25,0,0));
            add(controls);
            revalidate();
            repaint();
        }
        if ("VehiclesValue".equals(selectionMenu)) {
            removeAll();
            revalidate();
            repaint();
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Main".equals(selectionMenu)) {
            if (e.getActionCommand().equals("Select")) {
                int indexMain = list1.getSelectedIndex();
                System.out.println("Index Selected: " + indexMain);
                String valueMain = (String) list1.getSelectedValue();
                System.out.println("Value Selected: " + valueMain);
                if ("Vehicles".equals(valueMain)) {
                    selectionMenu = "VehiclesValue";
                    System.out.println("Menu selected: " + selectionMenu);
                    revalidate();
                    repaint();
                }
            }
        }
    }

    public void createAndShowGUI() {
        JFrame f = new MTGSAMPServerReference();
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //f.add(new drawOnPanel());
        f.setSize(1200, 800);
        f.setLocationRelativeTo(null);
        list1.setSize(250, 250);
        list1.setLocation(0, 0);
        select1.setSize(75, 25);
        select1.setLocation(251, 276);
        MTGSAMPServerReference.this.repaint();
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            MTGSAMPServerReference gui = new MTGSAMPServerReference();
            gui.createAndShowGUI();
            }
        });
    }
}

我已经完成了我的研究,但无法弄清楚我做错了什么。

如果我尝试更改我的JFrame f to a Global Variable instead of a Local Variable,它不会显示任何开始。

是的,我知道我是mixing Commmand Line with GUI,但那是only for debugging purposes。完成后,我将简单地删除所有内容Command Line related

无论如何,对我的问题有什么想法吗?

并提前感谢!

4

2 回答 2

2

这是一个使用 a 的示例CardLayout,它消除了static变量并通过 setter 和 getter 提供对某些内部值的访问,以进行演示

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

public class MTGSAMPServerReference extends JFrame implements ActionListener {

    private JList list1;
    private JButton select1;
    private String selectionMenu;
    private JPanel mainMenuPane;
    private JPanel vehicleMenuPane;

    public MTGSAMPServerReference(String selectionMenu) {
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
        Object[] mainMenu = {"Vehicles", "Bikes/Bicycles", "Boats", "Houses", "Businesses", "Objects", "Jobs", "Ranks", "Licenses", "VIP", "FAQ's"};
        Object[] VehiclesValueMenu = {"Lower Class", "Upper Class", "VIP"};

        mainMenuPane = new JPanel(new BorderLayout(5, 5));
        list1 = new JList<Object>(mainMenu);
        list1.setVisibleRowCount(10);
        select1 = new JButton("Select");
        select1.addActionListener(this);
        mainMenuPane.add(new JScrollPane(list1));
        mainMenuPane.add(select1, BorderLayout.PAGE_END);
        mainMenuPane.setBorder(new EmptyBorder(25, 25, 0, 0));

        vehicleMenuPane = new JPanel();
        vehicleMenuPane.add(new JLabel("Vehicle"));

        CardLayout cl = new CardLayout();
        setLayout(cl);
        add("main", mainMenuPane);
        add("vehicle", vehicleMenuPane);

        cl.show(getContentPane(), "main");

        setSelectionMenu(selectionMenu);

    }

    public String getSelectionMenu() {
        return selectionMenu;
    }

    public void setSelectionMenu(String value) {
        if (selectionMenu == null ? value != null : !selectionMenu.equals(value)) {
            selectionMenu = value;
            updateMenu();
        }
    }

    protected void updateMenu() {
        CardLayout cl = (CardLayout) getContentPane().getLayout();
        if ("Main".equals(selectionMenu)) {
            cl.show(getContentPane(), "main");
        } else if ("VehiclesValue".equals(selectionMenu)) {
            cl.show(getContentPane(), "vehicle");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Main".equals(selectionMenu)) {
            if (e.getActionCommand().equals("Select")) {
                int indexMain = list1.getSelectedIndex();
                System.out.println("Index Selected: " + indexMain);
                String valueMain = (String) list1.getSelectedValue();
                System.out.println("Value Selected: " + valueMain);
                if ("Vehicles".equals(valueMain)) {
                    setSelectionMenu("VehiclesValue");
                    System.out.println("Menu selected: " + selectionMenu);
                }
            }
        } else {
            setSelectionMenu("Main");
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MTGSAMPServerReference gui = new MTGSAMPServerReference("Main");
                gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                gui.pack();
                gui.setLocationRelativeTo(null);
                gui.setVisible(true);
            }
        });
    }
}
于 2013-07-26T12:17:12.863 回答
1

我看到该代码有几个问题:

  • MTGSAMPServerReference 被调用了两次,一次是主要的,另一次是createAndShowGUI().

  • MTGSAMPServerReference 扩展了 JFrame,但 createAndShowGUI()JFrame f也包含一个局部变量。本质上,您正在分配this一个局部变量f,这没有任何意义。

  • 要初始化 GUI,您需要执行构造函数createAndShowGUI(),然后所有可以执行的代码都归因于事件。在你的情况下的actionPerformed()方法。如果要删除框架内的组件,则必须在此处执行正确的代码(或在某些时候从那里调用)。

  • 如您的链接所述,要删除您必须removeAll()在 JFrame 的 contentPane 上执行的所有组件,使用getContentPane().

  • 我不知道你打算用这些比较做什么if("Main".equals(selectionMenu))。同样是初始化代码,它运行了一次。在那里你构建了所有的 GUI。该变量 selectionMenu将来可能会更改,但这不会追溯执行该代码。如果您想使用 的新值执行任何操作selectionMenu,请在 actionListener 上执行。

这是您的代码的有效修改。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MTGSAMPServerReference extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    private static JList list1;
    private static JButton select1;
    public static String selectionMenu = "Main"; //accomplishes nothing

    public static void main(String[] args) 
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            MTGSAMPServerReference gui = new MTGSAMPServerReference();
            gui.createAndShowGUI();
            }
        });
    }

    public void createAndShowGUI() {
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //f.add(new drawOnPanel());
        setSize(1200, 800);
        setLocationRelativeTo(null);
        list1.setSize(250, 250);
        list1.setLocation(0, 0);
        select1.setSize(75, 25);
        select1.setLocation(251, 276);
        setVisible(true);
    }


    public MTGSAMPServerReference() {
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
            Object[]mainMenu = {"Vehicles", "Bikes/Bicycles", "Boats", "Houses", "Businesses", "Objects", "Jobs", "Ranks", "Licenses", "VIP", "FAQ's"};
                Object[]VehiclesValueMenu = {"Lower Class", "Upper Class", "VIP"};
            JPanel controls = new JPanel(new BorderLayout(5,5));
            list1 = new JList<Object>(mainMenu);
            list1.setVisibleRowCount(10);
            select1 = new JButton("Select");
            select1.addActionListener(this);
            controls.add(new JScrollPane(list1));
            controls.add(select1, BorderLayout.PAGE_END);
            controls.setBorder(new EmptyBorder(25,25,0,0));
            add(controls);
            //revalidate(); //uneeded at this point the JFrame is not yet visible, thus nothing to repaint
            //repaint();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Select")) {
            int indexMain = list1.getSelectedIndex();
            System.out.println("Index Selected: " + indexMain);
            String valueMain = (String) list1.getSelectedValue();
            System.out.println("Value Selected: " + valueMain);
            if ("Vehicles".equals(valueMain)) {
                System.out.println("Menu selected: " + selectionMenu);
                getContentPane().removeAll(); //equivalent to this.getContentPane().removeAll();
                revalidate();
                repaint();
            }
        }
    }  
} 

此外,我已经删除了对变量的修改,selectionMenu它现在并没有真正做任何事情。

正如其他人所说,您可能希望在某些时候使用另一个布局。哪一个取决于您在 JFrame 上还需要什么。

干杯。

于 2013-07-26T12:25:23.000 回答