gpt4 book ai didi

java - 有什么办法可以让 BorderLayout 东、南、西边框变大吗?

转载 作者:行者123 更新时间:2023-12-02 05:45:20 27 4
gpt4 key购买 nike

我一直在为元素周期表的 GUI 编写代码。我的一切看起来都很好,但是我添加到东、西和南边界(金属、非金属和镧系元素)的许多按钮都有一个“...”作为按钮的文本,而不是中心的元素符号(过渡金属)。这是因为当我执行 frame.pack() 时,东、南、西的按钮被塞进了各自的边框。有什么办法可以让这些边框稍微大一些,这样按钮就不会那么拥挤,从而显示它们想要的标题而不是“...”?我尝试更改框架大小和每个按钮的首选按钮大小(第 14 行和第 137 行),但 pack() 方法似乎会覆盖这一点。

public class TestLayout
{
public static void main (String[] args)
{
JFrame window = new JFrame();
JPanel mainPanel = new JPanel();

window.setTitle ("Panda Productions: Periodic Table");
window.setSize(1700, 1200);
window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
window.setContentPane(mainPanel);
window.setResizable(false);

JPanel toggle = new JPanel();
toggle.setBorder(BorderFactory.createTitledBorder("Enable Subshell View:"));
toggle.setPreferredSize(new Dimension(200, 100));
JPanel legend = new JPanel();
legend.setBorder(BorderFactory.createTitledBorder("Legend:"));
JLabel programTitle = new JLabel(" Periodic Table of The Elements ");
programTitle.setFont(new Font("Century Gothic", Font.BOLD, 36));
JLabel errorInfo = new JLabel(" Please Select/Search for an Element...");
errorInfo.setFont(new Font("Century Gothic", Font.BOLD, 14));
JLabel legendLabel = new JLabel();
legendLabel.setText(convertToMultiline("<font color='fuchsia'>Pink = Noble Gas</font> <html><font color='red'>Red = Alkali Metals</font> <font color='blue'>Blue = Alkaline Earth</font> <font color='Green'>Green = Transition Metal</font> \n<font color='yellow'>Yellow = Lanthanide/Actinide</font> <font color='orange'>Orange = Metal</font> <font color='gray'>Gray = Non-Metal</font> <font color='purple'>Purple = Halogen</font> <font color='aqua'>Cyan = Metalloid</font>"));
JTextField elementInput = new JTextField ();
JTextField atomicNumInput = new JTextField ();
JCheckBox viewToggle = new JCheckBox();
JButton searchButton1 = new JButton("Search for Name");
JButton searchButton2 = new JButton("Search for Atomic #");

BorderLayout border = new BorderLayout();

BorderLayout topGrid = new BorderLayout();
GridLayout bottomGrid = new GridLayout(1,3);

JPanel transitionLegendPanel = new JPanel();
BoxLayout transitionLegend = new BoxLayout(transitionLegendPanel, BoxLayout.Y_AXIS);

JPanel userInputPanel = new JPanel();
userInputPanel.setPreferredSize(new Dimension(320, 100));
userInputPanel.setBorder(BorderFactory.createTitledBorder("Search by Element Name or Atomic #:"));
GridLayout userInput = new GridLayout(2,2);

JPanel titlePanel = new JPanel();
BoxLayout titleLayout = new BoxLayout (titlePanel, BoxLayout.Y_AXIS);

GridLayout metalsLayout = new GridLayout(7,2);
GridLayout nonMetalsLayout = new GridLayout(7,6);
GridLayout transitionMetalsLayout = new GridLayout(4,10);
GridLayout lanthanidesLayout = new GridLayout(2,15);

JPanel topGridPanel = new JPanel();
JPanel bottomGridPanel = new JPanel();
JPanel lanthanidesPanel = new JPanel();
JPanel metalsPanel = new JPanel();
JPanel nonMetalsPanel = new JPanel();
JPanel transitionMetalsPanel = new JPanel();

mainPanel.setLayout(border);
topGridPanel.setLayout(topGrid);
bottomGridPanel.setLayout(bottomGrid);
mainPanel.add(topGridPanel, BorderLayout.NORTH);
mainPanel.add(bottomGridPanel, BorderLayout.SOUTH);

metalsPanel.setLayout(metalsLayout);
nonMetalsPanel.setLayout(nonMetalsLayout);
mainPanel.add(metalsPanel, BorderLayout.WEST);
mainPanel.add(nonMetalsPanel, BorderLayout.EAST);

transitionLegendPanel.setLayout(transitionLegend);
mainPanel.add(transitionLegendPanel, BorderLayout.CENTER);

userInputPanel.setLayout(userInput);
titlePanel.setLayout(titleLayout);
topGridPanel.add(userInputPanel, BorderLayout.WEST);
topGridPanel.add(titlePanel, BorderLayout.CENTER);
topGridPanel.add(toggle, BorderLayout.EAST);

bottomGridPanel.add(new JLabel(""));
lanthanidesPanel.setLayout(lanthanidesLayout);
bottomGridPanel.add(lanthanidesPanel);
bottomGridPanel.add(new JLabel(""));

userInputPanel.add(elementInput);
userInputPanel.add(searchButton1);
userInputPanel.add(atomicNumInput);
userInputPanel.add(searchButton2);

titlePanel.add(programTitle);
titlePanel.add(errorInfo);
toggle.add(viewToggle);
legend.add(legendLabel);
transitionLegendPanel.add(legend);
transitionLegendPanel.add(Box.createRigidArea(new Dimension(0,5)));
transitionMetalsPanel.setLayout(transitionMetalsLayout);
transitionLegendPanel.add(transitionMetalsPanel);

BufferedReader reader = null;
ElementsTest[] element = new ElementsTest[118];

try {
File file = new File("Elements.txt");
reader = new BufferedReader(new FileReader(file));

for (int counter = 0 ; counter < 118 ; counter++)
{
String name = reader.readLine();
int atomicNum = counter + 1;
String atomicWeight = reader.readLine();
String elementSymbol = reader.readLine();
String elementCharge = reader.readLine();
String fullElectronConfig = reader.readLine();
String shortElectronConfig = reader.readLine();
String elementState = reader.readLine();
String elementType = reader.readLine();
String density = reader.readLine();
String meltingPoint = reader.readLine();
String boilingPoint = reader.readLine();
String emptyLine = reader.readLine();

element[counter] = new ElementsTest (name, atomicNum, atomicWeight, elementSymbol, elementCharge, fullElectronConfig, shortElectronConfig, elementState, elementType, density, meltingPoint, boilingPoint);
}

} catch (IOException e) {
e.printStackTrace();
}

JButton[] buttonArray = new JButton[118];
for (int i = 0 ; i < 118 ; i++)
{
buttonArray[i] = new JButton();
buttonArray[i].setPreferredSize(new Dimension(10, 10));
}

for (int counter2 = 0 ; counter2 < 118 ; counter2++)
{
String currentSymbol = element[counter2].getElementSymbol();
buttonArray[counter2].setText(currentSymbol);

if (element[counter2].getElementType().equals("Metal"))
{
buttonArray[counter2].setBackground(Color.ORANGE);
buttonArray[counter2].setOpaque(true);
}

if (element[counter2].getElementType().equals("Non-Metal"))
{
buttonArray[counter2].setBackground(Color.GRAY);
buttonArray[counter2].setOpaque(true);
}

if (element[counter2].getElementType().equals("Noble Gas"))
{
buttonArray[counter2].setBackground(Color.PINK);
buttonArray[counter2].setOpaque(true);
}

if (element[counter2].getElementType().equals("Transition Metal"))
{
buttonArray[counter2].setBackground(new Color(17, 131, 14));
buttonArray[counter2].setOpaque(true);
}

if (element[counter2].getElementType().equals("Lanthanide"))
{
buttonArray[counter2].setBackground(Color.YELLOW);
buttonArray[counter2].setOpaque(true);
}

if (element[counter2].getElementType().equals("Alkali Metal"))
{
buttonArray[counter2].setBackground(Color.RED);
buttonArray[counter2].setOpaque(true);
}

if (element[counter2].getElementType().equals("Alkaline Earth Metal"))
{
buttonArray[counter2].setBackground(Color.BLUE);
buttonArray[counter2].setOpaque(true);
}

if (element[counter2].getElementType().equals("Halogen"))
{
buttonArray[counter2].setBackground(new Color(173, 91, 255));
buttonArray[counter2].setOpaque(true);
}

if (element[counter2].getElementType().equals("Metalloid"))
{
buttonArray[counter2].setBackground(Color.CYAN);
buttonArray[counter2].setOpaque(true);
}

if (element[counter2].getElementType().equals("Actinide"))
{
buttonArray[counter2].setBackground(Color.YELLOW);
buttonArray[counter2].setOpaque(true);
}

else {
buttonArray[counter2].setOpaque(true);
}
}

metalsPanel.add(buttonArray[0]);
metalsPanel.add(new JButton(""));
metalsPanel.add(buttonArray[2]);
metalsPanel.add(buttonArray[3]);
metalsPanel.add(buttonArray[10]);
metalsPanel.add(buttonArray[11]);
metalsPanel.add(buttonArray[18]);
metalsPanel.add(buttonArray[19]);
metalsPanel.add(buttonArray[36]);
metalsPanel.add(buttonArray[37]);
metalsPanel.add(buttonArray[54]);
metalsPanel.add(buttonArray[55]);
metalsPanel.add(buttonArray[86]);
metalsPanel.add(buttonArray[87]);

for (int x = 20 ; x < 30 ; x++)
{
transitionMetalsPanel.add(buttonArray[x]);
}

for (int x = 38 ; x < 48 ; x++)
{
transitionMetalsPanel.add(buttonArray[x]);
}

transitionMetalsPanel.add(new JButton("57-71"));

for (int x = 71 ; x < 80 ; x++)
{
transitionMetalsPanel.add(buttonArray[x]);
}

transitionMetalsPanel.add(new JButton("89-103"));

for (int x = 103 ; x < 112 ; x++)
{
transitionMetalsPanel.add(buttonArray[x]);
}

nonMetalsPanel.add(new JButton(""));
nonMetalsPanel.add(new JButton(""));
nonMetalsPanel.add(new JButton(""));
nonMetalsPanel.add(new JButton(""));
nonMetalsPanel.add(new JButton(""));
nonMetalsPanel.add(buttonArray[1]);

for (int x = 4 ; x < 10 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}

for (int x = 12 ; x < 18 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}

for (int x = 30 ; x < 36 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}

for (int x = 48 ; x < 54 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}

for (int x = 80 ; x < 86 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}

for (int x = 112 ; x < 118 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}

for (int x = 56 ; x < 71 ; x++)
{
lanthanidesPanel.add(buttonArray[x]);
}

for (int x = 88 ; x < 103 ; x++)
{
lanthanidesPanel.add(buttonArray[x]);
}

window.pack();
window.setVisible (true);
}

public static String convertToMultiline(String orig)
{
return "<html>" + orig.replaceAll("\n", "<br>");
}
}

当前 GUI 输出: GUI Current Output

最佳答案

NORTHSOUTHEASTWEST 位置由组件首选尺寸决定

最好的选择是首先避免干扰 setPreferredSize 并允许组件和布局管理器在那里工作。

如果您需要提供额外的填充,请使用 EmptyBorder 或边框或布局管理器的组合,它们使您能够指定填充,例如 GridBagLayout (例如最好的控制量)

此外,避免在窗口上使用 setSize,使用 pack 并允许布局管理器根据组件和组件的需要计算窗口的首选大小。字体和其他渲染管道详细信息

例如...

enter image description here

关于java - 有什么办法可以让 BorderLayout 东、南、西边框变大吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24130677/

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