因此,当字符串等于某个值时,我希望将所有内容从JFrame
中移除,但是当我调用removeAll();
后跟revalidate();
和repaint();
时,它不会不做任何改变。
我尝试按照here调用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
。
无论如何,对我的问题有任何想法?
提前谢谢!
答案 0 :(得分:2)
这是一个使用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);
}
});
}
}
答案 1 :(得分:1)
我发现该代码有几个问题:
MTGSAMPServerReference被调用两次,一次在main中,另一次在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上想要的其他内容。
干杯。