gpt4 book ai didi

java - GUI 不会显示按钮

转载 作者:行者123 更新时间:2023-12-02 08:58:11 26 4
gpt4 key购买 nike

在这个程序中,所有内容都会编译,看起来我添加了所有按钮面板和文本字段,但是当我运行该程序时,主面板显示没有任何添加的对象。关机弹出屏幕正确显示。下面是 GUI 类。我尝试过将组件公开,但没有改变任何东西。据此,子面板应该可以正常工作。然而,也许一双新的眼睛可以帮助我看到我所错过的东西。

import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class BinarySearchTreeGUI {
private JFrame mainF;
private JPanel bstMain;
private JPanel sortOrder;
private JPanel numType;

JRadioButton ascendingB = new JRadioButton("Ascending");
JRadioButton descendingB = new JRadioButton("Descending");
JRadioButton integerB = new JRadioButton("Integer");
JRadioButton fractionB = new JRadioButton("Fraction");

private JButton button;
public JTextField unsortedList, sortedList;
private JLabel l1, l2;

@SuppressWarnings("OverridableMethodCallInConstructor")

BinarySearchTreeGUI() {
launchApp();
}

private void launchApp() {
createNewComponents();
setDisplayFrame();
setComponents();
setComponentsEditableVisible();
addComponents();
onShutDownRequest();
}

private ButtonAction buttonActionOnClick() {
return new ButtonAction(unsortedList);
}

private void addComponents() {
mainF.add(bstMain);
bstMain.add(button);
bstMain.add(l1);
bstMain.add(l2);
bstMain.add(sortedList);
bstMain.add(unsortedList);
buttonActionOnClick();
}

private void setComponentsEditableVisible() {
sortedList.setEditable(false);
mainF.setVisible(true);
}

private void setComponents() {
bstMain.setLayout(null);
button.setBounds(180, 160, 120, 30);
l1.setBounds(50, 15, 200, 25);
l2.setBounds(55, 90, 200, 25);
sortedList.setBounds(120, 90, 280, 25);
unsortedList.setBounds(120, 15, 280, 25);
setSubPanels();
}

private void createNewComponents() {
mainF = new JFrame("Binary Search Tree Sort");
bstMain = new JPanel();
button = new JButton("Perform Sort");
l1 = new JLabel("Original List");
l2 = new JLabel("Sorted List");
unsortedList = new JTextField();
sortedList = new JTextField();
sortOrder = new JPanel();
numType = new JPanel();
}

private void setDisplayFrame() {
mainF.setSize(550, 350);
mainF.setResizable(false);
mainF.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}

private void onShutDownRequest() {
mainF.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int reply;
reply = JOptionPane.showConfirmDialog(mainF, "Would you like to Shutdown this Program", "Shutdown Request", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
}

private void setSubPanels() {
createPanelSubPanel(5, "Sort Order", sortOrder, ascendingB, descendingB);
createPanelSubPanel(260, "Numeric Type", numType, integerB, fractionB);
}

private void createPanelSubPanel(int location, String title, JPanel panel, JRadioButton ascendingB, JRadioButton descendingB) {
panel.setLayout(new GridLayout(2, 1));
panel.setBounds(location, 240, 230, 70);

TitledBorder title1;
title1 = BorderFactory.createTitledBorder(title);
panel.setBorder(title1);

ascendingB.setSelected(false);
descendingB.setSelected(true);
ButtonGroup bg = new ButtonGroup();
//add buttons
bg.add(ascendingB);
bg.add(descendingB);
panel.add(ascendingB);
panel.add(descendingB);
bstMain.add(panel);
}

private class ButtonAction extends BinarySearchTree {
ButtonAction(JTextField textField) {
button.addActionListener((ActionEvent event) -> {
CheckData reviewData = new CheckData();
BinarySearchTree<String> unsortedList = new BinarySearchTree<>();
if (integerB.isSelected()) {
sortedList.setText(unsortedList.inOrderSort(unsortedList.root));
}
else {
sortedList.setText(unsortedList.desOrderSort(unsortedList.root));
}
});
}
}
}

最佳答案

不要公开组件,因为这违反了 OOP 原则并增加了代码中出现错误的风险(增加了“耦合”和复杂性)

您当前在添加所有组件之前将 GUI 设置为可见,这可能会导致问题,因为在显示 JFrame 后 GUI 可能无法显示添加到 JFrame 的组件。最好先添加所有内容,然后将 GUI 设置为可见。

其他不相关的问题:

  • 您使用的是 null 布局和 setBounds,这会导致难以调试和增强 GUI,而 GUI 在其他平台上可能看起来很差。
  • 您正在调整 GUI 来创建和显示 JFrame,这通常会让您陷入困境,并且通常可以通过创建 JPanel 来实现更灵活的 GUI 创建,然后将 JPanel 放入 JFrame 或 JDialogs、其他 JPanel 或JTabbedPanes,或在需要时通过 CardLayouts 进行交换。同样,这将大大提高 GUI 编码的灵 active

更好的方法是以更合乎逻辑的方式重新创建 GUI。

坚持住......

关于java - GUI 不会显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60364991/

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