- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在为元素周期表的 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 输出:
最佳答案
NORTH
、SOUTH
、EAST
和 WEST
位置由组件首选尺寸决定
最好的选择是首先避免干扰 setPreferredSize
并允许组件和布局管理器在那里工作。
如果您需要提供额外的填充,请使用 EmptyBorder
或边框或布局管理器的组合,它们使您能够指定填充,例如 GridBagLayout
(例如最好的控制量)
此外,避免在窗口上使用 setSize
,使用 pack
并允许布局管理器根据组件和组件的需要计算窗口的首选大小。字体和其他渲染管道详细信息
例如...
关于java - 有什么办法可以让 BorderLayout 东、南、西边框变大吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24130677/
来自 BorderLayout javadoc: A border layout lays out a container, arranging and resizing its components
我试图在 BorderLayout 的中心添加一个 GridBagLayout,但是当我将它添加到中心时,它没有出现。 尽管如此,如果我将它放在 BorderLayout 中的另一个位置,它会正确显示
我想制作一个带有 BorderLayout 的程序,其中中心和东部在图形上是分开的。我尝试了以下操作,但不幸的是分隔符位于 JFrame 的左侧,而不是在 centerPanel 和 rightPan
我试图将两个 JButton 彼此相邻放置在 JFrame 的中心,当 JFrame 已重新调整大小。 为此,我将两个按钮放在一个带有 FlowLayout 的面板中,然后将其放置在一个带有中心 Bo
我正在尝试设置披萨订单表单的框架,但在使用 orderPanel 和 ButtonPanel 时遇到问题。我可以让其中之一出现,但不能同时出现?在我发布的当前代码中,显示了按钮,但未显示文本框/ord
我正在从事一个计算机科学项目,我遇到了一些边框布局问题:这是我的配置(简化) public class MainPanel extends JPanel{ JSplitPane Pcenter
我决定编写一个小型 Java 程序来试验 BorderLayout,因为我正在开发一个 Java 游戏,我需要同时将 2 个对象放置在一个 JFrame 中,我问的每个人都说我需要 BorderLay
我一直在尝试在这个的死点制作一个 2x2 网格,但我做不到。网格要么会覆盖其余部分,要么会适合,但我只能看到最后一个按钮。 import javax.swing.*; import java.awt.
我正在编写一个程序,该程序旨在使用在窗口的西侧和东侧有两个按钮的 BorderLayout。不知怎的,中心有一个很大的差距。有什么方法可以消除这个间隙并使两个按钮彼此相切吗?下面我附上了我的代码。如有
我正在制作一个标题栏,并将标题放在左侧,将两个按钮放在右侧。但如果我对两个按钮都使用 BorderLayout.EAST,则只会显示最后一个按钮。我可以让两者都可见吗?我尝试使用 JPanel,但它会
我分别在 MouseEnter 和 MouseExit 上添加和删除 JButton。这工作正常,但是当按钮添加到面板时,它出现在右上角,而不是指定的 BorderLayout.SOUTH 位置。 框
我正在尝试创建一个可以在其中绘画的 Canvas ,以及一个将 Canvas 重置为空状态的按钮。然而,使用 BorderLayout 我的按钮会被复制,第二个按钮是第一个按钮的图像副本,但看起来仍然
我很难弄清楚为什么这个边框布局仍然像流布局一样...... 我先尝试了这个: JPanel canvas = new JPanel(new BorderLayout()); add(canvas);
我是 Java GUI 的新手,目前正在致力于创建微波 GUI。我正在尝试将面板放置在面板中,但它并没有真正按照我认为的那样工作。我正在使用 BorderLayout,基本上将一个面板放入 Borde
我尝试做以下事情: 使用 BorderLayout 将组件添加到 JFrame 向 JFrame 添加另一个组件 我希望新组件能够“覆盖”旧组件,因为我使用的是 BorderLayout。如果我在调用
我有如下代码: public static void main(String[] args) { JFrame f = new BorderTest(); JPanel p = new
据我了解,将组件添加到 BorderLayout 时,它们会占据整个隔间的大小。将 panel1 添加到 contentPane 的北部时,似乎是下面的情况。但是,当我随后将 panel2 放在 pa
我在使用 BorderLayout 时遇到问题,但首先,这是我的 GUI 设置: 如您所见,我的 JFrame 中有 3 个不同的组件。添加 JMenu 和 JList 效果很好。但是我的 JPane
我的问题是: 如何将 JPanel 添加到东南部的 BorderLayout? 这是我的按钮组: buttonsPanel = new JPanel(); buttonsPanel.setLayout
我想要一个 JFrame,在左侧和右侧有一个黑色边框,宽度为 withfOfJFrame/10。 现在,我的尝试看起来像这样: JFrame f = new JFrame(); f.setSize(8
我是一名优秀的程序员,十分优秀!