gpt4 book ai didi

java - 使用 swing 在运行时创建和显示对象

转载 作者:行者123 更新时间:2023-11-29 06:00:22 26 4
gpt4 key购买 nike

我正在尝试为允许插入和删除的 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();
}
});
}
}

最佳答案

确保在从 displayPanel 移除组件后调用 revalidate()repaint() 即:

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 Canvas 的现成框架。例如JGraph , JFreeChartPiccolo2D .

关于java - 使用 swing 在运行时创建和显示对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10378043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com