将 JSlider 添加到具有 GridLayout 的 JFrame (Java)

Adding JSlider to JFrame which has a GridLayout (Java)

抱歉打扰,但我正在尝试编写一个 "Interactive periodic Table of elements",我想在所有元素的块下添加一个 JSlider:

这是我的代码(没有 JSlider)

import java.awt.Container;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class PeriodicTable extends JFrame {
public static int rows = 7,columns = 18;
static GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
public static int width = gd.getDisplayMode().getWidth();
static int height = gd.getDisplayMode().getHeight();
  public static void main(String[] args) {

    PeriodicTable pt = new PeriodicTable(rows, columns);
    pt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pt.pack();
    pt.setTitle("Interactive Periodic Table of Elements");
    pt.setVisible(true);


    pt.setSize((int)(width/1.2),(int)(height/1.8));
  }

public PeriodicTable(int rows, int columns) {
    final Container pane = getContentPane();
    pane.setLayout(new GridLayout(rows, columns));


      JButton buttonH = new JButton("H");
      buttonH.setBackground(Color.cyan);
      pane.add(buttonH);
      buttonH.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e)
          {
              JOptionPane.showMessageDialog(pane, "Information","Hydrogen", JOptionPane.PLAIN_MESSAGE, null);
          }
      });     
        for (int i = 1;i < 17;i++){
            JButton buttonDefault = new JButton("");
            pane.add(buttonDefault);
            buttonDefault.setBackground(Color.LIGHT_GRAY);   
            }


      JButton buttonHe = new JButton("He");
      buttonHe.setBackground(Color.white);
      pane.add(buttonHe);
      buttonHe.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonLi = new JButton("Li");
      buttonLi.setBackground(Color.ORANGE);
      pane.add(buttonLi);
      buttonLi.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonBe = new JButton("Be");
      buttonBe.setBackground(Color.YELLOW);
      pane.add(buttonBe);      
      buttonBe.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      for (int i = 1;i < 11;i++){
          JButton buttonDefault = new JButton("");
          pane.add(buttonDefault);
          buttonDefault.setBackground(Color.LIGHT_GRAY);   
          } 

      JButton buttonB = new JButton("B");
      buttonB.setBackground(Color.magenta);
      pane.add(buttonB);
      buttonB.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonC = new JButton("C");
      buttonC.setBackground(Color.cyan);
      pane.add(buttonC);
      buttonC.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonN = new JButton("N");
      buttonN.setBackground(Color.cyan);
      pane.add(buttonN);
      buttonN.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonO = new JButton("O");
      buttonO.setBackground(Color.cyan);
      pane.add(buttonO);
      buttonO.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonF = new JButton("F");
      buttonF.setBackground(Color.blue);
      pane.add(buttonF);
      buttonF.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonNe = new JButton("Ne");
      buttonNe.setBackground(Color.white);
      pane.add(buttonNe);
      buttonNe.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonNa = new JButton("Na");
      buttonNa.setBackground(Color.ORANGE);
      pane.add(buttonNa);         
      buttonNa.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonMg = new JButton("Mg");
      buttonMg.setBackground(Color.YELLOW);
      pane.add(buttonMg);
      buttonMg.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      for (int i = 1;i < 11;i++){
          JButton buttonDefault = new JButton("");
          pane.add(buttonDefault);
          buttonDefault.setBackground(Color.LIGHT_GRAY);   
          }
      JButton buttonAl = new JButton("Al");
      buttonAl.setBackground(Color.green);
      pane.add(buttonAl);
      buttonAl.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonSi = new JButton("Si");
      buttonSi.setBackground(Color.magenta);
      pane.add(buttonSi);
      buttonSi.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonP = new JButton("P");
      buttonP.setBackground(Color.cyan);
      pane.add(buttonP);
      buttonP.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonS = new JButton("S");
      buttonS.setBackground(Color.cyan);
      pane.add(buttonS);
      buttonS.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonCl = new JButton("Cl");
      buttonCl.setBackground(Color.blue);
      pane.add(buttonCl);
      buttonCl.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonAr = new JButton("Ar");
      buttonAr.setBackground(Color.white);
      pane.add(buttonAr);
      buttonAr.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonK = new JButton("K");
      buttonK.setBackground(Color.ORANGE);
      pane.add(buttonK);   
      buttonK.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonCa = new JButton("Ca");
      buttonCa.setBackground(Color.YELLOW);
      pane.add(buttonCa);
      buttonCa.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonSc = new JButton("Sc");
      buttonSc.setBackground(Color.pink);
      pane.add(buttonSc);
      buttonSc.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });    
      JButton buttonTi = new JButton("Ti");
      buttonTi.setBackground(Color.pink);
      pane.add(buttonTi);
      buttonTi.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });    
      JButton buttonV = new JButton("V");
      buttonV.setBackground(Color.pink);
      pane.add(buttonV);
      buttonV.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });    
      JButton buttonCr = new JButton("Cr");
      buttonCr.setBackground(Color.pink);
      pane.add(buttonCr);
      buttonCr.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });    
      JButton buttonMn = new JButton("Mn");
      buttonMn.setBackground(Color.pink);
      pane.add(buttonMn);
      buttonMn.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });    
      JButton buttonFe = new JButton("Fe");
      buttonFe.setBackground(Color.pink);
      pane.add(buttonFe);
      buttonFe.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });    
      JButton buttonCo = new JButton("Co");
      buttonCo.setBackground(Color.pink);
      pane.add(buttonCo);
      buttonCo.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });    
      JButton buttonNi = new JButton("Ni");
      buttonNi.setBackground(Color.pink);
      pane.add(buttonNi);
      buttonNi.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });    
      JButton buttonCu = new JButton("Cu");
      buttonCu.setBackground(Color.pink);
      pane.add(buttonCu);
      buttonCu.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });    
      JButton buttonZn = new JButton("Zn");
      buttonZn.setBackground(Color.pink);
      pane.add(buttonZn);
      buttonZn.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });    

      JButton buttonGa = new JButton("Ga");
      buttonGa.setBackground(Color.green);
      pane.add(buttonGa);
      buttonGa.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonGe = new JButton("Ge");
      buttonGe.setBackground(Color.magenta);
      pane.add(buttonGe);
      buttonGe.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonAs = new JButton("As");
      buttonAs.setBackground(Color.magenta);
      pane.add(buttonAs);
      buttonAs.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });   
      JButton buttonSe = new JButton("Se");
      buttonSe.setBackground(Color.cyan);
      pane.add(buttonSe);
      buttonSe.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
      JButton buttonBr = new JButton("Br");
      buttonBr.setBackground(Color.blue);
      pane.add(buttonBr);
      buttonBr.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });   
      JButton buttonKr = new JButton("Kr");
      buttonKr.setBackground(Color.white);
      pane.add(buttonKr);
      buttonKr.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
         {
             JOptionPane.showMessageDialog(pane, "Information","Title", JOptionPane.PLAIN_MESSAGE, null);
         }
     });     
        for (int i = 1;i < 55;i++){
            JButton buttonDefault = new JButton("");
            pane.add(buttonDefault);
            buttonDefault.setBackground(Color.LIGHT_GRAY);   
            }



  }


}

抱歉所有的代码,只是不想错过任何东西。

不管怎样,我想在所有框下面做一个 JSlider,所以它看起来像这样:

    [][][][][][] (blocks)
    [][][][][][] (blocks)
    [][][][][][] (blocks)
    ------------ (slider)

我想将 JSlider 添加到窗格中,但每当我这样做时,它只会将 JSlider 放入 1 个网格(在网格布局中)

非常感谢您的宝贵时间:)

任何帮助将不胜感激!

利用多种布局。使用 GridLayout...

在它们自己的容器中布置按钮
public PeriodicTable(int rows, int columns) {
    final Container pane = new JPanel();
    pane.setLayout(new GridLayout(rows, columns));

然后利用框架的(默认)BorderLayout 添加它和滑块

    getContentPane().add(pane);

    JSlider slider = new JSlider();
    getContentPane().add(slider, BorderLayout.SOUTH);

这是一种非常常用且功能强大的 UI 设计方法,您应该习惯使用它 ;)