我創建了一個 Java 程式,旨在用于以下目的:單擊每個代表元素周期表中的化學元素的 JRadioButton 物件后,由新 JFrame 中的 JLabel 物件表示的相應螢屏截圖將展示周期表中每個化學元素的各種屬性桌子。
該程式具有名為 PeriodicTable、AtomicNumber 和 Properties 的公共類。PeriodicTable 包含 main 方法并實體化 AtomicNumber。AtomicNumber 是一個顯示元素周期表的 JFrame,而 Properties 打開一個新的 JFrame 用于顯示屬性的 png 影像。所有化學元素都由 JRadioButton 物件表示。元素周期表中每種化學元素的各種屬性是一個已保存為 png 影像的螢屏截圖,使用 JLabel ImageIcon 方法實體化。
在按照元素周期表中的確切順序排列所有化學元素后,我注意到 JFrame 中沒有足夠的空間來添加屬性的 png 檔案。為了解決這個問題,我使用名為 Properties 的公共類創建了一個新的 JFrame,其中只能顯示第一個化學元素 Hydrogen 的 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);
}
}
uj5u.com熱心網友回復:
對于所有元素,您應該只需要在 Class 上。
現在,這不是一個答案,而只是對手頭任務的不同方法(概念)的建議,描述它的最佳方式是在行動中看到它:

這實際上來自www.britannica.com,您可以在這里玩。當頁面顯示時,單擊小元素周期表影像以縮放影像。打開一個面板,顯示更大版本的元素周期表,底部是一個影像微調器。將影像微調器滾動到最后一張影像,然后用滑鼠指標單擊它。互動式元素周期表將顯示在視窗中。現在只需將滑鼠指標移到元素周期表元素上,然后選擇要查看其屬性的元素。這基本上就是我會這樣做的方式,但是,對于每個人來說都是如此。我只是想我會傳遞它。
元素周期表本身可以是一個 JPanel,由 103 個 JLabel 組成,采用網格型別布局,每個 JLabel 都有自己的元素影像和其他 JLabel,例如標題和圖例資料。按照你喜歡的方式裝扮它。重要的部分是創建一個全域滑鼠偵聽器來處理所有元素 JLabel 的mouseEntered、mouseExited(用于邊框突出顯示)和mouseClicked(用于顯示屬性)事件。這甚至可能是一個內部類,例如:
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
}
}
然后,當您在 Main 類的成員區域中時,您可能會遇到以下情況:
ElementsMouseHandler elementsMouseHandler;
然后在你初始化所有元素 JLabels 之前,你可以有這樣的東西:
elementsMouseHandler = new ElementsMouseHandler();
當您實際初始化元素 JLabel 時,您可以執行以下操作:
elementJLabel[idx].addMouseListener(elementMouseHandler);
現在,同一個滑鼠偵聽器可以處理所有 Elements JLabel 的滑鼠事件。無論如何……值得深思。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/498056.html
