繁体   English   中英

Java中的Swing GUI:不同的元素将不适合我

[英]Swing GUI in Java: Different elements won't layout properly for me

我无法让我的GUI完成我想要的操作。 这是我第一次使用Swing,所以如果这是一个愚蠢的问题我会提前道歉。 我的大部分代码也是从oracle.com上的样本中复制/粘贴的,但我觉得我对它的所有功能都有所了解。

在我的Swing GUI中,我目前有两个元素:一个JTable和一个JComboBox(我相信它是由JScrollPanel包含的)。 我希望它格式化的方式是使用表格上方的ComboBox,但我似乎只能让它向左或向右移动。 我以为我可以使用这一行:

add(charList, BorderLayout.LINE_START);

其中“charList”是我的ComboBox,但它似乎没有任何影响。 无论我设置它还是其他元素,它仍然保持一致。

这是我的完整代码,全部在一个文件中:

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

public class MeleeEdit extends JPanel {
    private boolean DEBUG = false;

    public  String[] attributes = {
            "Initial Walk Velocity", "Walk Acceleration?", "Walk Maximum Velocity", "Slow Walk Max?"    
     };
    public  String[] description = {
            "N/A",
            "N/A, bruh",
            "N/A, BRUH",
            "N/A, BRUH!"    
     };

    public MeleeEdit() {
        super(new GridLayout(1,0));

        String[] characters = { "Captain Falcon", "Young Link", "Donkey Kong", "Doctor Mario", "Falco" };
        JComboBox charList = new JComboBox(characters);
        charList.setSelectedIndex(0);
        //petList.addActionListener(this);


        JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 600));
        table.setFillsViewportHeight(true);


        table.add(Box.createHorizontalStrut(5));
        table.add(new JSeparator(SwingConstants.VERTICAL));
        table.add(Box.createHorizontalStrut(5));
        table.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);
        //scrollPane.setLayout(BorderLayout.CENTER);

        //Add the scroll pane to this panel.
        add(charList, BorderLayout.LINE_START);
        add(scrollPane, BorderLayout.CENTER);

    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"Attribute",
                                        "Value",
                                        "Info",
                                        };
        private Object[][] data = initGrid();

        public Object[][] initGrid(){
            Object[][] tmp = new Object[3][3];
            for(int i = 0; i < 3; i ++){
                tmp[i][0] = attributes[i];
                tmp[i][1] = new Float(4.5);
                tmp[i][2] = description[i];
            }
            return tmp;
        }


        public int getColumnCount() {

            return columnNames.length;
        }
        public int getRowCount() {
            return data.length;
        }
        public String getColumnName(int col) {
            return columnNames[col];
        }
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if(col==1)
                return true;
            else
                return false;

        }

        public void setValueAt(Object value, int row, int col) {
            if(value==null)
                return;
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Melee Character Attribute Editor v0.1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        MeleeEdit newContentPane = new MeleeEdit();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

我似乎无法弄清楚它为什么不起作用。 有什么建议么?

  • 改变super(new GridLayout(1,0)); to super(new BorderLayout());
  • 更改add(charList, BorderLayout.LINE_START); add(charList, BorderLayout.PAGE_START); (就个人而言,我更喜欢BorderLayout.NORTH ,但我很老了)

请查看在容器布置组件以及如何使用边框以获取更多详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM