我一直在为周期表的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输出:
答案 0 :(得分:2)
NORTH
,SOUTH
,EAST
和WEST
位置由组件首选大小决定
最好的选择是首先避免弄乱setPreferredSize
并允许组件和布局管理器在那里工作。
如果您需要提供额外的填充,请使用EmptyBorder
或边框组合或布局管理器,它们可以指定填充,例如GridBagLayout
(用于最精细的控制)
另外,避免在窗口上使用setSize
,使用pack
并允许布局管理器根据组件的需要以及字体和其他渲染管道细节计算窗口的首选大小
例如......