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
。
无论如何,对我的问题有什么想法吗?
并提前感谢!