簡體   English   中英

為什么我的 JButton 和 JLists 沒有出現? (邊框布局

[英]Why are my JButton and JLists not appearing? (BorderLayout

我正在嘗試學習 swing 並完成我們被賦予的任務。 我不明白為什么代碼不在 SOUTH 部分顯示 JButtons,因為在 CENTER 部分顯示文本字段、combobox 和標簽時沒有問題。

我使用與在 SOUTH 和 EAST 中所做的相同的格式將組件添加到我的 CENTER 部分,但只有中心顯示任何內容。

public class ProductListGUI{
    JMenu menu;
    JMenuItem about,importData,inventory,export;

    ProductListGUI(){
        JFrame f = new JFrame("Assignment 2");

        JPanel p1 = new JPanel();
        JList<String> list = new JList<>();
        list.setBounds(600,0,200,600);
        JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        JPanel p3 = new JPanel();

        p1.setLayout(null);
        p1.setBounds(0,0,600,500);
        p1.setBackground(new Color(230,230,230));
        scrollPane.setLayout(null);
        scrollPane.setBounds(600,0,200,500);
        p3.setLayout(null);
        p3.setBounds(0,500,800,100);
        p3.setBackground(new Color(230,230,230));

        JLabel l1,l2,l3,l4;
        JTextField t1,t2,t3;
        l1=new JLabel("ProductID");
        l1.setBounds(10,100,200,30); 
        t1=new JTextField();  
        t1.setBounds(100,100,200,30);
        l2=new JLabel("Name");
        l2.setBounds(10,150,200,30); 
        t2=new JTextField();
        t2.setBounds(100,150,200,30);
        l3=new JLabel("Quantity");
        l3.setBounds(10,250,200,30); 
        t3=new JTextField();
        t3.setBounds(100,250,200,30);
        p1.setBorder(BorderFactory.createTitledBorder("Product Details"));

        JCheckBox checkBox = new JCheckBox("Available for Next Day Delivery");
        checkBox.setBounds(10,300,250,50);

        l4 = new JLabel("Item Type");
        l4.setBounds(10,200,200,30);
        String[] itemType = {"Select type","Homeware","Hobby","Garden"};
        JComboBox dropdown = new JComboBox(itemType);
        dropdown.setBounds(100,200,120,20);

        p1.add(t1);p1.add(l1);p1.add(t2);p1.add(l2);p1.add(t3);p1.add(l3);p1.add(l4);p1.add(dropdown);p1.add(checkBox);


        JButton b1 = new JButton("New Item");
        b1.setBounds(200,550,80,20);
        JButton b2 = new JButton("Save");
        b2.setBounds(300,550,80,20);
        JButton b3 = new JButton("Delete Selected");
        b3.setBounds(600,550,80,20);
        b3.setEnabled(false);

        p3.add(b1);p3.add(b2);p3.add(b3);


        JMenuBar mb = new JMenuBar();
        menu = new JMenu("Actions");

        about = new JMenuItem("About");
        importData = new JMenuItem("Import Data");
        inventory = new JMenuItem("Inventory");
        export = new JMenuItem("Export to CSV");

        menu.add(about);menu.add(importData);menu.add(inventory);menu.add(export);
        mb.add(menu);
        
        f.getContentPane().add(p1,BorderLayout.CENTER);
        f.getContentPane().add(scrollPane,BorderLayout.EAST);
        f.getContentPane().add(p3,BorderLayout.SOUTH);
        f.setJMenuBar(mb);
        f.setSize(800,600);
        f.setLayout(new BorderLayout());
        f.setVisible(true);
    }

這些代碼更改至少應該有所幫助。

必須改變的事情:

  • 每個 JPanel 都應該有一個布局。 將布局設置為null (根據 TG 的評論)並不好。
  • 刪除了按鈕的setBounds()方法。
  • f.setLayout(new BorderLayout()); 被移到代碼的頂部,在設置布局之前, Components被添加到 JFrame。
import javax.swing.*;
import java.awt.*;

public class ProductListGUI {

    JMenu menu;
    JMenuItem about,importData,inventory,export;

    ProductListGUI(){
        JFrame f = new JFrame("Assignment 2");

        JPanel panel1 = new JPanel();
        JList<String> list = new JList<>();
        list.setBounds(600,0,200,600);
        JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        JPanel panel3 = new JPanel();

        panel1.setBounds(0,0,600,500);
        panel1.setBackground(new Color(230,230,230));
        scrollPane.setBounds(600,0,200,500);
        panel3.setBounds(0,0,800,100);
        panel3.setBackground(new Color(230,230,230));

        JLabel l1,l2,l3,l4;
        JTextField t1,t2,t3;
        l1=new JLabel("ProductID");
        l1.setBounds(10,100,200,30);
        t1=new JTextField();
        t1.setBounds(100,100,200,30);
        l2=new JLabel("Name");
        l2.setBounds(10,150,200,30);
        t2=new JTextField();
        t2.setBounds(100,150,200,30);
        l3=new JLabel("Quantity");
        l3.setBounds(10,250,200,30);
        t3=new JTextField();
        t3.setBounds(100,250,200,30);
        panel1.setBorder(BorderFactory.createTitledBorder("Product Details"));

        JCheckBox checkBox = new JCheckBox("Available for Next Day Delivery");
        checkBox.setBounds(10,300,250,50);

        l4 = new JLabel("Item Type");
        l4.setBounds(10,200,200,30);
        String[] itemType = {"Select type","Homeware","Hobby","Garden"};
        JComboBox dropdown = new JComboBox(itemType);
        dropdown.setBounds(100,200,120,20);

        panel1.add(t1);
        panel1.add(l1);
        panel1.add(t2);
        panel1.add(l2);
        panel1.add(t3);
        panel1.add(l3);
        panel1.add(l4);
        panel1.add(dropdown);
        panel1.add(checkBox);


        JButton b1 = new JButton("New Item");
        JButton b2 = new JButton("Save");
        JButton b3 = new JButton("Delete Selected");
        b3.setEnabled(false);

        panel3.add(b1);
        panel3.add(b2);
        panel3.add(b3);


        JMenuBar mb = new JMenuBar();
        menu = new JMenu("Actions");

        about = new JMenuItem("About");
        importData = new JMenuItem("Import Data");
        inventory = new JMenuItem("Inventory");
        export = new JMenuItem("Export to CSV");

        menu.add(about);
        menu.add(importData);
        menu.add(inventory);
        menu.add(export);
        mb.add(menu);

        f.setLayout(new BorderLayout());
        f.getContentPane().add(panel1,BorderLayout.CENTER);
        f.getContentPane().add(scrollPane,BorderLayout.EAST);
        f.getContentPane().add(panel3,BorderLayout.SOUTH);
        f.setJMenuBar(mb);
        f.setSize(800,600);
        f.setVisible(true);
    }

    public static void main(String[] args) {

        ProductListGUI gui = new ProductListGUI();
    }
}

這是一個非常簡單的 BorderLayout 示例,將來可能會有所幫助,也可能不會。

import javax.swing.*;
import java.awt.*;

public class TestGui {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Test Frame");

        frame.setLayout(new BorderLayout());
        frame.setSize(800,600);
        frame.getContentPane().add(createJPanel("CENTER", Color.RED), BorderLayout.CENTER);
        frame.getContentPane().add(createJPanel("NORTH", Color.CYAN), BorderLayout.NORTH);
        frame.getContentPane().add(createJPanel("EAST", Color.LIGHT_GRAY), BorderLayout.EAST);
        frame.getContentPane().add(createJPanel("SOUTH", Color.GREEN), BorderLayout.SOUTH);
        frame.getContentPane().add(createJPanel("WEST", Color.YELLOW), BorderLayout.WEST);

        frame.setVisible(true);
    }

    private static JPanel createJPanel(String title, Color color) {

        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout());
        jPanel.add(new JLabel(title), BorderLayout.CENTER);
        jPanel.setBackground(color);

        return jPanel;
    }
}

暫無
暫無

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

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