gpt4 book ai didi

java - Java 中的 Swing GUI : Different elements won't layout properly for me

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:40 25 4
gpt4 key购买 nike

我无法让我的 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)); 更改为 super(new BorderLayout());
  • add(charList, BorderLayout.LINE_START); 更改为 add(charList, BorderLayout.PAGE_START);(就个人而言,我更喜欢 BorderLayout.NORTH,但我就是那样的老派)

看看Laying Out Components Within a ContainerHow to Use Borders了解更多详情

关于java - Java 中的 Swing GUI : Different elements won't layout properly for me,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28227664/

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