我正在尝试为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();
}
});
}
}
答案 0 :(得分:3)
请确保在从revalidate()
删除组件后致电repaint()
和displayPanel
,即:
displayPanel.removeAll();
printBst(0, 800, 0);
displayPanel.revalidate();
displayPanel.repaint();
另外,请注意:
printBst(0, 800, 0);
导致displayPanel
内的无效(不在边界内)坐标,其大小定义为(400,300)窗口的左上角为0,0。尝试以下操作,您应该在面板中间的某处看到您的标签:
printBst(0, 400, 0);
Absolute positioning可能非常难以管理。查看A Visual Guide to Layout Managers,看看是否可以找到合适的布局来帮助您。
您也可以考虑准备好可以为您提供2D画布的框架。例如JGraph,JFreeChart或Piccolo2D。