我正在尝试为允许插入和删除的 BST 树创建一个简单的 GUI。但是,我在运行时将组件插入 GUI 时遇到了很多麻烦。我想做的是每次插入或删除时“刷新”GUI。我创建了一个名为 printBst 的方法,该方法创建 Jlabels 来显示编号索引,如下所示,但它们没有显示出来。之后我尝试重新验证和验证 GUI,但它仍然无法正常工作。有人有想法么?
package source;
import javax.swing.*;
import source.BST.BSTnode;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
public class Frame2 extends JFrame implements ActionListener{
BST bst;
JPanel displayPanel, buttonPanel, totalGUI;
JButton insertButton, deleteButton, resetButton;
JTextField insertField, deleteField;
public JPanel createContentPane (){
bst = new BST();
totalGUI = new JPanel();
totalGUI.setLayout(null);
displayPanel = new JPanel();
displayPanel.setLayout(null);
displayPanel.setLocation(10, 80);
displayPanel.setSize(400, 300);
totalGUI.add(displayPanel);
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(10, 0);
buttonPanel.setSize(800, 80);
totalGUI.add(buttonPanel);
insertField = new JTextField(1);
insertField.addActionListener(this);
insertField.setLocation(0, 10);
insertField.setSize(150, 30);
buttonPanel.add(insertField);
insertButton = new JButton("Insert");
insertButton.setLocation(160, 10);
insertButton.setSize(150, 30);
insertButton.addActionListener(this);
buttonPanel.add(insertButton);
deleteField = new JTextField(1);
deleteField.addActionListener(this);
deleteField.setLocation(320, 10);
deleteField.setSize(150, 30);
buttonPanel.add(deleteField);
deleteButton = new JButton("Delete");
deleteButton.setLocation(480, 10);
deleteButton.setSize(150, 30);
deleteButton.addActionListener(this);
buttonPanel.add(deleteButton);
resetButton = new JButton("Reset");
resetButton.setLocation(640, 10);
resetButton.setSize(150, 30);
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
totalGUI.setOpaque(true);
return totalGUI;
}
public void printBst(BSTnode node, int x, int x2, int y) {
if (node != null) {
JLabel current = new JLabel(""+ node.data);
current.setLocation((x+x2)/2, y);
current.setSize(100, 30);
current.setHorizontalAlignment(0);
displayPanel.add(current);
printBst(node.left, x, (x2+x)/2, y+60);
printBst(node.right, (x2+x)/2, x2, y+60);
System.out.println("here");
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == insertButton)
{
bst.insert(Integer.valueOf(insertField.getText()));
displayPanel.removeAll();
printBst(bst.root, 0, 800, 0);
totalGUI.revalidate();
validate();
}
else if(e.getSource() == deleteButton)
{
bst.delete(Integer.valueOf(deleteField.getText()));
displayPanel.removeAll();
printBst(bst.root, 0, 800, 0);
totalGUI.revalidate();
validate();
}
else if(e.getSource() == resetButton)
{
bst.clear();
displayPanel.removeAll();
printBst(bst.root, 0, 800, 0);
totalGUI.revalidate();
validate();
}
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("[=] JButton Scores! [=]");
Frame2 demo = new Frame2();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}