簡體   English   中英

GUI 不會顯示按鈕

[英]GUI won't display buttons

在這個程序中,一切都被編譯,看起來我添加了所有按鈕面板和文本字段,但是當我運行程序時,主面板顯示沒有任何添加的對象。 關機彈出屏幕正確顯示。 下面是 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 設置為可見。

其他不相關的問題:

  • 您正在使用空布局和setBounds ,這使得調試和增強 GUI 變得困難,GUI 在其他平台上可能看起來很差。
  • 您正在將 GUI 用於創建和顯示 JFrame,這通常會使您陷入困境,並且通常通過創建 JPanel 來實現更靈活的 GUI 創建,然后可以將其放置到 JFrames 或 JDialogs、其他 JPanel 或 JTabbedPanes 中,或在需要的地方通過 CardLayouts 交換。 同樣,這將大大增加您的 GUI 編碼的靈活性

更好的做法是以更合乎邏輯的方式重新創建 GUI。

不掛斷....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM