我创建了一个用于以下目的的Java程序:单击表示周期表中化学元素的每个JRadioButton对象时,新JFrame中由JLabel对象表示的相应屏幕截图将显示周期表中每个化学元素的各种属性。
该程序具有名为PeriodicTable、AtomicNumber和Properties的公共类。PeriodicTable包含主方法并实例化AtomicNumber。AtomicNumber是一个显示周期表的JFrame,而属性打开一个新的JFrame来显示属性的png图像。所有的化学元素都用JRadioButton对象来表示。周期表中每个化学元素的各种属性都是一个屏幕截图,它被保存为png图像,并用JLabel ImageIcon方法实例化。
在按照元素周期表的顺序排列所有化学元素之后,我注意到JFrame中没有足够的空间来添加属性的png文件。为了解决这个问题,我创建了一个名为Properties的公共类的新JFrame,其中只能显示第一个化学元素氢的png图像。虽然为属性调整png图像的大小似乎是一种可行的方法,但字体太小,因此难以辨认。为了显示属性的不同png图像,根据单击表示化学元素的JRadioButton对象,我尝试在公共类AtomicNumber中添加和删除ActionEvent中的png图像,但没有结果。
我已经考虑过创建100多个类来显示每个包含相应属性png文件的窗口的可能性,但这似乎太麻烦了。在不创建100+类的情况下,在单击表示化学元素的JRadioButton对象时,是否有其他方法显示每个相应的png属性文件?谢谢你的帮助。
我的代码如下:
//PeriodicTable.java
public class PeriodicTable {
public static void main(String[] args) {
new AtomicNumber();
}
}
********************
//AtomicNumber.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class AtomicNumber extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
static ButtonGroup periodicTable;
ImageIcon[] elementImage;
ImageIcon periodicTableIcon;
static JButton[] button;
static JLabel[] properties;
static JPanel panel;
static JRadioButton[] element;
JTextField title;
AtomicNumber() {
elementClip = new Clip[119];
propertiesClip = new Clip[119];
elementAudioFile = new File[119];
propertiesAudioFile = new File[119];
elementAudioFile[1] = new File("H.wav");
elementImage = new ImageIcon[119];
properties = new JLabel[119];
button = new JButton[2];
button[0] = new JButton("reset");
button[0].setBounds(1000, 500, 200, 50);
button[1] = new JButton("restart");
button[1].setBounds(1000, 550, 200, 50);
for (int i = 0; i < 2; i++) {
button[i].addActionListener(this);
button[i].setFocusable(false);
button[i].setFont(new Font("Times New Roman", Font.BOLD, 30));
button[i].setPreferredSize(new Dimension(240, 50));
}
title = new JTextField("The Periodic Table of the Chemical Elements");
title.setBounds(0, 0, 1080, 65);
title.setEditable(false);
title.setFont(new Font("Arial", Font.BOLD, 50));
element = new JRadioButton[119];
element[1] = new JRadioButton("1");
element[1].setBounds(50, 75, 50, 50);
elementImage[1] = new ImageIcon(getClass().getClassLoader().getResource("H.png"));
properties[1] = new JLabel();
element[2] = new JRadioButton("2");
element[2].setBounds(925, 75, 50, 50);
elementImage[2] = new ImageIcon(getClass().getClassLoader().getResource("He.png"));
properties[2] = new JLabel();
element[3] = new JRadioButton("3");
element[3].setBounds(50, 125, 50, 50);
elementImage[3] = new ImageIcon(getClass().getClassLoader().getResource("Li.png"));
properties[3] = new JLabel();
element[4] = new JRadioButton("4");
element[4].setBounds(100, 125, 50, 50);
elementImage[4] = new ImageIcon(getClass().getClassLoader().getResource("Be.png"));
properties[4] = new JLabel();
element[5] = new JRadioButton("5");
element[5].setBounds(675, 125, 50, 50);
elementImage[5] = new ImageIcon(getClass().getClassLoader().getResource("B.png"));
properties[5] = new JLabel();
element[6] = new JRadioButton("6");
element[6].setBounds(725, 125, 50, 50);
elementImage[6] = new ImageIcon(getClass().getClassLoader().getResource("C.png"));
properties[6] = new JLabel();
element[7] = new JRadioButton("7");
element[7].setBounds(775, 125, 50, 50);
elementImage[7] = new ImageIcon(getClass().getClassLoader().getResource("N.png"));
properties[7] = new JLabel();
element[8] = new JRadioButton("8");
element[8].setBounds(825, 125, 50, 50);
elementImage[8] = new ImageIcon(getClass().getClassLoader().getResource("O.png"));
properties[8] = new JLabel();
element[9] = new JRadioButton("9");
element[9].setBounds(875, 125, 50, 50);
elementImage[9] = new ImageIcon(getClass().getClassLoader().getResource("F.png"));
properties[9] = new JLabel();
element[10] = new JRadioButton("10");
element[10].setBounds(925, 125, 50, 50);
elementImage[10] = new ImageIcon(getClass().getClassLoader().getResource("Ne.png"));
properties[10] = new JLabel();
element[11] = new JRadioButton("11");
element[11].setBounds(50, 175, 50, 50);
elementImage[11] = new ImageIcon(getClass().getClassLoader().getResource("Na.png"));
properties[11] = new JLabel();
element[12] = new JRadioButton("12");
element[12].setBounds(100, 175, 50, 50);
elementImage[12] = new ImageIcon(getClass().getClassLoader().getResource("Mg.png"));
properties[12] = new JLabel();
element[13] = new JRadioButton("13");
element[13].setBounds(675, 175, 50, 50);
elementImage[13] = new ImageIcon(getClass().getClassLoader().getResource("Al.png"));
properties[13] = new JLabel();
element[14] = new JRadioButton("14");
element[14].setBounds(725, 175, 50, 50);
elementImage[14] = new ImageIcon(getClass().getClassLoader().getResource("Si.png"));
properties[14] = new JLabel();
element[15] = new JRadioButton("15");
element[15].setBounds(775, 175, 50, 50);
elementImage[15] = new ImageIcon(getClass().getClassLoader().getResource("P.png"));
properties[15] = new JLabel();
element[16] = new JRadioButton("16");
element[16].setBounds(825, 175, 50, 50);
elementImage[16] = new ImageIcon(getClass().getClassLoader().getResource("S.png"));
properties[16] = new JLabel();
element[17] = new JRadioButton("17");
element[17].setBounds(875, 175, 50, 50);
elementImage[17] = new ImageIcon(getClass().getClassLoader().getResource("Cl.png"));
properties[17] = new JLabel();
element[18] = new JRadioButton("18");
element[18].setBounds(925, 175, 50, 50);
elementImage[18] = new ImageIcon(getClass().getClassLoader().getResource("Ar.png"));
properties[18] = new JLabel();
element[19] = new JRadioButton("19");
element[19].setBounds(50, 225, 50, 50);
elementImage[19] = new ImageIcon(getClass().getClassLoader().getResource("K.png"));
properties[19] = new JLabel();
element[20] = new JRadioButton("20");
element[20].setBounds(100, 225, 50, 50);
elementImage[20] = new ImageIcon(getClass().getClassLoader().getResource("Ca.png"));
properties[20] = new JLabel();
element[21] = new JRadioButton("21");
element[21].setBounds(150, 225, 50, 50);
elementImage[21] = new ImageIcon(getClass().getClassLoader().getResource("Sc.png"));
properties[21] = new JLabel();
element[22] = new JRadioButton("22");
element[22].setBounds(225, 225, 50, 50);
elementImage[22] = new ImageIcon(getClass().getClassLoader().getResource("Ti.png"));
properties[22] = new JLabel();
element[23] = new JRadioButton("23");
element[23].setBounds(275, 225, 50, 50);
elementImage[23] = new ImageIcon(getClass().getClassLoader().getResource("V.png"));
properties[23] = new JLabel();
element[24] = new JRadioButton("24");
element[24].setBounds(325, 225, 50, 50);
elementImage[24] = new ImageIcon(getClass().getClassLoader().getResource("Cr.png"));
properties[24] = new JLabel();
element[25] = new JRadioButton("25");
element[25].setBounds(375, 225, 50, 50);
elementImage[25] = new ImageIcon(getClass().getClassLoader().getResource("Mn.png"));
properties[25] = new JLabel();
element[26] = new JRadioButton("26");
element[26].setBounds(425, 225, 50, 50);
elementImage[26] = new ImageIcon(getClass().getClassLoader().getResource("Fe.png"));
properties[26] = new JLabel();
element[27] = new JRadioButton("27");
element[27].setBounds(475, 225, 50, 50);
elementImage[27] = new ImageIcon(getClass().getClassLoader().getResource("Co.png"));
properties[27] = new JLabel();
element[28] = new JRadioButton("28");
element[28].setBounds(525, 225, 50, 50);
elementImage[28] = new ImageIcon(getClass().getClassLoader().getResource("Ni.png"));
properties[28] = new JLabel();
element[29] = new JRadioButton("29");
element[29].setBounds(575, 225, 50, 50);
elementImage[29] = new ImageIcon(getClass().getClassLoader().getResource("Cu.png"));
properties[29] = new JLabel();
element[30] = new JRadioButton("30");
element[30].setBounds(625, 225, 50, 50);
elementImage[30] = new ImageIcon(getClass().getClassLoader().getResource("Zn.png"));
properties[30] = new JLabel();
element[31] = new JRadioButton("31");
element[31].setBounds(675, 225, 50, 50);
elementImage[31] = new ImageIcon(getClass().getClassLoader().getResource("Ga.png"));
properties[31] = new JLabel();
element[32] = new JRadioButton("32");
element[32].setBounds(725, 225, 50, 50);
elementImage[32] = new ImageIcon(getClass().getClassLoader().getResource("Ge.png"));
properties[32] = new JLabel();
element[33] = new JRadioButton("33");
element[33].setBounds(775, 225, 50, 50);
elementImage[33] = new ImageIcon(getClass().getClassLoader().getResource("As.png"));
properties[33] = new JLabel();
element[34] = new JRadioButton("34");
element[34].setBounds(825, 225, 50, 50);
elementImage[34] = new ImageIcon(getClass().getClassLoader().getResource("Se.png"));
properties[34] = new JLabel();
element[35] = new JRadioButton("35");
element[35].setBounds(875, 225, 50, 50);
elementImage[35] = new ImageIcon(getClass().getClassLoader().getResource("Br.png"));
properties[35] = new JLabel();
element[36] = new JRadioButton("36");
element[36].setBounds(925, 225, 50, 50);
elementImage[36] = new ImageIcon(getClass().getClassLoader().getResource("Kr.png"));
properties[36] = new JLabel();
element[37] = new JRadioButton("37");
element[37].setBounds(50, 275, 50, 50);
elementImage[37] = new ImageIcon(getClass().getClassLoader().getResource("Rb.png"));
properties[37] = new JLabel();
element[38] = new JRadioButton("38");
element[38].setBounds(100, 275, 50, 50);
elementImage[38] = new ImageIcon(getClass().getClassLoader().getResource("Sr.png"));
properties[38] = new JLabel();
element[39] = new JRadioButton("39");
element[39].setBounds(150, 275, 50, 50);
elementImage[39] = new ImageIcon(getClass().getClassLoader().getResource("Y.png"));
properties[39] = new JLabel();
element[40] = new JRadioButton("40");
element[40].setBounds(225, 275, 50, 50);
elementImage[40] = new ImageIcon(getClass().getClassLoader().getResource("Zr.png"));
properties[40] = new JLabel();
element[41] = new JRadioButton("41");
element[41].setBounds(275, 275, 50, 50);
elementImage[41] = new ImageIcon(getClass().getClassLoader().getResource("Nb.png"));
properties[41] = new JLabel();
element[42] = new JRadioButton("42");
element[42].setBounds(325, 275, 50, 50);
elementImage[42] = new ImageIcon(getClass().getClassLoader().getResource("Mo.png"));
properties[42] = new JLabel();
element[43] = new JRadioButton("43");
element[43].setBounds(375, 275, 50, 50);
elementImage[43] = new ImageIcon(getClass().getClassLoader().getResource("Tc.png"));
properties[43] = new JLabel();
element[44] = new JRadioButton("44");
element[44].setBounds(425, 275, 50, 50);
elementImage[44] = new ImageIcon(getClass().getClassLoader().getResource("Ru.png"));
properties[44] = new JLabel();
element[45] = new JRadioButton("45");
element[45].setBounds(475, 275, 50, 50);
elementImage[45] = new ImageIcon(getClass().getClassLoader().getResource("Rh.png"));
properties[45] = new JLabel();
element[46] = new JRadioButton("46");
element[46].setBounds(525, 275, 50, 50);
elementImage[46] = new ImageIcon(getClass().getClassLoader().getResource("Pd.png"));
properties[46] = new JLabel();
element[47] = new JRadioButton("47");
element[47].setBounds(575, 275, 50, 50);
elementImage[47] = new ImageIcon(getClass().getClassLoader().getResource("Ag.png"));
properties[47] = new JLabel();
element[48] = new JRadioButton("48");
element[48].setBounds(625, 275, 50, 50);
elementImage[48] = new ImageIcon(getClass().getClassLoader().getResource("Cd.png"));
properties[48] = new JLabel();
element[49] = new JRadioButton("49");
element[49].setBounds(675, 275, 50, 50);
elementImage[49] = new ImageIcon(getClass().getClassLoader().getResource("In.png"));
properties[49] = new JLabel();
element[50] = new JRadioButton("50");
element[50].setBounds(725, 275, 50, 50);
elementImage[50] = new ImageIcon(getClass().getClassLoader().getResource("Sn.png"));
properties[50] = new JLabel();
element[51] = new JRadioButton("51");
element[51].setBounds(775, 275, 50, 50);
elementImage[51] = new ImageIcon(getClass().getClassLoader().getResource("Sb.png"));
properties[51] = new JLabel();
element[52] = new JRadioButton("52");
element[52].setBounds(825, 275, 50, 50);
elementImage[52] = new ImageIcon(getClass().getClassLoader().getResource("Te.png"));
properties[52] = new JLabel();
element[53] = new JRadioButton("53");
element[53].setBounds(875, 275, 50, 50);
elementImage[53] = new ImageIcon(getClass().getClassLoader().getResource("I.png"));
properties[53] = new JLabel();
element[54] = new JRadioButton("54");
element[54].setBounds(925, 275, 50, 50);
elementImage[54] = new ImageIcon(getClass().getClassLoader().getResource("Xe.png"));
properties[54] = new JLabel();
element[55] = new JRadioButton("55");
element[55].setBounds(50, 325, 50, 50);
elementImage[55] = new ImageIcon(getClass().getClassLoader().getResource("Cs.png"));
properties[55] = new JLabel();
element[56] = new JRadioButton("56");
element[56].setBounds(100, 325, 50, 50);
elementImage[56] = new ImageIcon(getClass().getClassLoader().getResource("Ba.png"));
properties[56] = new JLabel();
/*element[57]=new JRadioButton("57);
elementImage[57]=new ImageIcon(getClass().getClassLoader().getResource("La.png"));
properties[57]=new JLabel();*/
periodicTableIcon = new ImageIcon(getClass().getClassLoader().getResource("periodicTableIcon.png"));
periodicTable = new ButtonGroup();
panel = new JPanel();
panel.add(title);
for (int i = 1; i < 57; i++) {
element[i].addActionListener(this);
element[i].setFocusable(false);
element[i].setIcon(elementImage[i]);
element[i].setHorizontalTextPosition(SwingConstants.CENTER);
element[i].setVerticalTextPosition(SwingConstants.TOP);
periodicTable.add(element[i]);
panel.add(element[i]);
panel.add(properties[i]);
}
for (int i = 0; i < 2; i++) {
panel.add(button[i]);
}
panel.setLayout(null);
panel.setOpaque(false);
this.add(panel);
this.getContentPane().setBackground(Color.WHITE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setIconImage(periodicTableIcon.getImage());
this.setSize(1500, 1500);
this.setTitle("The Periodic Table of the Chemical Elements");
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Properties Properties = new Properties();
for (int i = 1; i < 119; i++) {
if (e.getSource() == button[0] && element[i].isSelected()) {
periodicTable.clearSelection();
}
}
if (e.getSource() == button[1]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new AtomicNumber();
JOptionPane.showMessageDialog(null, "Program has restarted.", "Restart", JOptionPane.INFORMATION_MESSAGE);
}
});
}
if (element[1].isSelected()) {
new Properties().setVisible(true);}
********************
//Properties.java
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Properties extends JFrame {
private static final long serialVersionUID = 1L;
JLabel[] properties;
JPanel propertiesPanel;
Properties() {
properties = new JLabel[119];
properties[0] = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("H_properties.png")));
propertiesPanel = new JPanel();
propertiesPanel.add(properties[0]);
this.add(propertiesPanel);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.setSize(1250, 340);
}
}
发布于 2022-06-29 05:23:24
对于所有的元素,您应该只需要在类中使用。
现在,这不是一个答案,而仅仅是一个建议,也许对手头的任务采取一种不同的方法(概念),描述它的最好方法是看到它付诸行动:
这实际上来自www.britannica.com,您可以使用在这里玩。当页面显示时,单击小周期表图像,以便缩放图像。打开一个面板,显示更大版本的周期表,底部是图像旋转器。将图像旋转到最后一个图像,并使用鼠标指针单击它。窗口中将显示一个交互式周期表。现在,只需将鼠标指针移到周期表元素上,并选择要查看属性的元素。这基本上是我做这件事的方式,但是,对每个人来说都是如此。我只是想把它传过去。
元素周期表本身可以是一个由103个JLabels组成的网格类型布局,每个元素都有自己的元素映像,以及其他用于标题和图例数据的JLabels。按你喜欢的方式打扮。重要的部分是创建一个全局鼠标侦听器来处理mouseEntered、mouseExited (用于边框突出显示)和mouseClicked (用于显示属性)所有元素JLabels的事件。这甚至可能是一个内部类,例如:
private class ElementsMouseHandler extends java.awt.event.MouseAdapter {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
// Do whatever you like here
}
@Override
public void mouseReleased(java.awt.event.MouseEvent evt) {
// Example:
// Ignore the right mouse button.
if (SwingUtilities.isRightMouseButton(evt)) {
return;
}
/* Which JLabel fired the event. You can do this also in
the other events below. */
javax.swing.JLabel source = (javax.swing.JLabel) evt.getSource();
// Get the name placed into the element JLabel's Name property
String elementName = source.getName();
/* Do whatever you want here like perhaps display another JPanel
in the window containing the properties information or the
image thereof related to the elementName retrieved. */
}
@Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
// Do whatever you like here
}
@Override
public void mouseExited(java.awt.event.MouseEvent evt) {
// Do whatever you like here
}
}
然后,当您的主要类的成员区域中,您可能有以下内容:
ElementsMouseHandler elementsMouseHandler;
然后,在初始化所有元素JLabels之前,您可以得到如下内容:
elementsMouseHandler = new ElementsMouseHandler();
当您实际初始化元素JLabel时,您可以这样做:
elementJLabel[idx].addMouseListener(elementMouseHandler);
现在,相同的鼠标侦听器可以处理所有元素JLabels的鼠标事件。Anyways...food代表思想。
https://stackoverflow.com/questions/72799802
复制